Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Populate a Contact Lookup Field from a QR Code Value

Posted on by Microsoft Employee

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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans