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,...
Answered

checking for null JavaScript

(0) ShareShare
ReportReport
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!

I have the same question (0)
  • Suggested answer
    MikeC282 Profile Picture
    2,175 on at

    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.

  • Community Member Profile Picture
    on at

    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.

  • Elpibato Profile Picture
    160 on at

    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?

  • Verified answer
    Community Member Profile Picture
    on at

    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
    160 on at

    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
    on at

    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
    160 on at

    It works perfect!! exactly what i want.

    Thank you so much!

    Regards,

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 171 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 83

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans