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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
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();
}
 
I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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

  • Suggested answer
    PabloCRP Profile Picture
    1,088 on at

    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.

  • Akanksha Ranjan Profile Picture
    460 on at

    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.

  • msboy Profile Picture
    140 on at

    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  

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…

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 60 Super User 2026 Season 1

#2
Eugen Podkorytov Profile Picture

Eugen Podkorytov 26

#3
CU07021359-0 Profile Picture

CU07021359-0 23 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans