Set and Retrieve Lookup

Good Afternoon,

I am having an issue on a form where on load I retrieve data from the arguments passed through when opening in a dialog and using that data to set lookup fields. The issue I have is when I then use a function to retrieve the value of the lookup the value is not retrieve properly. My code to set the a field from the arguments is below: -

// Function to check if args and set Address from Args
function checkAndSetAddressFromArgs () {
	if (window.frameElement && Dialog.getArgs()){
		var addressNameId = Dialog.getArgs().addressNameId;
		if (addressNameId == null || addressNameId == '' || addressNameId == 'undefined' || addressNameId == 'null') {
			fd.field("Address_x0020_Name").value = null;
		} else {
			fd.field("Address_x0020_Name").value = addressNameId;
		}
	}	
}

If I then try to retrieve the value of the above Lookup for use in another function as per - var addressNameId = fd.field('Address_x0020_Name').value.LookupId; it is returned as undefined. If I use - var addressNameId = fd.field('Address_x0020_Name').value; This does return the ID rather than the object…

Help appreciated.

Dear Tony,
Yes, that’s because you are setting field value with ID, so the value is number. In order to retrieve full value with display field and extra fields, please, use fd.field('Lookup').widget.dataSource.data();

For example:

//fd.field('Lookup').value == ID
var items = fd.field('Lookup').widget.dataSource.data();
for(var i = 0; i < items.length; i++){
	if(items[i].LookupId == fd.field('Lookup').value){
		fd.field('Lookup').value = items[i];
		break;
    }
}
//use fd.field('Lookup').value here
alert(fd.field('Lookup').value.LookupValue);
1 Like