Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Answered

checking for null JavaScript

Posted on by 160

Hi,

I want to create salutation text for field "salutation for mail" on Contact's form. I have a field which name is "salutation" and is a OptionSet field. If one option is selected, then the field "salutation for mail" should be filled out with combination of firstname, lastname and Titel. 

But these fields ( firstname, lastname and Titel) can be empty. 

if i check it in a condition like below, i get "null" in field "salutation for mail" if these fields are empty:

 if (Salutation == "" && firstName != null && Titel != null){
formContext.getAttribute("Salutationformail").setValue(Salutation+" "+Titel+" "+firstName+" "+lastName);
}

Does anybody have an idea how can i check these fields for null value so that i dont get null if these are empty?

Thanks

Best Regards!

  • Elpibato Profile Picture
    Elpibato 160 on at
    RE: checking for null JavaScript

    It works perfect!! exactly what i want.

    Thank you so much!

    Regards,

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: checking for null JavaScript

    Hi Elpibato,

    Do you mean there is an extra space between salutation and lastName, right?

    Please try this code:

    if(Salutation == "yourValue"){
        Title = Title != null?" "   Title:"";
    	firstName = firstName != null?" "  firstName:"";
    	formContext.getAttribute("Salutationformail").setValue(Salutation Title firstName " " lastName);
    }

  • Elpibato Profile Picture
    Elpibato 160 on at
    RE: checking for null JavaScript

    Thnaks, it works!

    But i have just one problem, if i use this code and titel is empty then i get an empty string between salutation and lastName like this: "Dear   Lopez".

    Do you have any idea why it seems like this?

    Best Regrads,

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: checking for null JavaScript

    Hi Elpibato,

    I see.

    You can try the following code:

    if(Salutation == "yourValue"){
        Title = Title != null?Title:"";
    	firstName = firstName != null?firstName:"";
    	formContext.getAttribute("Salutationformail").setValue(Salutation " " Title " " firstName " " lastName);
    }

  • Elpibato Profile Picture
    Elpibato 160 on at
    RE: checking for null JavaScript

    Thanks for your answer!

    I mean if I use this code then i get like this: "Dear Mr. null Lopez"

    I dont now how can I avoid this problem if one of these field is empty?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: checking for null JavaScript

    Hi Elpibato,

    I'm not sure what does 'Does anybody have an idea how can i check these fields for null value so that i dont get null if these are empty?' mean.

    Do you mean that if firstName and lastName are empty, you don't want to get "null" in field "salutation for mail"? So which value do you want to get?

    According to the code, you didn't do anything when firstName and lastName are empty. If I misunderstand your question, please correct me.

  • Suggested answer
    MikeC282 Profile Picture
    MikeC282 2,095 on at
    RE: checking for null JavaScript

    So you want to set the salutation if all three fields are not null right? You got the condition wrong. It should be:

    if (Salutation !== null && Salutation !== undefined && firstName !== null && firstName !== undefined && Titel != null && Titel != undefined) {

    formContext.getAttribute("Salutationformail").setValue(Salutation+" "+Titel+" "+firstName+" "+lastName);

    }

    If this has answered your question please mark it as the answer.

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans