Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Get the field value from contact entity using JavaScript

(0) ShareShare
ReportReport
Posted on by 140

 function SetTitle(executionContext)
{
    var formContext = executionContext.getFormContext();
    var ticketnumber = formContext.getAttribute("ticketnumber")
    var contact = formContext.getAttribute("customerid");
    //var regardingObjectDetails = formContext.getAttribute("regardingobjectid").getValue();
    var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/contacts(ContactId)?$select=funds_personid", true);
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) {
            if(ticketnumber != null && contact != null){
             
            var result = JSON.parse(this.response);
            var funds_personid = result["funds_personid"];
           var Ticket = ticketnumber.getValue();
           var con = contact.getValue()[0].name;
           var title = formContext.getAttribute("title").setValue(Ticket   " "   con   " "   funds_personid)
        }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();
}

Hey guys I am trying to get this Id field from contact entity and set case title to few values

but Dynamics gives me error ContactID is not defined. could you guys tell what the issue is 

Person ID is text field 

 function SetTitle(executionContext)
{
    var formContext = executionContext.getFormContext();
    var ticketnumber = formContext.getAttribute("ticketnumber")
    var contact = formContext.getAttribute("customerid");
    //var regardingObjectDetails = formContext.getAttribute("regardingobjectid").getValue();
    var req = new XMLHttpRequest();
req.open("GET"Xrm.Page.context.getClientUrl()   "/api/data/v9.1/contacts(ContactId)?$select=funds_personid"true);
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) {
            if(ticketnumber != null && contact != null){
             
            var result = JSON.parse(this.response);
            var funds_personid = result["funds_personid";
           var Ticket = ticketnumber.getValue();
           var con = contact.getValue()[0.name;
           var title = formContext.getAttribute("title").setValue(Ticket   " "   con   " "   funds_personid)
        }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();
}
 
  • msboy Profile Picture
    msboy 140 on at
    RE: Get the field value from contact entity using JavaScript

    I idea was not to hard code specific contact record ID  it but get the person id based on customer Id select which is a look up to contact  

  • Akanksha Ranjan Profile Picture
    Akanksha Ranjan 460 on at
    RE: Get the field value from contact entity using JavaScript

    Hi,

    I have modified your code. Please replace your code with the code below. 

    This will solve your issue.

    function SetTitle(executionContext) {
    var formContext = executionContext.getFormContext();
    var ticketnumber = formContext.getAttribute("ticketnumber")
    var contact = formContext.getAttribute("customerid").getValue()[0].id.replace("{", "").replace("}", "");
    //var regardingObjectDetails = formContext.getAttribute("regardingobjectid").getValue();
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/contacts(" + contact + ")?$select=funds_personid", true);
    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) {
    if (ticketnumber != null && contact != null) {

    var result = JSON.parse(this.response);
    var funds_personid = result["funds_personid"];
    var Ticket = ticketnumber.getValue();
    var con = contact.getValue()[0].name;
    var title = formContext.getAttribute("title").setValue(Ticket + " " + con + " " + funds_personid)
    }
    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();
    }

    Thanks

    Akanksha

    Please mark the answer as Verified if found helpful.

  • Suggested answer
    PabloCRP Profile Picture
    PabloCRP 1,086 on at
    RE: Get the field value from contact entity using JavaScript

    Hi, replace ContactId value as you need

    function SetTitle(executionContext) {
        var formContext = executionContext.getFormContext();
        var ticketnumber = formContext.getAttribute("ticketnumber")
        var contact = formContext.getAttribute("customerid");
        //var regardingObjectDetails = formContext.getAttribute("regardingobjectid").getValue();
        var ContactId = contact.getValue()?.[0].id.replace(/[{|}]/g, "");
        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl()   `/api/data/v9.1/contacts(${ContactId})?$select=funds_personid`, true);
        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) {
                    if (ticketnumber != null && contact != null) {
    
                        var result = JSON.parse(this.response);
                        var funds_personid = result["funds_personid"];
                        var Ticket = ticketnumber.getValue();
                        var con = contact.getValue()[0].name;
                        var title = formContext.getAttribute("title").setValue(Ticket   " "   con   " "   funds_personid)
                    }
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
    }

    regards.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Get the field value from contact entity using JavaScript

    Hello,

    Issue is in the line

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/contacts(ContactId)?$select=funds_personid", true);

    to be more specific - ContactId - you should provide the Id of the record you want to get instead of ContactId

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

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans