locked
Relaying Attributes Between Different Entities RRS feed

  • Question

  • Hi,

    We have a CRM deployment being used as part of a Helpdesk and have different entities such as Customer, Contract and Jobs. I need to set up a system where if a check box on the Contract field called Do Not Serve is ticked, when a Job is created and the Customer is selected, an alert box will pop up informing the user, 'This Customer is currently on a Do Not Serve List'.

    After reading up on many different articles, I think this can be achieved using a retrieveattribute and SOAP requests but am lacking in JavaScript knowledge to be able to complete this unfortunately.

    Would anyone be able to point me in the right direction to get started with this?

    EDIT: Can I use any of the functions details here to achieve this in any way?


    Wednesday, May 30, 2012 11:33 AM

Answers

  • For anyone still following this thread, problem was resolved using the following code:

    function CheckValue() {
         var CustomerID;
         if (crmForm.all.<<CustomerIDFielName>>.DataValue != null) //Please chage field name here
         {
             CustomerID = crmForm.all.customerid.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>" +
     "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
     "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'" +
     " xsi:type='q1:QueryExpression'>" +
     "<q1:EntityName>ab_contract</q1:EntityName>" +
     "<q1:ColumnSet xsi:type='q1:ColumnSet'>" +
     "<q1:Attributes>" +
     "<q1:Attribute>ab_onstop</q1:Attribute>" +
     "</q1:Attributes>" +
     "</q1:ColumnSet>" +
     "<q1:Distinct>false</q1:Distinct>" +
     "<q1:Criteria>" +
     "<q1:FilterOperator>And</q1:FilterOperator>" +
     "<q1:Conditions>" +
     "<q1:Condition>" +
     "<q1:AttributeName>CustomerIDFieldName</q1:AttributeName>" + //Please change with customerid field name
     "<q1:Operator>Equal</q1:Operator>" +
     "<q1:Values>" +
     "<q1:Value xsi:type='xsd:string'>" + CustomerID + "</q1:Value>" +
     "</q1:Values>" +
     "</q1:Condition>" +
     "</q1:Conditions>" +
     "</q1:Criteria>" +
     "</query>" +
     "</RetrieveMultiple>" +
     "</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/RetrieveMultiple");
             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);
             }
             // Parse and display the results.
             else {
                 var results = resultXml.getElementsByTagName('BusinessEntity');
                 var msg = "";
                 if (results.length == 0) {
                     msg = "No contacts were found";
                     alert(msg);
                     return;
                 }
                 else {
                   
                    var resultXML=results[0];
                     
                        var _value = results[0].selectSingleNode('./q1:ab_onstop').nodeTypedValue;
                         if(_value==1)
                         alert("Customer is on the Do Not Serve List");
                       
                    }
                     
                }
             } 
        }

    For absolute newbies (like myself), dont'f forget to call the function using
    CheckValue();

    • Marked as answer by Josh Herbert Monday, June 11, 2012 11:29 AM
    Monday, June 11, 2012 11:29 AM

