Microsoft CRM 4.0 Customize CRMService.Fetch Method Using JScript - paymentermscode not working

الإجابة Microsoft CRM 4.0 Customize CRMService.Fetch Method Using JScript - paymentermscode not working

  • Friday, August 17, 2012 2:30 PM
     
     

    I found the below code which works well as is when pasted into the onChange event of the Potential Customer field of the Quote form.  When I substitute paymenttermscode for [name] in the beginning in var secondColumn and at the end where var name is defined I get an object required error.  But if I substitute ownerid in the same places it runs fine and displays the ownerid.  I am confused as to why this is so.  I need to be able to access the paymenttermscode.

    http://msdn.microsoft.com/en-us/library/cc677073

    // Prepare variables to fetch accounts.
    var fetchMapping = "logical";
    var entityName = "account";
    var firstColumn = "accountid";
    var secondColumn = "name";
    var linkEntity = "systemuser";
    var linkEntityTo ="owninguser";
    var filterType = "and";
    var conditionAttribute = "lastname";
    var operator = "ne";
    var value = "Cannon";
    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='"+fetchMapping+"'&gt;"+
    "&lt;entity name='"+entityName+"'&gt;"+
    "&lt;attribute name='"+firstColumn+"'/&gt;"+
    "&lt;attribute name='"+secondColumn+"'/&gt;"+
    "&lt;link-entity name='"+linkEntity+"' to='"+linkEntityTo+"'&gt;"+
    "&lt;filter type='"+filterType+"'&gt;"+
    "&lt;condition attribute='"+conditionAttribute+"'"+
    " operator='"+operator+"' value='"+value+"'/&gt;"+
    "&lt;/filter&gt;"+
    "&lt;/link-entity&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);
    // 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);
    }
    // Process and display the results.
    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');
          var msg = "\Account Id\t\t\t\tAccount Name\r";
    msg +="--------------------------------------------------------------------------------\r";
        for (i=0;i < results.length;i++)
        {
         var idValue = results[i].selectSingleNode('./accountid').nodeTypedValue;
         var name = results[i].selectSingleNode('./name').nodeTypedValue;
         msg += idValue +"\t"+ name+"\r";
        }
        alert(msg);
    }


    GM

All Replies

  • Wednesday, August 22, 2012 7:49 PM
     
     Answered

    The following code produces the customer Name and Payment Terms Code Name for the onChange event for Potential Customer, customerid, in the Quote form.  It probably isn't the best way to do this since I basically don't know what I am doing, but it works.

    // Prepare variables to fetch accounts.
    var fetchMapping = "logical";
    var entityName = "account";
    var firstColumn = "name";
    var secondColumn = "paymenttermscode";
    var linkEntity = "account";
    var linkEntityTo ="accountid";
    var filterType = "and";
    var conditionAttribute = "name";
    var operator = "like";
    var lookup = new Array();
    lookup = crmForm.all.customerid.DataValue;
    var value = lookup[0].name;
    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='"+fetchMapping+"'&gt;"+
    "&lt;entity name='"+entityName+"'&gt;"+
    "&lt;attribute name='"+firstColumn+"'/&gt;"+
    "&lt;attribute name='"+secondColumn+"'/&gt;"+
    "&lt;link-entity name='"+linkEntity+"' to='"+linkEntityTo+"'&gt;"+
    "&lt;filter type='"+filterType+"'&gt;"+
    "&lt;condition attribute='"+conditionAttribute+"'"+
    " operator='"+operator+"' value='"+value+"'/&gt;"+
    "&lt;/filter&gt;"+
    "&lt;/link-entity&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);
    // 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);
    }
    // Process and display the results.
    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');
          var msg = "\Account Id\tTerms\r";
    msg +="--------------------------------------------------------------------------------\r";
        for (i=0;i < results.length;i++)
        {
         var idValue = results[i].selectSingleNode('./name').nodeTypedValue;
    try
    {
        var name = results[i].selectSingleNode('./paymenttermscode').getAttribute("name");
    }
    catch(err)
    {
          var name = " ";
    }
        msg += idValue +"\t"+ name +"\r";
        }
        alert(msg);
    }


    GM

    • Marked As Answer by sbdt Wednesday, August 22, 2012 7:50 PM
    •