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.
Steve, you're a genius!! That works! Thank you so much.
All the best,
Pete.
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);}
);
}
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,188 Super User 2024 Season 2
Martin Dráb 230,030 Most Valuable Professional
nmaenpaa 101,156