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

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Retrieve fields from 2 different but related entities and compare them

(0) ShareShare
ReportReport
Posted on by 34

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.

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    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);}
    );
    }

  • pmrsim Profile Picture
    34 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.

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 61 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 52 Most Valuable Professional

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 52 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans