locked
how to get contact email in crm 2013 RRS feed

  • Question

  • Hi,

    I have created an entity and its contact lookup field. I want to get contact email address on lookup field change event. I have created a script but its not working in CRM 2013. Kindly tell me whats the issue in my script

    function getContactId() {

        var authenticationHeader, xml, xHReq;

        var resultXml, errorCount, msg, returnValue, returnId;




    if(Xrm.Page.context.getClientUrl)//Post UR 12
    {
        authenticationHeader = Xrm.Page.context.getClientUrl();
    }
    else//Pre UR 12
    {
        authenticationHeader = Xrm.Page.context.getServerUrl();
    }

        var contact = new Array();




        contact = Xrm.Page.getAttribute("primarycontactid").getValue();

        //alert(contact[0].id.toString());

        //alert(contact[0].name);




        // Define the SOAP XML to access Microsoft Dynamics CRM Web service.

        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>" +

        // Specify the RetrieveMultiple message.

            "<RetrieveMultiple xmlns=" +

            "\"http://schemas.microsoft.com/crm/2007/WebServices\">" +

        // Specify that this is a QueryByAttribute query.

            "<query xmlns:q1=" +

            "\"http://schemas.microsoft.com/crm/2006/Query\" " +

            "xsi:type=\"q1:QueryByAttribute\">" +

        // Query the customeraddress entity.

            "<q1:EntityName>contact</q1:EntityName>" +

        // Set the columns you want to return.

            "<q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +

            "<q1:Attributes>" +

            "<q1:Attribute>emailaddress1</q1:Attribute>" +




            "</q1:Attributes>" +

            "</q1:ColumnSet>" +

        // Specify the attribute that you are querying on.

            "<q1:Attributes>" +

            "<q1:Attribute>contactid</q1:Attribute>" +

            "</q1:Attributes>" +

        // Set the value of the attribute using the customerid

        // value of the case record.

            "<q1:Values>" +

            "<q1:Value xsi:type=\"xsd:string\">" +

            contact[0].id +

            "</q1:Value>" +

            "</q1:Values>" +

            "</query>" +

            "</RetrieveMultiple>" +

            "</soap:Body>" +

            "</soap:Envelope>";




        // Create an instance of an XMLHTTP object.

        xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

        // Configure the XMLHttp object for the Microsoft CRM Web services.

        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

        xmlHttpRequest.setRequestHeader(

            "SOAPAction",

            "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple"

            );

        xmlHttpRequest.setRequestHeader(

            "Content-Type", "text/xml; charset=utf-8"

            );

        xmlHttpRequest.setRequestHeader(

            "Content-Length", xml.length

            );

        // Send the XMLHttp request.

        xmlHttpRequest.send(xml);

        // Capture the XMLHttp response in XML format.

        resultXml = xmlHttpRequest.responseXML;




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

        // alert(errorCount.toString());

        if (errorCount != 0) {

            msg = resultXml.selectSingleNode('//description').nodeTypedValue;

            alert("An error has been occured on loading Customer Information\nError Details: " + msg);

        }

        else {

            if (resultXml.selectNodes("//q1:emailaddress1") != null) {




                var req = resultXml.selectSingleNode("//q1:emailaddress1").nodeTypedValue;

                Xrm.Page.getAttribute("emailaddress1").setValue(req);

            }

            else {

                alert("CRM 2011 encountered an error while fetching for email address");

            }

        }

    }

    Thursday, July 17, 2014 12:07 PM

