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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Javascript - how to add "if null" condition

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Esteban Coto Alfaro Profile Picture
    on at

    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.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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) {

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans