I am quite new to javascript and got a question which is probably simple to many :-)
I got a Javascript on Incidents where I retrieve a value from related entiy new_Agreement.
The lookupfield new_Agreement is set by the "mapping function" and therefore I want to run the Javascript on load.
So the value in new_reference from Agreement is set in to similar field on the Incident record.
However, the user should be able to overwrite the new value, so therefore I only want to run the js function if the new_reference on Incident is null. How do I add this condition to the javascript ?
function RetrieveReference (executionContext) {
var formContext = executionContext.getFormContext();
if (formContext.getAttribute("new_agreement").getValue()) {
var id = formContext.getAttribute("new_agreement").getValue()[0].id;
Xrm.WebApi.retrieveRecord("new_agreement", id, "?$select=new_reference").then(
function success(result) {
formContext.getAttribute("new_reference").setValue(result.new_reference);
},
function (error) {
console.log(error.message);
});
}
}
Best Regards,
Dorthe
Hello,
Try to replace line
if (formContext.getAttribute("new_agreement").getValue()) {
with line
if (formContext.getAttribute("new_agreement").getValue() != null && formContext.getAttribute("new_reference").getValue() == null) {
Hello, just add on the if condition a "!= null".
Like this:
function RetrieveReference(executionContext) {
var formContext = executionContext.getFormContext();
if (formContext.getAttribute("new_agreement").getValue() != null) {
var id = formContext.getAttribute("new_agreement").getValue()[0].id;
Xrm.WebApi.retrieveRecord("new_agreement", id, "?$select=new_reference").then(
function success(result) {
formContext.getAttribute("new_reference").setValue(result.new_reference);
},
function (error) {
console.log(error.message);
});
}
}
Thanks!
Community Support Team - Esteban
If this Post helps, then please consider Accept as solution to help the other members find it more quickly.
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,228 Super User 2024 Season 2
Martin Dráb 230,056 Most Valuable Professional
nmaenpaa 101,156