Use Javascript to set a lookup based on matching fields
-
Thursday, November 15, 2012 9:50 PM
I have two entities, A and B, each of which have fields x and y. When x and y are filled in on a new record of type A, I would like a lookup field z to be filled in with the value for a preexisting record of type B (matched by x and y). There should only be one match (or possibly no matches). How can this be accomplished with javascript?
- Edited by CRMuser123 Friday, November 16, 2012 1:46 AM
All Replies
-
Friday, November 16, 2012 12:53 AM
You might be able to do this using workflows and the onchange of certain fields. You can definitely do this if the other entity is a lookup entity.
I have to say your description of the problem is confusing.
You might be better doing this as a plugin rather than javascript
Ben Hosking
Check out my CRM Blog
Linked-In Profile
Follow Me on Twitter! -
Friday, November 16, 2012 1:00 AM
I have to say your description of the problem is confusing.
Please let me know how my description could be made any clearer. -
Friday, November 16, 2012 1:05 AM
ok, what are you trying to accomplish
You have entity A which is created with fields x and y. Then for some reason you seem to want a lookup of entity B on the entity A form with the same field values?
is entity b newly created?
it sounds like you could have a workflow which gets triggered and creates entity b and assigns to the lookup
Ben Hosking
Check out my CRM Blog
Linked-In Profile
Follow Me on Twitter! -
Friday, November 16, 2012 1:45 AM
OK, I see. Entity B already exists. I simply want to find a B record that matches (based on x and y) and insert it into the lookup field. There will only be one match, or no matches. Is there no way to do this with javascript?ok, what are you trying to accomplish
You have entity A which is created with fields x and y. Then for some reason you seem to want a lookup of entity B on the entity A form with the same field values?
is entity b newly created?
it sounds like you could have a workflow which gets triggered and creates entity b and assigns to the lookup
-
Friday, November 16, 2012 4:51 AM
hi CRMuser,
As per my understanding, you can achieve this using javascript.
Using soap you can retrieve the X and Y values of B entity. And iterate to check all the values with the X and Y values of A entity.
if both matches, then grab the Guid of that particular B entity record.
so, if u have guid of the B entity record, you can fill the A entity lookup field using java script.
Thanks and Regards,
------------------------------------------------
Mark as answer, if this reply helps you
-
Friday, November 16, 2012 9:27 AM
ahhh right I understand
Yes you can do this and I would say the best way to achieve this is to us OData
this will help you getting started
https://crmbusiness.wordpress.com/2012/04/30/crm-2011-getting-started-with-odata/
on this page it shows an example of selecting a user based on some details
https://crmbusiness.wordpress.com/2012/03/21/crm-2011-setting-a-user-lookup-with-the-logged-in-user-with-javascript/
function Getinfo() { var context; var serverUrl; var UserID; var ODataPath; context = Xrm.Page.context; serverUrl = context.getServerUrl(); UserID = context.getUserId(); ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; var retrieveUserReq = new XMLHttpRequest(); retrieveUserReq.open("GET", ODataPath + "/SystemUserSet(guid'" + UserID + "')", true); retrieveUserReq.setRequestHeader("Accept", "application/json"); retrieveUserReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); retrieveUserReq.onreadystatechange = function () { retrieveUserReqCallBack(this); }; retrieveUserReq.send(); } function retrieveUserReqCallBack(retrieveUserReq) { if (retrieveUserReq.readyState == 4 /* complete */) { if (retrieveUserReq.status == 200) { var retrievedUser = this.parent.JSON.parse(retrieveUserReq.responseText).d; if (retrievedUser.FullName != null) var setUservalue = new Array(); setUservalue[0] = new Object(); setUservalue[0].id = Xrm.Page.context.getUserId(); setUservalue[0].entityType = 'systemuser'; setUservalue[0].name = retrievedUser.FullName; Xrm.Page.getAttribute("meta_aurarrangedby").setValue(setUservalue) } else { } } }
I would also recommend using the odata query builder
http://dynamicsxrmtools.codeplex.com/
Ben Hosking
Check out my CRM Blog
Linked-In Profile
Follow Me on Twitter!- Proposed As Answer by Hoskinator Monday, November 19, 2012 11:53 PM
-
Monday, November 19, 2012 11:48 PMThanks, I will try this.