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

Announcements

No record found.

News and Announcements icon
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
    Microsoft Employee 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 202 Super User 2026 Season 1

#2
CU11031447-0 Profile Picture

CU11031447-0 100

#3
NeerajPawar Profile Picture

NeerajPawar 70

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans