Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Javascript - how to add "if null" condition

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

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Javascript - how to add "if null" condition

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

  • RE: Javascript - how to add "if null" condition

    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans