Answered by:
Auto Populate associated Lookup value on selection of other lookup in MSCRM 2011

Question
-
Hi All,
In Account entity
I have 2 lookup attributes
1.Product and
2.StateWhile selecting the Product lookup
Product lookup view is openingProduct is having a relationship with Product- State entity also
Product associated state I need auto populate on the Account entity State lookupProduct-State Entity State and produts are lookup attributes .
Relationships:Account to Product Entity
Many to One (N:1) on AccountProduct to Product-State entity
Many to One (N:1) on Product entity.Please let me know how to work on this.
Thanks in Advance.
Thanks,
- Edited by Rahul-22 Saturday, March 1, 2014 3:05 AM
Thursday, February 27, 2014 9:19 AM
Answers
-
Hi Rahul,
I hope I understand your requirements correctly. So, basically; the State Entity contains the mapping of Products to Cities.
In summary:
- Account entity have a lookup to the Product entity
- Product have a 1 to many relationship with the State entity
- State entity have a lookup to the Product and City entity
If this is correct, then the following JS should do the job
function product_onchange() { var product = Xrm.Page.getAttribute("new_productid").getValue(); if (product == null) { return; } var serverUrl = Xrm.Page.context.getClientUrl(); var query = serverUrl + "/xrmservices/2011/OrganizationData.svc/new_stateSet?$select=new_cityid&$filter=new_productid eq guid'" + product[0].id + "'"; var retrieveReq = new XMLHttpRequest(); retrieveReq.open("GET", query, false); retrieveReq.setRequestHeader("Accept", "application/json"); retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8"); retrieveReq.onreadystatechange = function () { getproductstate(this); }; retrieveReq.send(); } function getproductstate(retrieveReq) { if (retrieveReq.readyState == 4) { if (retrieveReq.status == 200) { var retrieved = JSON.parse(retrieveReq.responseText).d; Xrm.Page.getAttribute("new_cityid").setValue([{id: retrieved.results[0].OwnerId.Id, name: retrieved.results[0].OwnerId.Name, entityType: retrieved.results[0].OwnerId.LogicalName}]); } } }
BTW: you said "while opening the Product Record left navigation I had a Sate entity."; Does this mean a Product can have more than one state? If this is true, then a product can have more than one city. The above code will bring back the first city but you can adjust the query to sort or filter based on other conditions. Hope this helps and good luck.
Eric UNG [Senior Analyst Programmer :: Sydney, Australia]
Saturday, March 1, 2014 3:35 AM
All replies
-
Hi Toushu,
Here is some tested JS code (using oData) that might assist you. Hook the product_onchange() function to the onchange event of your product lookup field. (Make sure you change the entityname/attributenames highlighted in BOLD to match your system). Hope this helps.
function product_onchange() { var product = Xrm.Page.getAttribute("new_productid").getValue(); if (product == null) { return; } var serverUrl = Xrm.Page.context.getClientUrl(); var query = serverUrl + "/xrmservices/2011/OrganizationData.svc/new_productSet?$select=new_productstateid&$filter=new_productid eq guid'" + product[0].id + "'"; var retrieveReq = new XMLHttpRequest(); retrieveReq.open("GET", query, false); retrieveReq.setRequestHeader("Accept", "application/json"); retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8"); retrieveReq.onreadystatechange = function () { getproductstate(this); }; retrieveReq.send(); } function getproductstate(retrieveReq) { if (retrieveReq.readyState == 4) { if (retrieveReq.status == 200) { var retrieved = JSON.parse(retrieveReq.responseText).d; Xrm.Page.getAttribute("new_productstateid").setValue([{id: retrieved.results[0].OwnerId.Id, name: retrieved.results[0].OwnerId.Name, entityType: retrieved.results[0].OwnerId.LogicalName}]); } } }
Eric UNG [Senior Analyst Programmer :: Sydney, Australia]
Thursday, February 27, 2014 10:23 AM -
Hi Eric,
Thanks for your response,
But I'm unable to complete my requirement by using above code.
because my requirement slightly changed I apologies for thisIn account entity I'm selecting the Product lookup.
Product record value is populating on the Product lookup
while opening the Product Record left navigation I had a Sate entity.In state entity product and City are lookup values City lookup is pointing to City entity
Product is pointing to Product entity .Now I want to auto populate on change of the Product lookup in the Account entity product associated city in the City lookup
City lookup attribute is placed on the Account entity.Can you please suggest me how will I achieve this.
Account entity:
Product = new_Product
City = new_cityCity Entity:
Product = new_Productid
City = new_CityidThanks,
- Edited by Rahul-22 Friday, February 28, 2014 7:28 PM
Friday, February 28, 2014 7:20 PM -
Hi Rahul,
I hope I understand your requirements correctly. So, basically; the State Entity contains the mapping of Products to Cities.
In summary:
- Account entity have a lookup to the Product entity
- Product have a 1 to many relationship with the State entity
- State entity have a lookup to the Product and City entity
If this is correct, then the following JS should do the job
function product_onchange() { var product = Xrm.Page.getAttribute("new_productid").getValue(); if (product == null) { return; } var serverUrl = Xrm.Page.context.getClientUrl(); var query = serverUrl + "/xrmservices/2011/OrganizationData.svc/new_stateSet?$select=new_cityid&$filter=new_productid eq guid'" + product[0].id + "'"; var retrieveReq = new XMLHttpRequest(); retrieveReq.open("GET", query, false); retrieveReq.setRequestHeader("Accept", "application/json"); retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8"); retrieveReq.onreadystatechange = function () { getproductstate(this); }; retrieveReq.send(); } function getproductstate(retrieveReq) { if (retrieveReq.readyState == 4) { if (retrieveReq.status == 200) { var retrieved = JSON.parse(retrieveReq.responseText).d; Xrm.Page.getAttribute("new_cityid").setValue([{id: retrieved.results[0].OwnerId.Id, name: retrieved.results[0].OwnerId.Name, entityType: retrieved.results[0].OwnerId.LogicalName}]); } } }
BTW: you said "while opening the Product Record left navigation I had a Sate entity."; Does this mean a Product can have more than one state? If this is true, then a product can have more than one city. The above code will bring back the first city but you can adjust the query to sort or filter based on other conditions. Hope this helps and good luck.
Eric UNG [Senior Analyst Programmer :: Sydney, Australia]
Saturday, March 1, 2014 3:35 AM -
Eric,
Thanks for your response,
Function Product_Onchange is working fine but when it comes to the getproductstate function.
In retrieved.text I'm getting below error.
{
"error": {
"code": "", "message": {
"lang": "en-US", "value": "Operator 'eq' incompatible with operand types 'Microsoft.Crm.Metadata.ComplexTypeInstance`1[[Microsoft.Xrm.Sdk.EntityReference, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]' and 'System.Guid' at position 11."
}
}
}Thanks,
Sunday, March 2, 2014 5:30 PM