All replies

  • Aamir,

    MS CRM 2013 does not support 2007 endpoints, you need to convert your code to 2013 standards, so rewrite your query. Check below post for your reference

    http://msdn.microsoft.com/en-in/library/gg594434.aspx

    or you could simple use Odata query, check Odata designer to write your query


    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    Thursday, July 17, 2014 12:16 PM
    Moderator
  • Hi Aamir,

    The following code should do your job. This is in accordance with the new REST API introduced from CRM 2011. The 2007 SOAP endpoint isn't supported anymore.

    function GetContactEmail() { var contactId = Xrm.Page.getAttribute("primarycontactid").getValue(); if (contactId != null) { RetrieveEmail(contactId[0].id); } } function RetrieveEmail(contactId) { var authenticationHeader = ""; if(Xrm.Page.context.getClientUrl)//Post UR 12 { authenticationHeader = Xrm.Page.context.getClientUrl(); } else//Pre UR 12 { authenticationHeader = Xrm.Page.context.getServerUrl(); } var entity = "Contact"; var select = "?$select=EMailAddress1"; var oDataSelect; oDataSelect = authenticationHeader + "/XRMServices/2011/OrganizationData.svc/" + entity + "Set(guid'" + contactId + "')" + select + ""; $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: oDataSelect, beforeSend: function(XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function(data, textStatus, XmlHttpRequest) { SuccessCallback(data.d); }, error: function(xmlHttpRequest, textStatus, errorThrown) { alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown); } }); }

    function SuccessCallback(ContactEntity) { if (ContactEntity != null) { var email = ContactEntity.EMailAddress1; if (email != null) { Xrm.Page.getAttribute("emailaddress1").setValue(email); } } }


    Register GetContactEmail function on your Contact lookup field on change.


    Admin QuikView Solution for CRM 2013



    Thursday, July 17, 2014 12:40 PM
  • Hi Dynamotion,

    Is it work on CRM online?

    Thursday, July 17, 2014 1:14 PM
  • Yes, it should work for online as well.

    Admin QuikView Solution for CRM 2013

    Thursday, July 17, 2014 1:15 PM
  • I used this script but its not working. Please check is it working on your side?
    Thursday, July 17, 2014 1:42 PM
  • Hi Dynamotion,

    I used this script but error is occurred.  Please resolve it

    Thursday, July 17, 2014 1:59 PM
  • I think you are setting value in system field Xrm.Page.getAttribute("emailaddress1").setValue(req);

    where as it should be a custom field Xrm.Page.getAttribute("new_emailaddress1").setValue(req);


    Regards Faisal

    Thursday, July 17, 2014 2:55 PM
  • Hi Aamir,

    There was a mismatch in the field name. I had a look at your code carefully and changed it. Use this, I have modified it.

    function GetContactEmail() 
    {
    	var contactId = Xrm.Page.getAttribute("primarycontactid").getValue();
        
    	if (contactId != null) 
    	{
            RetrieveEmail(contactId[0].id);
        } 
    }
    
    function RetrieveEmail(contactId) 
    {
    	var authenticationHeader = "";
    	
    	if(Xrm.Page.context.getClientUrl)//Post UR 12
    	{
    		authenticationHeader = Xrm.Page.context.getClientUrl();
    	}
    	else//Pre UR 12
    	{
    		authenticationHeader = Xrm.Page.context.getServerUrl();
    	}
    	
        var entity = "Contact";
        var select = "?$select=EMailAddress1";
        var oDataSelect;
    	oDataSelect = authenticationHeader + "/XRMServices/2011/OrganizationData.svc/" + entity + "Set(guid'" + contactId + "')" + select + "";
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: oDataSelect,
            beforeSend: function(XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function(data, textStatus, XmlHttpRequest) {
               SuccessCallback(data.d);
            },
            error: function(xmlHttpRequest, textStatus, errorThrown) {
                alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
            }
        });
    }
    
    
    function SuccessCallback(ContactEntity) 
    {
        if (ContactEntity != null) 
    	{
            var email = ContactEntity.EMailAddress1; 
    
            if (email != null) 
            {
    			Xrm.Page.getAttribute("emailaddress1").setValue(email); 
            }   
        }
    }
    
    


    Admin QuikView Solution for CRM 2013

    Thursday, July 17, 2014 2:55 PM