locked
Need to auto populate fields on a form based on the value selected in the same form's lookup field RRS feed

  • Question

  • Hi,

    I have an customizable entity form. It has one lookup field and five other text fields. The lookup field pulls out values from its parent entity. So when I press the lookup button on my form, the parent entity form appears, and I choose a value for that field(say Organisation name). this value gets populated in my child entity's lookup field. So, now my requirement is, when this lookup field value gets populated from the parent entity, the other five fields in child entity form also have to get auto populated with the corresponding values from parent entity.Please help. 
    Wednesday, January 20, 2010 1:05 PM

Answers

  • Then your code should be like this as you are dealing with lookup:

    if (resultXml.selectSingleNode('//q1:primarycontactid') != null) {
       var lookup = [];
       var lookupValue = {};
       dataValue.id = resultXml.selectSingleNode('//q1:primarycontactid').nodeTypedValue;
       dataValue.typename = "contact";
       dataValue.name = resultXml.selectSingleNode('//q1:primarycontactid').getAttribute("name");
       lookup[0] = lookupValue;
    
       crmForm.all.new_contractorid.DataValue = lookup;
    
    }


    Daniel Cai | http://danielcai.blogspot.com

     

    Monday, March 22, 2010 5:30 PM

All replies

  • Hi
    catch the onChange() event of the lookup field and add your javasscript code in this event to populate the other fields
    e.g in OnChange(),

    crmForm.all.someField.DataValue = "Some Text Here";

    to enable onChange() go to settings=> customization ==> open entity ==> open form ==> select lookup field ==> click properties ==> enable onchange() and write the javascript there.

    i hope it helps.
    Wednesday, January 20, 2010 1:15 PM
  • Hi,

    You can use following code to fetch data based on Lookup ID,
    Just you have to change name of the Entity,lookup and the attributes name that you want to fetch from parent entity, you can paste this method on change of lookup.

    GetValues();
    Function GetValues()
     {
                if(crmForm.all.<LOOKFieldName>.DataValue!=null)
                {
                FetchID =crmForm.all.<LOOKFieldName>.DataValue[0].id ;
                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>" + EntityName + "</entityName>" +
                                "<id>" + FetchID + "</id>" +
                                "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
                                "<q1:Attributes>";
                //configure attribute list You can add attribute list
                      xml = xml + '<q1:Attribute>' +FirstAttribute + '</q1:Attribute>';
                      xml = xml + '<q1:Attribute>' +SecondAttribute + '</q1:Attribute>';
                      xml = xml + '<q1:Attribute>' +ThirdAttribute + '</q1:Attribute>';
                
                xml = xml + '</q1:Attributes></columnSet>';
                xml = xml + "</Retrieve></soap:Body></soap:Envelope>";
                //call function to create Soap Request to ms crm webservice
                 xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
                xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
                xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
                xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
                xmlHttpRequest.send(xml);
                resultXml = xmlHttpRequest.responseXML;
                //Check for error
                errorCount = resultXml.selectNodes('//error').length;
                        if (errorCount != 0) { //log error
                            
                            msg = resultXml.selectSingleNode('//description').nodeTypedValue;
                           alert(msg);
                        }
                        else 
                        {
                           if (resultXml.selectSingleNode('//q1:FirstAttribute') != null) {
                                crmForm.all.FirstAttribute.DataValue = resultXml.selectSingleNode('//q1:FirstAttribute').attributes[0].nodeTypedValue;
                            }
                            if (resultXml.selectSingleNode('//q1:SecondAttribute') != null) {
                                crmForm.all.SecondAttribute.DataValue = resultXml.selectSingleNode('//q1:SecondAttribute').attributes[0].nodeTypedValue;
                            }
                            if (resultXml.selectSingleNode('//q1:ThirdAttribute') != null) {
                                crmForm.all.ThirdAttribute.DataValue = resultXml.selectSingleNode('//q1:ThirdAttribute').attributes[0].nodeTypedValue;
                            }
                          }
    
                }
         }
    Let me know if you are facing any issue
    Mahain
    • Proposed as answer by HIMBAPModerator Wednesday, January 20, 2010 1:34 PM
    Wednesday, January 20, 2010 1:33 PM
    Moderator
  • Thanks Muhammad. I have done that part. I'm stuck up with the js code.

    Hi Mahender,

    Thanks for your reply. But I'm facing an issue. I'm very new to java scripts. I pasted the following code in the Onchange event of my lookup entity. I'm able to fetch the lookup value from the parent entity and its getting displayed in my lookup field. But the other text fields are not getting auto populated. I tried auto population for only one street field that you can see in the code. Also, I get a "Error on page" at the bottom left corner of the screen.


    if(crmForm.all.lookupentity.DataValue != null)
    {
    FetchID = crmForm.all.lookupentity.DataValue[0].id ;
    var authenticationHeader = GenerateAuthenticationHeader();

    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>" +<strong>masterdataentity</strong> +
    "</entityName>" +"<id>" + FetchID + "</id>" +
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" + "<q1:Attributes>";
    xml = xml + '<q1:Attribute>' +<strong>street</strong> + '</q1:Attribute>';
    xml = xml + '</q1:Attributes></columnSet>';
    xml = xml + "</Retrieve></soap:Body></soap:Envelope>";
    xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);
    resultXml = xmlHttpRequest.responseXML;

    if(resultXml.selectSingleNode('//q1:street) != null)
    {
    crmForm.all.street.DataValue = resultXml.selectSingleNode('//q1:street).attributes[0].nodeTypedValue;
    }
    }

    The ones that are bold are the changes that I made as you said. Is there any data type that has to be defined for the attribute?

    Thursday, January 21, 2010 10:09 AM
  • Hi,

    Sorry there was some mistake, i have changed it
    First make sure your field and entity name are correct to get proper result


    if(crmForm.all.lookupentity.DataValue != null)
    {
    var FetchID = crmForm.all.lookupentity.DataValue[0].id ;
    var authenticationHeader = GenerateAuthenticationHeader();
    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>masterdataentity</entityName>" +
        "<id>" + FetchID + "</id>" +
        "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
        "<q1:Attributes>";
    xml = xml + "<q1:Attribute>street</q1:Attribute>";
    xml = xml + "</q1:Attributes></columnSet>";
    xml = xml + "</Retrieve></soap:Body></soap:Envelope>";
    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);
    var resultXml = xmlHttpRequest.responseXML;
    
        if(resultXml.selectSingleNode('//q1:street') != null) 
        {
        crmForm.all.street.DataValue =resultXml.selectSingleNode('//q1:street').nodeTypedValue
        }
    }
    
    

    I have tested it my end, I am getting result after changing entityname and attribute name.
    Let me knw if you still facing issue.



    Mahain
    • Proposed as answer by LFechtman Tuesday, May 8, 2012 9:03 PM
    Thursday, January 21, 2010 11:48 AM
    Moderator
  • Thanks for this code. It works great!  One question:

     

    What if the field I want to retrieve from the parent object is a lookup field itself? So let's say I am retrieving account data and I want to retrieve the primary contact id field and place it in a lookup field on the child form?  I tried this:

    crmForm.all.new_contractorid.DataValue[0].id =resultXml.selectSingleNode('//q1:primarycontactid').nodeTypedValue

    and this

    crmForm.all.new_contractorid.DataValue =resultXml.selectSingleNode('//q1:primarycontactid').nodeTypedValue

    but neither worked (nor did I really expect them to as I was just guessing).

    Anyone know how to do this?

    Thanks in advance!

    Brian

    Monday, March 22, 2010 4:56 PM
  • Then your code should be like this as you are dealing with lookup:

    if (resultXml.selectSingleNode('//q1:primarycontactid') != null) {
       var lookup = [];
       var lookupValue = {};
       dataValue.id = resultXml.selectSingleNode('//q1:primarycontactid').nodeTypedValue;
       dataValue.typename = "contact";
       dataValue.name = resultXml.selectSingleNode('//q1:primarycontactid').getAttribute("name");
       lookup[0] = lookupValue;
    
       crmForm.all.new_contractorid.DataValue = lookup;
    
    }


    Daniel Cai | http://danielcai.blogspot.com

     

    Monday, March 22, 2010 5:30 PM
  • Hello Mahender,

    I use your code you demonstrated above but I still cant get to work.

    I currently have a order product form where you choose a product from a lookupview. I added two fields on the order product form call length and width which are on the product form. I want to be able to pick a product on the order product form and populate lenth on the order product form when choosing a product.

    this is the code i tried to modify:

    if (crmForm.all.productid.DataValue != null) {

        var FetchID = crmForm.all.productid.DataValue[0].id;

        var authenticationHeader = GenerateAuthenticationHeader();

        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>product</entityName>" +
        "<id>" + FetchID + "</id>" +
        "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
        "<q1:Attributes>";

        xml = xml + "<q1:Attribute>orbus_length</q1:Attribute>";

        xml = xml + "</q1:Attributes></columnSet>";

        xml = xml + "</Retrieve></soap:Body></soap:Envelope>";

        var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

        xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");

        xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

        xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

        xmlHttpRequest.send(xml);

        var resultXml = xmlHttpRequest.responseXML;


        if (resultXml.selectSingleNode('//q1:orbus_length') != null) {

            crmForm.all.orbus_length.DataValue = resultXml.selectSingleNode('//q1:orbus_length').nodeTypedValue

        }

    }


    Jose Fleitas
    Monday, October 24, 2011 2:14 PM
  • Hello Mahender,

    Solution to above thread was that I had to parse resultXML to float because field orbus_lenth is numeric datatype and doesnt except strings.

     

    Thanks,

    Jose Fleitas


    Jose Fleitas
    Tuesday, October 25, 2011 4:43 PM
  • Hi

    I need to do something very similar.

    On the quote product page I have added a field called vendor part number. When i select existing product and pick a product via a lookup i want to populate the vendor part number field based on product selected.

    What bits do i need to change in the above code from JoseFleitas?

    Thanks for any help.

    • Proposed as answer by JoseFleitas Friday, October 28, 2011 2:49 PM
    • Unproposed as answer by JoseFleitas Friday, October 28, 2011 2:49 PM
    Friday, October 28, 2011 1:58 PM
  • Hello,

    You can try this from Mahender example:

    if (crmForm.all.productid.DataValue != null) { var FetchID = crmForm.all.productid.DataValue[0].id; var authenticationHeader = GenerateAuthenticationHeader(); 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>product</entityName>" + "<id>" + FetchID + "</id>" + "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" + "<q1:Attributes>"; xml = xml + "<q1:Attribute>vendorpartnumber</q1:Attribute>"; xml = xml + "</q1:Attributes></columnSet>"; xml = xml + "</Retrieve></soap:Body></soap:Envelope>"; var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve"); xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xmlHttpRequest.setRequestHeader("Content-Length", xml.length); xmlHttpRequest.send(xml); var resultXml = xmlHttpRequest.responseXML; if (resultXml.selectSingleNode('//q1:vendorpartnumber') != null) { crmForm.all.vendorpartnumber.DataValue = resultXml.selectSingleNode('//q1:vendorpartnumber').nodeTypedValue } }

    You have to update this code with your vendorpartnumber field name (where you see I entered vedorpartnumber). Entity stays the same and the product Lookup name stays the same but if you have another name added on top where instead of productid.


    Hope this helps

    Jose Fleitas

    

    Jose Fleitas

    • Proposed as answer by JoseFleitas Wednesday, May 9, 2012 4:16 PM
    Wednesday, May 9, 2012 1:32 PM
  • Hello rstalbow,

    Here is an solution that might work also found on this site which is posted by AShish Kapoor: Let me know if it works: 

    

    Thanks,

    Jose Fleitas

    

    Jose Fleitas


    • Edited by JoseFleitas Wednesday, May 9, 2012 4:22 PM
    • Proposed as answer by JoseFleitas Wednesday, May 9, 2012 4:22 PM
    Wednesday, May 9, 2012 4:21 PM
  • Hi,

    I am trying to do something similar. 

    In the Evaluation (entity) form under Service, when creating a new evaluation. Depending upon the contact selected from the lookup field, need to auto-populate some fields like enduser street address one, enduser email, enduser city, enduser state/province, enduser country. 

    any help on how to modify the above code? Will the javascript differ for Dynamics CRM 2013?

    Thanks in advance!!!

    Tuesday, April 29, 2014 7:51 PM