locked
CRM 2011: Soap Call to Retrieve Contact Phone Number RRS feed

  • Question

  • I have a lookup field Sponsor called 'zy_sponsorid' and Sponsor Phone field 'zy_sponsorphone' I want to populate. Once I select a name from the lookup from Contacts, I am trying to get the Sponsor Phone to populate that Contact's phone number. This code worked in 4.0 perfectly, I ran it through the Migration Assistant and now I am having troubles. Can someone assist? Here is the code:

    function GetContactPhoneNumber(ContactId)
    {

    alert("getcontact is being called");
    var resultXml;
    var telephoneNumber = null;
    var errorCount;
    var msg;
    var xmlHttpRequest;
    var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();

    //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>telephone1</q1:Attribute>" +
    "</q1:Attributes>" +
    "</columnSet>" +
    "</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;

    var errorCount = resultXml.selectNodes('//error').length;

    if (errorCount != 0)
    {
        var msg = resultXml.selectSingleNode('//description').nodeTypedValue; //Process and display the results.
    }
    else
    {
        if (resultXml.selectSingleNode('//q1:telephone1') != null)
            telephoneNumber = resultXml.selectSingleNode('//q1:telephone1').nodeTypedValue;
    }

    return telephoneNumber;
    }

    // Set the telephone field
    if(Xrm.Page.getAttribute("zy_sponsorid").getValue() != null)
    {
    Xrm.Page.getAttribute("zy_sponsorphone").setValue(GetContactPhoneNumber(Xrm.Page.getAttribute("zy_sponsorid").getValue()[0].id));
    //Xrm.Page.getAttribute("zy_sponsorphone").setValue('(123) 456-7890');
    }
    else
    {
        Xrm.Page.getAttribute("zy_sponsorphone").setValue(null);
    }

    Wednesday, April 6, 2011 3:41 PM

Answers

  • function GetContactPhoneNumber(ContactId)
    {
     var resultXml;
     var telephoneNumber = null;
     var errorCount;
     var msg;
     var xmlHttpRequest;
     var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();

     //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>telephone1</q1:Attribute>" +
     "</q1:Attributes>" +
     "</columnSet>" +
     "</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;

     var errorCount = resultXml.selectNodes('//error').length;

     if (errorCount != 0)
     {
      var msg = resultXml.selectSingleNode('//description').nodeTypedValue; //Process and display the results.
     }
     else
     {
      if (resultXml.selectSingleNode('//q1:telephone1') != null)
      {
      telephoneNumber = resultXml.selectSingleNode('//q1:telephone1').nodeTypedValue;
      Xrm.Page.getAttribute("zy_sponsorphone").setValue(telephoneNumber);
      return;
      }
     }

     // If sponsorID is not null, calls getcontactphonenumber, if it is null clears the number
     if(Xrm.Page.getAttribute("zy_sponsorid").getValue() != null)
     {
      GetContactPhoneNumber(Xrm.Page.getAttribute("zy_sponsorid").getValue()[0].id)
      return;
     }
     else
     {
      Xrm.Page.getAttribute("zy_sponsorphone").setValue(null);
     }
    }

    • Marked as answer by Michael Karls Wednesday, April 6, 2011 10:25 PM
    Wednesday, April 6, 2011 10:25 PM

All replies

  • I have a couple questions.

    1. I am guessing you just forgot to paste the whole thing, but there is no close bracket "}" to end your function.

    2. add this: debugger; to the code and turn on script debugging in IE and execute the script.  It should stop on the break point and you can step through it using a jscript debugger like visual studio.  It will just ask you what you want to use to debug and Visual Studio should be an option if you have it. 

    I actually demonstrate the debugging technique in this article:

    http://mileyja.blogspot.com/2011/03/microsoft-dynamics-crm-2011-parsing.html

    I hope this helps!


    Jamie Miley
    http://mileyja.blogspot.com
    Linked-In Profile
    Follow Me on Twitter!
    Wednesday, April 6, 2011 4:00 PM
    Moderator
  • Wednesday, April 6, 2011 4:05 PM
    Moderator
  • Thanks Jamie. I'm checking it out right now.
    Wednesday, April 6, 2011 4:42 PM
  • function GetContactPhoneNumber(ContactId)
    {
     var resultXml;
     var telephoneNumber = null;
     var errorCount;
     var msg;
     var xmlHttpRequest;
     var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();

     //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>telephone1</q1:Attribute>" +
     "</q1:Attributes>" +
     "</columnSet>" +
     "</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;

     var errorCount = resultXml.selectNodes('//error').length;

     if (errorCount != 0)
     {
      var msg = resultXml.selectSingleNode('//description').nodeTypedValue; //Process and display the results.
     }
     else
     {
      if (resultXml.selectSingleNode('//q1:telephone1') != null)
      {
      telephoneNumber = resultXml.selectSingleNode('//q1:telephone1').nodeTypedValue;
      Xrm.Page.getAttribute("zy_sponsorphone").setValue(telephoneNumber);
      return;
      }
     }

     // If sponsorID is not null, calls getcontactphonenumber, if it is null clears the number
     if(Xrm.Page.getAttribute("zy_sponsorid").getValue() != null)
     {
      GetContactPhoneNumber(Xrm.Page.getAttribute("zy_sponsorid").getValue()[0].id)
      return;
     }
     else
     {
      Xrm.Page.getAttribute("zy_sponsorphone").setValue(null);
     }
    }

    • Marked as answer by Michael Karls Wednesday, April 6, 2011 10:25 PM
    Wednesday, April 6, 2011 10:25 PM
  • I am curious, what was the issue in the end?  Did you find the debugging method helpful?
    Jamie Miley
    http://mileyja.blogspot.com
    Linked-In Profile
    Follow Me on Twitter!
    Friday, April 8, 2011 4:03 PM
    Moderator
  • Nice and how to use this one for lookup field ?
    Monday, August 1, 2011 8:43 AM
  • Hi,

    To retirve lookup fields you can reffer the code example at hte following post: http://worldofdynamics.blogspot.com/2011/06/microsoft-dynamics-crm-2011-retrieve.html

    If this helps then Please Vote as Helpful and Mark as Answer.


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Monday, August 1, 2011 9:06 AM
  • The issue was seperating the function into 2 functions.

    One function that is just the SOAP call and one function that calls the SOAP call and populates the field.

    Having this as one big function overloads the system and causes an "Out of stack space" error.

    Thursday, August 11, 2011 4:32 PM
  • Hi,

    This should not be the issue, i would suggest you try the code from this link: http://worldofdynamics.blogspot.com/2011/06/microsoft-dynamics-crm-2011-retrieve.html


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Thursday, August 11, 2011 4:36 PM