web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Fetching Customer Name using JavaScript on Case Form

(0) ShareShare
ReportReport
Posted on by 676

Every Invoice has an Invoice Id associated with each of them. I have created a field of datatype 'Single line of text' on the Case entity and it is present on case form, where the user enters the Invoice Id. I want to populate customer field based on this Invoice Id using JavaScript. ?How can I achieve this, I successfully fetched the value entered by user but I am unable to fetch the associated customer's name here. 

Any help would be much appreciated.

*This post is locked for comments

I have the same question (0)
  • Andreas Cieslik Profile Picture
    9,267 on at

    Hi Mohd Tahir,

    you could post the code you already have done? So one of the post readers can improve it from there.

    Cheers,

    Andreas

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Mohd Tahir,

    Would you try get the customer id from Invoice entity?

    Make an XmlHttpRequest to [Your Org's Web Api Url]/invoices?$filter=invoicenumber eq '[User entered inovice number]'&$select=_customerid_value

    Add an HTTP header "Prefer" and its value "odata.include-annotations=OData.Community.Display.V1.FormattedValue"

    In the result, look for, "_customerid_value@OData.Community.Display.V1.FormattedValue"

    Regards,

    Charmis

  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at

    Hi

    call bellow function on your Invoice attribute change, replace new_invoiceid and new_customername with your attribute names.

    GetCustomerNameFromInvoice: function () {

           /* Replace new_invoiceid with your Invoice relationship attribute name*/

           var invoice = Xrm.Page.getAttribute("new_invoiceid").getValue();

           if (invoice != null) {

               var invoiceId = invoice[0].id.replace('{', '').replace('}', '');

               var req = new XMLHttpRequest();

               req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/invoices(" + invoiceId + ")?$select=_customerid_value", false);

               req.setRequestHeader("OData-MaxVersion", "4.0");

               req.setRequestHeader("OData-Version", "4.0");

               req.setRequestHeader("Accept", "application/json");

               req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

               req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

               req.onreadystatechange = function () {

                   if (this.readyState === 4) {

                       req.onreadystatechange = null;

                       if (this.status === 200) {

                           var result = JSON.parse(this.response);

                           var customerName = result["_customerid_value@OData.Community.Display.V1.FormattedValue"];

                           /* Replace new_customername with your customer single line text field*/

                           Xrm.Page.getAttribute("new_customername").setValue(customerName);

                       } else {

                           Xrm.Utility.alertDialog(this.statusText);

                       }

                   }

               };

               req.send();

           }

       }

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Mohd Tahir,

    The JavaScript code in the following thread helps you in retrieving the data from one entity to another entity. Please go through it. Hope this helps you.

    community.dynamics.com/.../139190

  • Mohd Tahir Profile Picture
    676 on at

    Hi Mahadeo,

    I want to set value of a lookup field. In my case lookup field is "customerid" ( customer name)  on the case form but it doesn't allows me to set text to a lookup field. How can I set value to a lookup field?

  • Suggested answer
    Community Member Profile Picture
    on at

    Refer this link:

    community.dynamics.com/.../javascript-set-lookup-details

  • Mohd Tahir Profile Picture
    676 on at

    Hi Saad,

    Thank you for sharing the relevant link but I want to know how to get these parameters which we need to set the lookup field including GUID?  Do I have to hit another API call to get these details?

  • Community Member Profile Picture
    on at

    Share your code here. I will show you how to get the details.

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Mohd Tahir,

    The following sample code is used to retrieve entity information by Entity Id via Javascript.

    function getRecordDetails() {
    
        var EntityName, EntityId, LookupFieldObject;
    
        var Name = "";
    
        var resultXml;
    
        LookupFieldObject = Xrm.Page.data.entity.attributes.get('parentaccountid');
    
        if (LookupFieldObject.getValue() != null) {
    
            EntityId = LookupFieldObject.getValue()[0].id;
    
            EntityName = LookupFieldObject.getValue()[0].entityType;
    
            resultXml = getDetails(EntityName, EntityId);
    
    
    
    // Retrieve lookup field value and set the value to current record field.
    
            if (resultXml != null && resultXml.attributes['primarycontactid'] != null) {          
    
                var lookupValue = new Array();
    
                lookupValue[0] = new Object();
    
                lookupValue[0].id = resultXml.attributes['primarycontactid'].id;
    
                lookupValue[0].name = resultXml.attributes['primarycontactid'].name;
    
                lookupValue[0].entityType = resultXml.attributes['primarycontactid'].logicalName;
    
                Xrm.Page.data.entity.attributes.get("parentcontactid").setValue(lookupValue);
    
            }
    
            else
    
                Xrm.Page.data.entity.attributes.get("parentcontactid").setValue(null);
    
        }
    
    }
    
    function getDetails(EntityName, EntityId) {
    
        var cols = ["primarycontactid"];
    
        var retrievedResult = XrmServiceToolkit.Soap.Retrieve(EntityName, EntityId, cols);
    
        return retrievedResult;
    
    }

    Hope this helps you.

  • Mohd Tahir Profile Picture
    676 on at

    Hi Saad,

    This is what I am trying to do. In the first part I am trying to retrieve invoice details then I again try to hit another API to get the account details, so that I can set them to the lookup field. Let me know if this is the correct approach:

    function GetCustomerNameFromInvoice()
    {
    /* Replace new_invoiceidsearch with your Invoice relationship attribute name*/
    var customerName;
    var invoiceId = Xrm.Page.getAttribute("new_invoiceidsearch").getValue();
    if (invoiceId != null) {
    //var invoiceId = invoice[0].id.replace('{', '').replace('}', '');
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/invoices?$filter=invoicenumber eq '" + invoiceId + "'&$select=_customerid_value", false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var result = JSON.parse(this.response);
    customerName = result.value[0]["_customerid_value@OData.Community.Display.V1.FormattedValue"]
    /* Replace new_customername with your customer single line text field*/
    Xrm.Page.getAttribute("customerid").setValue(customerName);
    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();
    }
    if (customerName != null)
    {
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts?$filter=name eq '" +customerName+ "'";
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var result = JSON.parse(this.response);
    customerName = result.value[0]["_customerid_value@OData.Community.Display.V1.FormattedValue"]
    /* Replace new_customername with your customer single line text field*/
    Xrm.Page.getAttribute("customerid").setValue(customerName);
    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();

    }




    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans