locked
onChange event populating another field RRS feed

  • Question

  • I have a Customer Lookup field on a form, which the user can select the customer.
    When the user selects a customer, I want another field automatically populated with the EmployeeNumber of the customer.
    Would anyone be able to help me out?

    I've tried following this: http://jianwang.blogspot.com/2008/12/crm-40-get-attribute-value-from-entitys.html but got lost halfway, specifically how do I reference the current field, the EmployeeNumber field, and also insert a Web Service or SQL command inside to get the required value.
    Thanks in advance.

    Friday, June 12, 2009 3:08 AM

Answers

  • The easiest way, albeit asynchronous would be with workflow. Failing this, I did once write a jscript to retrieve an attribute from a product record (new_supportvalue) when the product was looked up on a contract line form and put that attribute value into a field on the contract line form. It sounds pretty similar. The code is not the most elegant but you may be able to adapt it.

    var lookupItem = new Array;
    
    // Get the lookup for the productid attribute on the contract line form.
    lookupItem = crmForm.all.productid.DataValue;
    
    
    
    //if (lookupItem[0] != null)
    if (lookupItem != null)
    {
        // Display the GUID of the lookup.
    //    alert(lookupItem[0].id);
    var productid = lookupItem[0].id;
    
    // Prepare variables for a product to retrieve.
    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>product</entityName>"+ 
    "<id>"+productid+"</id>"+ 
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+ 
    "<q1:Attributes>"+ 
    "<q1:Attribute>new_supportvalue</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
    {
    
    if (resultXml.selectSingleNode("//q1:new_supportvalue") != null)
    {
    
    var supportvaluenode = resultXml.selectSingleNode("//q1:new_supportvalue");
    
    var parsedsv = parseFloat(supportvaluenode.nodeTypedValue);
    
    crmForm.all.price.DataValue = parsedsv;
    
    }
    
    else
    {
    crmForm.all.price.DataValue = null;
    
    }
    
    }
    
    
    }
    else
    {
    crmForm.all.price.DataValue = null;
    
    }
    
    This was largely cobbled together using the SDK. The only complication I can foresee is that you're using the customer lookup, which can reference a contact OR an account, otherwise I think it may work.

    Leon Tribe
    Want to hear me talk about all things CRM? Check out my blog

    Want to hear me talk about all things CRM? Check out my blog http://leontribe.blogspot.com/
    • Proposed as answer by Leon TribeMVP Friday, June 12, 2009 3:30 AM
    • Marked as answer by ABR1 Friday, June 12, 2009 5:06 AM
    Friday, June 12, 2009 3:29 AM

All replies

  • The easiest way, albeit asynchronous would be with workflow. Failing this, I did once write a jscript to retrieve an attribute from a product record (new_supportvalue) when the product was looked up on a contract line form and put that attribute value into a field on the contract line form. It sounds pretty similar. The code is not the most elegant but you may be able to adapt it.

    var lookupItem = new Array;
    
    // Get the lookup for the productid attribute on the contract line form.
    lookupItem = crmForm.all.productid.DataValue;
    
    
    
    //if (lookupItem[0] != null)
    if (lookupItem != null)
    {
        // Display the GUID of the lookup.
    //    alert(lookupItem[0].id);
    var productid = lookupItem[0].id;
    
    // Prepare variables for a product to retrieve.
    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>product</entityName>"+ 
    "<id>"+productid+"</id>"+ 
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+ 
    "<q1:Attributes>"+ 
    "<q1:Attribute>new_supportvalue</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
    {
    
    if (resultXml.selectSingleNode("//q1:new_supportvalue") != null)
    {
    
    var supportvaluenode = resultXml.selectSingleNode("//q1:new_supportvalue");
    
    var parsedsv = parseFloat(supportvaluenode.nodeTypedValue);
    
    crmForm.all.price.DataValue = parsedsv;
    
    }
    
    else
    {
    crmForm.all.price.DataValue = null;
    
    }
    
    }
    
    
    }
    else
    {
    crmForm.all.price.DataValue = null;
    
    }
    
    This was largely cobbled together using the SDK. The only complication I can foresee is that you're using the customer lookup, which can reference a contact OR an account, otherwise I think it may work.

    Leon Tribe
    Want to hear me talk about all things CRM? Check out my blog

    Want to hear me talk about all things CRM? Check out my blog http://leontribe.blogspot.com/
    • Proposed as answer by Leon TribeMVP Friday, June 12, 2009 3:30 AM
    • Marked as answer by ABR1 Friday, June 12, 2009 5:06 AM
    Friday, June 12, 2009 3:29 AM
  • Thanks Leon, I'll give it a go.
    I'll go with whichever is simpler, but with workflow it will only auto-populate when I save the form or close it, correct?
    Friday, June 12, 2009 3:42 AM
  • No, workflow will trigger when the field changes i.e. you perform the lookup. However, there will be no evidence that the additional information will have hit the form unless the user closes and reopens the form (assuming the workflow has completed in that time). Jscript is instantaneous and will show the field on the form immediately to the user but requires code similar to that above.

    Leon Tribe
    Want to hear me talk about all things CRM? Check out my blog

    Want to hear me talk about all things CRM? Check out my blog http://leontribe.blogspot.com/
    Friday, June 12, 2009 4:12 AM
  • Thanks so much Leon.
    I got exactly what I wanted now :)  Your code works beautifully (once I understood it).

    var lookupItem = new Array;
    
    // Get the lookup for the productid attribute on the contract line form.
    lookupItem = crmForm.all.customerid.DataValue;
    
    //if (lookupItem[0] != null)
    if (lookupItem != null)
    {
        // Display the GUID of the lookup.
        //alert(lookupItem[0].id);
    var contactid= lookupItem[0].id;
    
    // Prepare variables for a product to retrieve.
    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>employeeid</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
    {
       
         if (resultXml.selectSingleNode("//q1:employeeid") != null)
         {
       
                 var supportvaluenode = resultXml.selectSingleNode( "//q1:employeeid");
                 var parsedsv = supportvaluenode.nodeTypedValue;
                 crmForm.all.new_requestinitiatoremployeenumber.DataValue = parsedsv;
          }
          else
          { 
                  crmForm.all.new_requestinitiatoremployeenumber.DataValue = null;
           }
    }
    
    
    }
    else
    {
    crmForm.all.new_requestinitiatoremployeenumber.DataValue = null;
    
    }

    Friday, June 12, 2009 5:06 AM
  • A lot of the nested ifs are for exception capture e.g. if they go back to the lookup and clear the entry or if the parent record has a blank entry in the field being retrieved. Glad it worked ;)

    Leon Tribe
    Want to hear me talk about all things CRM? Check out my blog

    Want to hear me talk about all things CRM? Check out my blog http://leontribe.blogspot.com/
    Friday, June 12, 2009 6:55 AM