All replies

  • Hi,

    yes you can implement this using javascript, if you have that checkbox in your contract entity and a cuatomer field,  you need to use retrievemultiple function to get value of checkbox based on customer id, but if you have that field in customer entity itself you need to use retrieve function to get checkbox value based on customer id. once you retrieve that value you can show alert message based on that value.


    Mahain : Check My Blog
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Proposed as answer by HIMBAPModerator Wednesday, May 30, 2012 12:02 PM
    • Unproposed as answer by Josh Herbert Tuesday, June 5, 2012 1:58 PM
    Wednesday, May 30, 2012 11:59 AM
    Moderator
  • Thanks for your reply. The check box only exists in the contract entity so how would I use the following in an OnChange event in the jobs form?

    The check box is called ab_onstop and is in the ab_contract entity.

    The entity where this needs to be displayed is called ab_jobs.

    function RetrieveRecord(entityName, entityId, attrArray) {
      var xmlSoapBody = ""
        + "    <Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"
        + "      <entityName>" + entityName + "</entityName>"
        + "      <id>" + entityId + "</id>"
        + "      <columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"
        + "        <q1:Attributes>";
    
      for (index in attrArray) {
        xmlSoapBody += "          <q1:Attribute>" + attrArray[index] + "</q1:Attribute>";
      }
    
      xmlSoapBody += ""
        + "        </q1:Attributes>"
        + "      </columnSet>"
        + "    </Retrieve>";
    
      var resultXml = MischiefMayhemSOAP(xmlSoapBody, 'http://schemas.microsoft.com/crm/2007/WebServices/Retrieve');
    
      if (resultXml != null) {
        var resultArray = new Array();
    
        for (index in attrArray) {
          if (resultXml.selectSingleNode("//q1:" + attrArray[index]) != null) {
            resultArray[index] = resultXml.selectSingleNode("//q1:" + attrArray[index]).nodeTypedValue;
          } else {
            resultArray[index] = null;
          }
        }
    
        return resultArray;
      } else {
        return null;
      }
    }

    And would I just be able to use the value returned in an if statement i.e.:

    if (ab_onstop = 1)
    {
    alert('Customer is on the Do Not Serve List')
    }
    Apologies for the extended questions but this is my first JavaScript experience!
    Wednesday, May 30, 2012 12:45 PM
  • Hi,

    yes, you need to check for 1 or 0 for true and false.


    Mahain : Check My Blog
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, May 30, 2012 4:07 PM
    Moderator
  • Thanks once again but I think I wasn't clear enough with my last request. To phrase it another way, I only want to retrieve one value so do i still need to use an array or can I just specify the one value that I need to retrieve?

    Would you be able to rewrite this code to enable me to do this please?

    Thursday, May 31, 2012 3:46 PM
  • Hi,

    I did not notice your last reply, you said checkbox is on contract entity, and you want to check based on the customer selected in job if contract record for this customer have this check box enabled or not right ?? so you need to use retrievemultiple because you want to check contract record based on customer id , you can use below code onchage of customer

    field, Please make sure to change customerid field in below script, let me know if you are having any issue

    function CheckValue() {
        var CustomerID;
        if (crmForm.all.<<CustomerIDFielName>>.DataValue != null) //Please chage field name here
        {
            CustomerID = crmForm.all.customerid.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>" +
    "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
    "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'" +
    " xsi:type='q1:QueryExpression'>" +
    "<q1:EntityName>ab_contract</q1:EntityName>" +
    "<q1:ColumnSet xsi:type='q1:ColumnSet'>" +
    "<q1:Attributes>" +
    "<q1:Attribute>ab_onstop</q1:Attribute>" +
    "</q1:Attributes>" +
    "</q1:ColumnSet>" +
    "<q1:Distinct>false</q1:Distinct>" +
    "<q1:Criteria>" +
    "<q1:FilterOperator>And</q1:FilterOperator>" +
    "<q1:Conditions>" +
    "<q1:Condition>" +
    "<q1:AttributeName>CustomerIDFieldName</q1:AttributeName>" + //Please change with customerid field name
    "<q1:Operator>Equal</q1:Operator>" +
    "<q1:Values>" +
    "<q1:Value xsi:type='xsd:string'>" + CustomerID + "</q1:Value>" +
    "</q1:Values>" +
    "</q1:Condition>" +
    "</q1:Conditions>" +
    "</q1:Criteria>" +
    "</query>" +
    "</RetrieveMultiple>" +
    "</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/RetrieveMultiple");
            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);
            }
            // Parse and display the results.
            else {
                var results = resultXml.getElementsByTagName('BusinessEntity');
                var msg = "";
                if (results.length == 0) {
                    msg = "No contacts were found";
                    alert(msg);
                    return;
                }
                else {
                 
                    var resultXML=results[0];
                   
                        var _value = results[i].selectSingleNode('./q1:ab_onstop').nodeTypedValue;
                        if(_value==1)
                        alert("Customer is on the Do Not Serve List");
                     
                    }
                   
                }
            }
        }

    Thursday, May 31, 2012 4:42 PM
    Moderator
  •  "<q1:AttributeName>CustomerIDFieldName</q1:AttributeName>" + //Please change with customerid field name
    
     

    Thanks once again Mahendar. This has helped me greatly. However, I think it just needs a little tweaking to work with my environment. Could you please explain this part here as to what it does? Should that not be ab_stop and not customerid?

    Also, Could you please explain exactly what the SOAP message is doing between <soap:body> and </soap:body>? I just can't seem to understand it.

    Friday, June 1, 2012 12:20 PM
  • Hi,

    you are welcome.

    As I said earlier because your checkbox and customer lookup is on contract entity, so we have written retrievemultiple function to retrieve contract record based on customer id, that you will select in job entity record,

    so we have added a condition here, you need to replace CustomerIDFieldName with customer name field in contract entity.

    "<q1:AttributeName>CustomerIDFieldName</q1:AttributeName>" + //Please change with customerid field name
    "<q1:Operator>Equal</q1:Operator>" +
    "<q1:Values>" +
    "<q1:Value xsi:type='xsd:string'>" + CustomerID + "</q1:Value>" +

    let me know if still you have any question


    Conatact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Proposed as answer by HIMBAPModerator Friday, June 1, 2012 5:50 PM
    • Unproposed as answer by Josh Herbert Thursday, June 7, 2012 9:14 AM
    Friday, June 1, 2012 5:50 PM
    Moderator
  • Hi Mahendar,

    Thanks for the explanation. I'm still having some trouble with this though.

    I keep getting the following error:

    Any ideas on what is causing this?

    Tuesday, June 5, 2012 1:53 PM
  • Please try to change below line

     var _value = results[i].selectSingleNode('./q1:ab_onstop').nodeTypedValue;

    to

     var _value = results[0].selectSingleNode('./q1:ab_onstop').nodeTypedValue;

    also you can try to debug and see what resultset you are getting in resultXml


    Conatact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Tuesday, June 5, 2012 2:29 PM
    Moderator
  • I made the change and it now mostly works perfectly for customers where the checkbox is ticked but I get the following error with a few customers for whome the box is not ticked.

    Also, I am unsure of how to run a debugger for this. How do I get started?

    Tuesday, June 5, 2012 3:04 PM
  • For anyone still following this thread, problem was resolved using the following code:

    function CheckValue() {
         var CustomerID;
         if (crmForm.all.<<CustomerIDFielName>>.DataValue != null) //Please chage field name here
         {
             CustomerID = crmForm.all.customerid.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>" +
     "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
     "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'" +
     " xsi:type='q1:QueryExpression'>" +
     "<q1:EntityName>ab_contract</q1:EntityName>" +
     "<q1:ColumnSet xsi:type='q1:ColumnSet'>" +
     "<q1:Attributes>" +
     "<q1:Attribute>ab_onstop</q1:Attribute>" +
     "</q1:Attributes>" +
     "</q1:ColumnSet>" +
     "<q1:Distinct>false</q1:Distinct>" +
     "<q1:Criteria>" +
     "<q1:FilterOperator>And</q1:FilterOperator>" +
     "<q1:Conditions>" +
     "<q1:Condition>" +
     "<q1:AttributeName>CustomerIDFieldName</q1:AttributeName>" + //Please change with customerid field name
     "<q1:Operator>Equal</q1:Operator>" +
     "<q1:Values>" +
     "<q1:Value xsi:type='xsd:string'>" + CustomerID + "</q1:Value>" +
     "</q1:Values>" +
     "</q1:Condition>" +
     "</q1:Conditions>" +
     "</q1:Criteria>" +
     "</query>" +
     "</RetrieveMultiple>" +
     "</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/RetrieveMultiple");
             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);
             }
             // Parse and display the results.
             else {
                 var results = resultXml.getElementsByTagName('BusinessEntity');
                 var msg = "";
                 if (results.length == 0) {
                     msg = "No contacts were found";
                     alert(msg);
                     return;
                 }
                 else {
                   
                    var resultXML=results[0];
                     
                        var _value = results[0].selectSingleNode('./q1:ab_onstop').nodeTypedValue;
                         if(_value==1)
                         alert("Customer is on the Do Not Serve List");
                       
                    }
                     
                }
             } 
        }

    For absolute newbies (like myself), dont'f forget to call the function using
    CheckValue();

    • Marked as answer by Josh Herbert Monday, June 11, 2012 11:29 AM
    Monday, June 11, 2012 11:29 AM