I've seen several posts about populating lookup fields using a QR code field value (such as a GUID). I had to find a way to populate a Contact lookup field using a QR Code as a means to take attendance at an event...1:N relationship from the custom event entity to contacts. I was using Google's QR Generator API to dynamically create unique QR Codes that are linked to from a customer service portal. Google's API for QR Generation can be accessed by using:
chart.googleapis.com/chart value you want to encode]
I found that most suggested answers for accomplishing the lookup field population involved a now deprecated HTTP Request function to perform a lookup action on the CRM (also involved using additional SDK libraries). In my case, however, I was able to completely bypass the use of an HTTP Request and additional libraries and instead directly set the necessary lookup field attributes: contact ID, contact display name (i.e. "John Smith"), and entity type (contact). Here is my custom JavaScript:
function QRValue_OnChange() {
var qrvalue = Xrm.Page.getAttribute("demo_attendeeqrvalue").getValue(); // the QR value
var splitqr;
if(qrvalue) {
splitqr=qrvalue.split("&");
setLookupValue(splitqr[0].replace("%20"," "),splitqr[1]);
}
}
function setLookupValue(lookupName, lookupId) {
var lookupData = new Array();
var lookupItem = new Object();
lookupItem.id = lookupId;
lookupItem.name = lookupName;
lookupItem.entityType = "contact";
lookupData[0] = lookupItem;
Xrm.Page.getAttribute("demo_attendee").setValue(lookupData); // the lookup field to populate
}
In this case, I encoded the contact's name followed by an ampersand (&) and then the contact's unique id. I used the & symbol as a means to later split the QR code into contact name and contact ID before passing the parameters to the lookup field set function. (I have a replace function call because spaces are usually encoded as '%20' in URLs.) This JS file runs onChange() of the QR field (Google how to set up a custom JS Web Resource File on a form). This strategy worked like a charm for our needs. Hope this helps someone! Let me know if it does so I can feel good :)
*This post is locked for comments