locked
Auto-Resolution on Lookup using Javascript RRS feed

  • Question

  • Hi,

      In CRM 4.0 is there any way using the auto resolution property of lookup with javascript. Imagine that on contact form when I fill a text area with an accountnumber for an accout, the regardingaccount lookup on contact form will automaticaly fills on the onchange event of text area. Is this possible using javascirpt and how ?

    Thanks in advance,
    Goktug
    Tuesday, January 19, 2010 10:31 PM

Answers

  • Hi,

    I use the code similar to this:

    var field0 = crmForm.all.new_accountnumber;
    var field1 = crmForm.all.parentcusomerid;
    
    if (field0.DataValue != null)
    {
        var temp= field0.DataValue;
        var resultXml = RetrieveAttributeValues(temp);
        ProcessResult(resultXml);
    }
    
    
    //*** Functions ***
    
    function ProcessResult(resultXml) {
    
    // Check for errors.
    var errorCount = resultXml.selectNodes('//error').length;
    if (errorCount != 0) 
    {
        var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
        alert(msg);
    }
    else 
    {
    // Capture the result and UnEncode it.
    var resultSet = new String();
    resultSet = resultXml.text;
    resultSet.replace('&lt;','<');
    resultSet.replace('&gt;','>');
    
    // Create an XML document that you can parse.
       var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
       oXmlDoc.async = false; 
    // Load the XML document that has the UnEncoded results.
       oXmlDoc.loadXML(resultSet);
    // Display the results.
       var results = oXmlDoc.getElementsByTagName('result');
    
      if (results.length == 0)
      {
       alert("Error ...");
       return;
      }
      else
       {
         var id= results[0].selectSingleNode('./accountid);
         if (id != null)
         {
             var lookupData = new Array();
             var lookupItem= new Object();
             lookupItem.id = id.nodeTypedValue         
             lookupItem.typename = "account";
             lookupItem.name = id.attributes[0].nodeTypedValue;
             lookupData[0] = lookupItem;
             field1.DataValue = lookupData ;
         }
        }
     }
    }
    
    function RetrieveAttributeValues(sValue) 
    {
    
    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>" +
    "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ 
    "<fetchXml>"+
    "&lt;fetch mapping='logical'&gt;"+
    "&lt;entity name='account'&gt;"+
    "&lt;filter type='and'&gt;"+
    "&lt;condition attribute='accountnumber' operator='eq' value='"+sValue+"'/&gt;"+
    "&lt;/filter&gt;"+
    "&lt;/entity&gt;"+
    "&lt;/fetch&gt;"+
    "</fetchXml>"+ 
    "</Fetch>"+ 
    "</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/Fetch");
    xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xHReq.setRequestHeader("Content-Length", xml.length);
    xHReq.send(xml);
    
    return xHReq.responseXML;
    
    }
    
    Wednesday, January 20, 2010 12:57 AM

All replies

  • Hi Gotkug,
    Of course you can. You can call the web service with JavaScript, pass the account number as the clause, and get all the results back to fill in the account lookup with the data values provided. There is an awesome series of articles explaining it in detail over on Customer Effective Blog:
    1. Calling CRM Web Service With JavaScript
    2. Client-side Web Service Calls for Microsoft Dynamics CRM 4.0: Calling the Metadata Service with Javascript ; and
    3. Client-side Web Service Calls for Microsoft Dynamics CRM 4.0: Calling the CRM Service with Javascript
    Tuesday, January 19, 2010 10:43 PM
  • Ranko tahnx for reply, but I need to do this without calling a web service, I just want to give accountnumber to lookup and the lookup will find the right account in javascript, is this clear?. In your method I have to retrieve value,name and type of lookup to set it but I just want to  give accountnumber(which is a search parameter for account) to lookup and rest lookup will find the account.
    Tuesday, January 19, 2010 10:53 PM
  • I am 99% sure that you can write a fetchXml that passes account number, and then use the results to resolve it. Is that enough to get you started? If not give me a reply and I'll digg around through my old JavaScripts to see if I can find an example.
    Tuesday, January 19, 2010 11:00 PM
  • Hi,

    I use the code similar to this:

    var field0 = crmForm.all.new_accountnumber;
    var field1 = crmForm.all.parentcusomerid;
    
    if (field0.DataValue != null)
    {
        var temp= field0.DataValue;
        var resultXml = RetrieveAttributeValues(temp);
        ProcessResult(resultXml);
    }
    
    
    //*** Functions ***
    
    function ProcessResult(resultXml) {
    
    // Check for errors.
    var errorCount = resultXml.selectNodes('//error').length;
    if (errorCount != 0) 
    {
        var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
        alert(msg);
    }
    else 
    {
    // Capture the result and UnEncode it.
    var resultSet = new String();
    resultSet = resultXml.text;
    resultSet.replace('&lt;','<');
    resultSet.replace('&gt;','>');
    
    // Create an XML document that you can parse.
       var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
       oXmlDoc.async = false; 
    // Load the XML document that has the UnEncoded results.
       oXmlDoc.loadXML(resultSet);
    // Display the results.
       var results = oXmlDoc.getElementsByTagName('result');
    
      if (results.length == 0)
      {
       alert("Error ...");
       return;
      }
      else
       {
         var id= results[0].selectSingleNode('./accountid);
         if (id != null)
         {
             var lookupData = new Array();
             var lookupItem= new Object();
             lookupItem.id = id.nodeTypedValue         
             lookupItem.typename = "account";
             lookupItem.name = id.attributes[0].nodeTypedValue;
             lookupData[0] = lookupItem;
             field1.DataValue = lookupData ;
         }
        }
     }
    }
    
    function RetrieveAttributeValues(sValue) 
    {
    
    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>" +
    "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ 
    "<fetchXml>"+
    "&lt;fetch mapping='logical'&gt;"+
    "&lt;entity name='account'&gt;"+
    "&lt;filter type='and'&gt;"+
    "&lt;condition attribute='accountnumber' operator='eq' value='"+sValue+"'/&gt;"+
    "&lt;/filter&gt;"+
    "&lt;/entity&gt;"+
    "&lt;/fetch&gt;"+
    "</fetchXml>"+ 
    "</Fetch>"+ 
    "</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/Fetch");
    xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xHReq.setRequestHeader("Content-Length", xml.length);
    xHReq.send(xml);
    
    return xHReq.responseXML;
    
    }
    
    Wednesday, January 20, 2010 12:57 AM
  • It needs to lookup this lookup somewhere, it can't pluck it from thin air. So it is easiest to make a call to the database, and one way to do that is via the webservice. The code is a bt clunky but it works (I've done several auto-fill fields similar to what you are asking for).
    In your case the lookup_type is fixed of course since you always want to lookup an account, you need to tell it which field you are searching (account number) and the value to match (from the user input).

    Another way to do this which is not quite so automated but is far more elegant from a javascript perspective is to pass the account number as a parameter to the lookup window, so the user types an account number, clicks on the usual spyglass for the regardingaccount lookup and the search only shows accounts with a matching number (hopefully only one, and hopefully something there unless there was a typing error).
    More on this method discussed in this newsgroup thread:
    http://groups.google.com/group/microsoft.public.crm/browse_thread/thread/3ae9836b5f2c4618/c0b0e1df6e1c7f80?lnk=raot
    Saturday, January 23, 2010 1:40 PM
  • Hi,

    In case you are still looking for a solution, pls have a look at this post

     

    Monday, April 11, 2011 7:39 AM