Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Retrieve fields from 2 different but related entities and compare them

Posted on by 64

Hi, hoping you can help.

I'm writing a jscript using sdk.rest.retrieverecord. I'm on an entity that has relationship with both Account and Contact.
On both Account and Contact I have a field called User Name. I'm using jscript to retrieve both these values and compare them.

I have a retrieverecords segment for both Account and Contacct and, from testing, I know it's working a bringing back the correct results.

But when i come to compare those two results, the variables have become undefined. I have moved bits of code around, defined the var's in different ways, but I can't get the comparison to work.

It's like the var's become undefined once the code moves on from the retrieverecord segment. This may be expected behaviour, but how do I get round this?

I've pasted the code below.... p.s. I tried to use the insert code function here, but it wouldn't work.

------------------------

var contact = Xrm.Page.getAttribute("xx_signedonbehalfofemployer").getValue();
var organisation = Xrm.Page.getAttribute("xx_employername").getValue();
var contactusername;
var organisationusername;

if (contact != null && organisation != null) {

SDK.REST.retrieveRecord(
contact[0].id,
"Contact",
"adx_identity_username",
null,
function (result) {
contactusername = result.adx_identity_username;
//alert(contactusername); // this alert returns the expected result
},
function (error) {alert(error.message);}
);

SDK.REST.retrieveRecord(
organisation[0].id,
"Account",
"xx_UserName",
null,
function (result) {
organisationusername = result.xx_UserName;
//alert(organisationusername); // this alert returns the expected result
},
function (error) {alert(error.message);}
);

alert(contactusername); // this alert throws up an error, contactusername is not defined
alert(organisationusername); // this alert throws up an error, organisationusername is not defined

//This is what I eventually want to do:
if (contactusername == null && organisationusername != null) {
alert("mismatch");
}


}

--------------------------

Thanks,

Pete.

  • pmrsim Profile Picture
    pmrsim 64 on at
    RE: Retrieve fields from 2 different but related entities and compare them

    Steve, you're a genius!! That works! Thank you so much.

    All the best,

    Pete.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve fields from 2 different but related entities and compare them

    Hi petesimons,

    I think this is caused by SDK.REST.retrieveRecord() method is asyn method.

    When you alert contactusername, this line contactusername = result.adx_identity_username; hasn't been executed. So you get undefined value.

    You can use Promise(developer.mozilla.org/.../Promise) to have a try

    Or 

    compare them in success callback function:

    var contact = Xrm.Page.getAttribute("xx_signedonbehalfofemployer").getValue();
    var organisation = Xrm.Page.getAttribute("xx_employername").getValue();
    var contactusername;
    var organisationusername;

    if (contact != null && organisation != null) {

    SDK.REST.retrieveRecord(
    contact[0].id,
    "Contact",
    "adx_identity_username",
    null,
    function (result) {
    contactusername = result.adx_identity_username;
    //alert(contactusername); // this alert returns the expected result

    SDK.REST.retrieveRecord(
    organisation[0].id,
    "Account",
    "xx_UserName",
    null,
    function (result) {
    organisationusername = result.xx_UserName;
    //alert(organisationusername); // this alert returns the expected result
    alert(contactusername); // this alert throws up an error, contactusername is not defined
    alert(organisationusername); // this alert throws up an error, organisationusername is not defined

    //This is what I eventually want to do:
    if (contactusername == null && organisationusername != null) {
    alert("mismatch");
    }
    },
    function (error) {alert(error.message);}
    );
    },
    function (error) {alert(error.message);}
    );
    }

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,030 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans