Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Retrieve fields from related entity using Jscript II

Posted on by Microsoft Employee

Hi,

I am novice in js-scripting and try to retrieve fields from a related entitity "contact"

and fill some fields with contact-details.

This should happen onchange of a lookup-field "new_projektkontakt"

I have loaded the SDK.WEBAPI.js library befor my script :

function retrieveContact() {
var contact = Xrm.Page.getAttribute('new_projektkontakt').getValue();

SDK.REST.retrieveRecord("", "Contact", "EMailAddress1,FullName,MobilePhone,Telephone1", null, function(result) {
var eMailAddress1 = result.EMailAddress1;
var fullName = result.FullName;
var mobilePhone = result.MobilePhone;
var telephone1 = result.Telephone1;
}, function(error) {
Xrm.Utility.alertDialog(error.message);
});

Xrm.Page.getAttribute('new_name').setValue(fullname);
Xrm.Page.getAttribute('new_email').setValue(eMailAddress1);
Xrm.Page.getAttribute('new_mobiltelefon').setValue(mobilePhone);
Xrm.Page.getAttribute('new_festnetz').setValue(telephone1);
}

I get the errormessage :

ReferenceError: retrieveContact is not defined
at eval (eval at RunHandlerInternal (xxxxxxxgmbh.crm4.dynamics.com/.../ClientApiWrapper.aspx), <anonymous>:1:1)
at RunHandlerInternal (xxxxxxxgmbh.crm4.dynamics.com/.../ClientApiWrapper.aspx)
at RunHandlers (xxxxxxxgmbh.crm4.dynamics.com/.../ClientApiWrapper.aspx)
at ExecuteHandler (xxxxxxxgmbh.crm4.dynamics.com/.../ClientApiWrapper.aspx)
at Mscrm.TurboForm.Control.CustomScriptsManager.$Db_1 (xxxxxxxgmbh.crm4.dynamics.com/.../formcontrols.js)
at Mscrm.TurboForm.Control.CustomScriptsManager.executeHandler (xxxxxxxgmbh.crm4.dynamics.com/.../formcontrols.js)
at Mscrm.TurboForm.Control.CustomScriptsManager.executeHandlerByDescriptor (xxxxxxxgmbh.crm4.dynamics.com/.../formcontrols.js)
at xxxxxxxgmbh.crm4.dynamics.com/.../formcontrols.js
at xxxxxxxgmbh.crm4.dynamics.com/.../global.ashx
at Mscrm.TurboForm.Control.Data.LookupDataAttribute.fireOnChange (xxxxxxxgmbh.crm4.dynamics.com/.../formcontrols.js)

Help is much appreciated !!

Harald

*This post is locked for comments

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Retrieve fields from related entity using Jscript II

    you need to pass guid of the record which you are fetching.

    function retrieveContact() {

    var contact = Xrm.Page.getAttribute('new_projektkontakt').getValue();

    SDK.REST.retrieveRecord("guid","Contact", "EMailAddress1,FullName,MobilePhone,Telephone1", null, function(result) {

    var eMailAddress1 = result.EMailAddress1;

    var fullName = result.FullName;

    var mobilePhone = result.MobilePhone;

    var telephone1 = result.Telephone1;

    }, function(error) {

    Xrm.Utility.alertDialog(error.message);

    });

    Xrm.Page.getAttribute('new_name').setValue(fullname);

    Xrm.Page.getAttribute('new_email').setValue(eMailAddress1);

    Xrm.Page.getAttribute('new_mobiltelefon').setValue(mobilePhone);

    Xrm.Page.getAttribute('new_festnetz').setValue(telephone1);

    }

    Thanks,

    Shahbaaz

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Retrieve fields from related entity using Jscript II

    Hello Harald,

    Seems you are using old way to retrieve the data , I would suggest try to retrieve using Web API. Here is the reference you can try

    community.dynamics.com/.../ms-crm-2016-web-api-operations-retrieve-single-or-multiple-records

    Good Luck.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve fields from related entity using Jscript II

    Many thanks Shahbaaz !

    sorry, this didn't help, getting the error :

    TypeError: Cannot read property 'retrieveRecord' of undefined at retrieveContact (roentgenpartnergmbh.crm4.dynamics.com/.../new_projectcontact.js)

    Do you have another idea ?

    Thanks,

    Harald

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Retrieve fields from related entity using Jscript II

    Hi Harald,

    Please try this,

    function retrieveContact() {

    var contact = Xrm.Page.getAttribute('new_projektkontakt').getValue();

    SDK.REST.retrieveRecord("contact", "EMailAddress1,FullName,MobilePhone,Telephone1", null, function(result) {

    var eMailAddress1 = result.EMailAddress1;

    var fullName = result.FullName;

    var mobilePhone = result.MobilePhone;

    var telephone1 = result.Telephone1;

    }, function(error) {

    Xrm.Utility.alertDialog(error.message);

    });

    Xrm.Page.getAttribute('new_name').setValue(fullname);

    Xrm.Page.getAttribute('new_email').setValue(eMailAddress1);

    Xrm.Page.getAttribute('new_mobiltelefon').setValue(mobilePhone);

    Xrm.Page.getAttribute('new_festnetz').setValue(telephone1);

    }

    Thanks,

    Shahbaaz

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

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 Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans