Relate field auto popup value

Once click on the related field if the listview popup has only one record then the related field should have that value automatically else the user has select the value from the multiple records. I need this functionality in EditView of suitecrm.

How can I achieve this using the custom code.

Here is the solution: