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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

while error retrieve field value from Contact to Customer entity using javascript

(0) ShareShare
ReportReport
Posted on by

There are two entities 

1.  Contact

2. Customer

I will try to retrieve field value from contact(field name mobile) and update to Customer(field name sbl_contact_mobile) with below code but field value should be blank.So please check and correction of my code.

function GetDataOnChange()
{
var custid = Xrm.Page.getAttribute("parentaccountid").getValue()[0].id;
if(custid !=null) {
var cid =custid.replace("{","").replace("}","");
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts("+cid+")?$select=mobilephone", 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) {
var result = JSON.parse(this.response);
var contmobile = result["mobilephone"]; // will return value example - 8
Xrm.Page.getAttribute("sbl_contact_mobile").setValue(contmobile);

} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
}

 

I have the same question (0)
  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    do they have relationship ? would you consider to use workflow instead of webapi to achieve it?.

    I personally will suggest you to use workflow to achieve it, because JS only happens in client side.

  • Community Member Profile Picture
    on at

    Yes I have done with Workflow but i need to retrieve with script

  • Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    would you mind to debug the JS and let us know which line is causing the problem?

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    By the way, I would suggest you to use rest builder to help you to generate the code.

    www.toplinestrategies.com/.../crm-rest-builder-useful-tool-working-crm-2016%E2%80%99s-web-api

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    I have checked your code and it looks fine. You need to ensure that the custid you are passing is a contact record (and not account). Also the mobile number should exists in the record you are retrieving.

    Hope this helps.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Dynamics 365 general

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans