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)

Autopopulate fields based on lookup field selection

(0) ShareShare
ReportReport
Posted on by

i am trying to autopolulate fields on a form based on a contact selected as a lookup field... i have tried a code but doesn't seem to work properly... would anyone kindly help me with regards to this...

if (crmForm.all.exordia_firstname.DataValue == null)

{
var lookupfield = crmForm.all.cutomerid.DataValue;
if (lookupfield [0]! = null)
{
var lookupvalue = lookupfield [0]. firstname;
}
else
{
var lookupvalue = " ";
}
crmForm.all.exordia_firstname.DataValue = lookupvalue;
}

i have tried this code on the onchange event of the lookup field of customer in a case. and doesn't seem to work.

your help will be greatly appreciated. 

thank you in advance

 

*This post is locked for comments

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

     Hi.

     To select lookup entity primaty attribute value change your code to following:

     

    if (crmForm.all.exordia_firstname.DataValue == null)

    {
    var lookupfield = crmForm.all.cutomerid.DataValue;
    if (lookupfield != null && lookupfield [0] != null)
    {
    var lookupvalue = lookupfield [0].name;
    }
    else
    {
    var lookupvalue = " ";
    }
    crmForm.all.exordia_firstname.DataValue = lookupvalue;
    }

     

  • xtzee Profile Picture
    on at

    hi a33ik

    thank you for the quick response the code you gave me is working perfectly thank you...

    i would like to know how posible it is to retrieve data from the customer lookup field such as telephone numbers... if you can also do that for me... your help will be greatly appreciated... thank you once more!!

    Kind Regards

    xtzee

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

     Hi.

    You can use such script to retrieve identifier of parent record

    var id = lookupfield[0].id;

    Look this article to read how to retrieve data from webservice.

  • xtzee Profile Picture
    on at

    hi a33ik,

    i have been trying to autopopulate fields based on the lookup field selection. but i can't seem to get the id.

    let me explain to you:

    on my case entity there is a customer lookup, now when i select a customer that has just logged a case, i would like to be able to see the parent customer appear on the other field... (being autopopulated) on the form. please show me how to go about doing that...

     your help will be greatly appreciated.

    i am not good with coding, please comment the code out for me! the link you gave me on crmservice.retrieve method using Jscript looks quite nifty but i can't get it to work... tells me that the contact id doesn't exist. what might i be doing wrong here?

    Thanks :-)

     

  • xtzee Profile Picture
    on at

    Hi,

    its me again... i am now using webservices to retrieve the information, the only problem i have is that i can only retrieve nvarchar values but i can't seem to retrieve lookup values. if you don't might, please look into this issue for me. and get back to me thank you!!

    your help will be much appreciated..

     Here is the snippet of the code.

    // Prepare variables for a contact to retrieve.
    var contactid = "4696f8cb-9a1c-dd11-ad3a-0003ff9ee217";
    var authenticationHeader = GenerateAuthenticationHeader();
    
    // Prepare the SOAP message.
    var xml = "<?xml version='1.0' encoding='utf-8'?>"+ 
    "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"+
    " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
    " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+ 
    authenticationHeader+ 
    "<soap:Body>"+ 
    "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ 
    "<entityName>contact</entityName>"+ 
    "<id>"+contactid+"</id>"+ 
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+ 
    "<q1:Attributes>"+ 
    "<q1:Attribute>fullname</q1:Attribute>"+ 
    "</q1:Attributes>"+ 
    "</columnSet>"+ 
    "</Retrieve>"+ 
    "</soap:Body>"+ 
    "</soap:Envelope>";
    // Prepare the xmlHttpObject and send the request.
    var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
    xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
    xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xHReq.setRequestHeader("Content-Length", xml.length);
    xHReq.send(xml);
    // Capture the result.
    var resultXml = xHReq.responseXML;
    
    // Check for errors.
    var errorCount = resultXml.selectNodes('//error').length;
    if (errorCount != 0)
    {
     var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
     alert(msg);
    }
    // Display the retrieved value.
    else
    {
    alert(resultXml.selectSingleNode("//q1:fullname").nodeTypedValue);
    }

    Thanks :-)

  • AlexBailey Profile Picture
    5 on at

    Hi Xtzee,

     I've also been trying to do what you are doing.  I'm glad to hear that you code segment works.  Can you be more clear about the actual item names, customized entities used, and the types of new attributes used to make this autopopulate feature work. 

    I am working on a similar instance where I want to pull the phone number and email address for clients based upon a contact that is selected on a service form.  In this service form I've created TWO lookup attributes; one is the client name(new_nameofindividualid) and the other is a lookup (new_phonenumberid) that will be autopopulated with the client's phone number.  Could you provide an example code base that I could use to create this autopopulate function?  Any help would be apprecitated. 

  • Ashish Kapoor Profile Picture
    on at

    Hi xtzee,

    You can also use an alternate approach instead of the service call, provided that the data that you need to auto-populate is present on the Lookup view. In that case, you can directly retrieve the values off the lookup view at the time of selection of data. Note: This will however not work in case the lookup field is selected from the Form Assistant.

    You can refer to the below link for sample code on my blog:

    http://msdynamicscrmworld.blogspot.com/2009/07/crm-40-lookup-retrieve-column-values.html

    Regards,
    Ashish Kapoor

  • LE LOUARN Pierre-Yves Profile Picture
    70 on at

    Dear all,

    I have the same problem like xtzee, i'm using webservice to retrieve name, email of a contact through a onChange event... and currently it's working fine. The only problem is to retrieve phone number or fax witch is not a nvarchar.

    The alternate approach proposed by Ashish seems to be good but the fact that this script will work only if the Lookup View is loaded is a problem for my case.

    If anyone have an idea to retrieve phone number or other fields differents than nvarchar, your help will be very appreciated..

    Thanks,

     Pierre-Yves L.

  • Brian89IT Profile Picture
    35 on at

    I am trying to do this but failing.. How would I get address1_line1, address1_line2, etc.. from a ParentCustomerID field? I am only able to get address1_name, not any of the other address values. How would I get all the values?

    Here's the code I have so far, it works and lets me get the address1_name, but I cannot do lookupItems[10] or above, and I believe thats where the fields are. I can't believe such a simple lookup is so hard! I've searched everywhere for solutions, but no one knows how to do this in Jscript.

    ===========

    if(crmForm.all.parentcustomerid.DataValue != null && crmForm.all.address1_name.Value == null)

    {
    var parentCustomerID = document.getElementById( "parentcustomerid" );
    var lookupItems = parentCustomerID.items[0].values;
    crmForm.all.address1_name.value = lookupItems[8].value;
    }

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