locked
how to modify my code for custom entity in crm 2013 RRS feed

  • Question

  • Hi,

    Please tell me what is user_accounts in this code and how to modify this code for custom entity name= "new_buyer" and lookup name="new_buyerlookup" and get attribute field "new_email"

    function GetEmail() {
        var account = Xrm.Page.getAttribute("parentaccountid").getValue();
        if (account === null || account === undefined) return;
    
        var req = new XMLHttpRequest();
        req.open('GET', encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'" + account[0].id + "')?$select=EMailAddress1&$expand=user_accounts&undefined"), true);
        req.setRequestHeader('Accept', 'application/json');
        req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    var result = JSON.parse(req.responseText).d;
                    var EMailAddress1 = result.EMailAddress1;
                    alert(EMailAddress1);
                }
                else {
                    alert(this.statusText);
                }
            }
        };
        req.send();
    
    }
    Thursday, March 20, 2014 6:21 AM

All replies

  • Hello,

    It seems user_accounts is the name of replationship between account and user entity, expend is used to get related entity record with primary entity, so if you are looking to get data from related entity where you have new email field first you need to get relationship (1:n) name between these entities and then schema name of the field, it should look something like below

    function GetEmail() {
    var EntitySchemaname="Provide here your new_buyer entity schema name";// may be new_buyerSet ?
    var Relationshipname="Provide relationshipname here";
    var Buyerlookup= Xrm.Page.getAttribute("new_buyerlookup").getValue();
    if (Buyerlookup === null || Buyerlookup=== undefined) return;
    var req = new XMLHttpRequest();
    req.open('GET', encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/"+EntitySchemaname+"(guid'" + account[0].id + "')?$select=EMailAddress1&$expand=Relationshipname"), true);
     req.setRequestHeader('Accept', 'application/json');
     req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
     req.onreadystatechange = function () {
     if (this.readyState === 4) {
     req.onreadystatechange = null;
     if (this.status === 200)
     { var result = JSON.parse(req.responseText).d;
     var EMailAddress1 = result.new_email; // change this field with new_email schema name
     alert(EMailAddress1); }
     else { alert(this.statusText); } } };
     req.send(); }


    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, March 20, 2014 6:55 AM
    Moderator
  • Hi,

          user_accounts in the query is the relationship record being fetched. If you wish to fetch new_buyer, your query should be something like following:

        req.open('GET', encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/new_buyerSet<Relevant name from WSDL note: for account entity it is AccountSet>(guid'" + Record GUID + "')?$select=new_buyerlookup,new_email "), true);

    Refer to following article for further syntax:

    http://msdn.microsoft.com/en-us/library/gg309461.aspx


    Hope this helps.
     
    -----------------------------------------------------------------------
     Minal Dahiya
    blog : http://minaldahiya.blogspot.com.au/

     
    If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"

    Thursday, March 20, 2014 7:01 AM
  • when I run this script I got an error "Not Found"
    Thursday, March 20, 2014 9:15 AM
  • Hi Aamir,

         You need to build the string based on your database schema.

    -Minal

    Thursday, March 20, 2014 9:30 AM
  • Hi Minal,

    here is my script and tell me what did I miss in my code?

    function GetEmail() {
    var EntitySchemaname="taskSet";// may be new_buyerSet ?
    var Relationshipname="new_task_task_Predecessor";
    var Buyerlookup= Xrm.Page.getAttribute("new_predecessor").getValue();
    alert(Buyerlookup[0].id);
    if (Buyerlookup == null || Buyerlookup == undefined) return;
    var req = new XMLHttpRequest();
    req.open('GET', encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/"+EntitySchemaname+"(guid'" + Buyerlookup[0].id + "')?$select=EMailAddress1,new_task_task_Predecessor"), true);
     req.setRequestHeader('Accept', 'application/json');
     req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
     req.onreadystatechange = function () {
     if (this.readyState == 4) {
     req.onreadystatechange = null;
     if (this.status == 200)
     { var result = JSON.parse(req.responseText).d;
     var EMailAddress1 = result.subject; // change this field with new_email schema name
     alert(EMailAddress1); }
     else { alert(this.statusText); } } };
     req.send(); }

    Thursday, March 20, 2014 9:32 AM
  • Hi,

    Please tell me what is EMailAddress1 in this line. Is it schema name or display name of email address field which value I want to get?

    $select=EMailAddress1&$expand=user_accounts&undefined"), true);


    Thursday, March 20, 2014 10:24 AM
  • Hi,

          EmailAddress1 is the email address field. Please refer to following article for understanding the syntax:

    http://msdn.microsoft.com/en-us/library/gg309461.aspx


    Hope this helps.
     
    -----------------------------------------------------------------------
     Minal Dahiya
    blog : http://minaldahiya.blogspot.com.au/

     
    If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"

    Thursday, March 20, 2014 10:53 AM
  • HI Minal,

    kindly check my script I am using this script in crm 2013 but  Xrm.Page.context.getAuthenticationHeader(); and selectSingleNode are not supported please tell me what should I use instead of Xrm.Page.context.getAuthenticationHeader(); and selectSingleNode

    function getEmail()
    {
        var authenticationHeader, xml, xHReq;
        var resultXml, errorCount, msg, returnValue, returnId;


        authenticationHeader = Xrm.Page.context.getAuthenticationHeader();

    var contact = Array();

         contact = Xrm.Page.getAttribute("new_predecessorid").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("new_emailaddress").setValue(req);
            }

            else {

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

            }

        }
    }

    Thursday, March 20, 2014 11:22 AM