web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Auto populating the field

(0) ShareShare
ReportReport
Posted on by 2,665

Hi,

I am working on a script to auto populate a lookup field based on lookup field. It seems to be auto populating on form load but not setting up the value in the field. Because based on this lookup field other fields should get filtered which is not happening. So I doubt it is not setting the lookup field. Here is my code, can someone please let me know where I went wrong?

function Userupdate()
{
 var CurrentUserval=Xrm.Page.getAttribute("ownerid").getValue();
 if(CurrentUserval!=null)
 {
 var Usertextvalue=CurrentUserval[0].name;
 var Userlookupid=CurrentUserval[0].id;
 //alert(Userlookupid);

 var serverUrl = Xrm.Page.context.getClientUrl();
  //The XRM OData end-point
 var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
 var odataSetName = "SystemUserSet";
 var odataSelect = serverUrl + ODATA_ENDPOINT + "/" + odataSetName +"(guid'" + Userlookupid + "')";
 var retrieveReq=new XMLHttpRequest();
    retrieveReq.open("GET", odataSelect, false);
//alert("Processed");
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {
    if (retrieveReq.readyState === 4) {
            if (retrieveReq.status === 200) {
//debugger; 
 
 var retrieved = JSON.parse(retrieveReq.responseText).d;
 var user_county=retrieved.new_County;
 if(user_county.Name!=null)
 {
 var county_Title=new Array();
 var county_Titleitem=new Object();
 county_Titleitem.id=retrieved.new_County.id;
 county_Titleitem.typename="new_County";
 county_Titleitem.name=user_county.Name;
 county_Title[0]=county_Titleitem;
 Xrm.Page.getAttribute("new_county").setValue(county_Title);
 }
   }
  }
 };
retrieveReq.send();
}
else 
return;
}


Thanks

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Sayhaitokumar Profile Picture
    7,042 on at

    Hi Roxanna,

    If you want to filter one lookup based on another lookup please follow the below link

    vijaykumarbonam.blogspot.in/.../on-selecting-one-lookup-field-another.html

  • crmprogrammer2013 Profile Picture
    2,665 on at

    Hi Saravana,

    My requirement is not to filter one lookup based on another lookup. I am just trying to autopopulate a field based on logged in user. It is auto populating the lookup field on form load, but I think it is not setting up the value. I think so because, based on this auto populated field another lookup field gets filtered(which is not happening).

    Thanks

  • Verified answer
    Hosk Profile Picture
    on at

    two things which might be wrong

    first the values being returned from the retrieve usually multiple records, so to get the data you need to use a index.

    Also when setting the the optionset the entity type

    this forum discusses the same problem.

    social.microsoft.com/.../how-to-set-lookup-field-value-in-crm-2013-by-using-javascript

    the code from the forum shows you what values you need, ID, name, entitytype.  I think this is the same in CRM 2013

    var value = new Array();

    value[0] = new Object();

    value[0].id = retrievedUser.results[0].new_Project.Id;

    value[0].name = retrievedUser.results[0].new_Project.Name;

    value[0].entityType = retrievedUser.results[0].new_Project.LogicalName;

    Xrm.Page.getAttribute("new_projectname").setValue(value;)

  • crmprogrammer2013 Profile Picture
    2,665 on at

    Ben, Guido - It worked. Thank you.

    Guido- Made a little change though. For others who might be looking for the same..

    countyValue[0] = new Object();

    countyValue[0].id = county.Id;

    countyValue[0].name = county.LogicalName; //Here county.Name

    countyValue[0].entityType = county.Name;//county.LogicalName

    Thanks

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans