Need Assistance with Multiple If Statements

Ishenferi
Tera Contributor

Hello SN Community!

 

I'm trying to script a UI action. Basically we have a user clicking the UI Action to check mandatory fields and send for approval. The script looks like this:

 

if (current.field1 == '') {
    gs.addInfoMessage("Please populate Field 1.");
    action.setRedirectURL(current);
}
if (current.field2 == '') {
    gs.addInfoMessage("Please populate Field 2.");
    action.setRedirectURL(current);
}
if (current.field3 == '') {
    gs.addInfoMessage("Please populate Field 3.");
    action.setRedirectURL(current);
}
if (current.field4 == '') {
    gs.addInfoMessage("Please populate Field 4.");
    action.setRedirectURL(current);
} else {
    gs.addInfoMessage("Record sent for approval.");
    current.status = '8';
    current.update();
    action.setRedirectURL(current);
}
 
And the majority of this works, however there is a hole where if I were to enter JUST Field 4 and click the UI Action, it will send the record for approval and also give notifications for Field1, 2, and 3.
 
I'll also include that I am in no way a formal developer, so if there is a simpler, cleaner way to script this I am very open to suggestions!
 
Thanks!
1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Try this. This will check one field at a time and only add the error on one.

 

 

if (current.field1 == '') {
    gs.addInfoMessage("Please populate Field 1.");
    action.setRedirectURL(current);
}
else if (current.field2 == '') {
    gs.addInfoMessage("Please populate Field 2.");
    action.setRedirectURL(current);
}
else if (current.field3 == '') {
    gs.addInfoMessage("Please populate Field 3.");
    action.setRedirectURL(current);
}
else if (current.field4 == '') {
    gs.addInfoMessage("Please populate Field 4.");
    action.setRedirectURL(current);
} else {
    gs.addInfoMessage("Record sent for approval.");
    current.status = '8';
    current.update();
    action.setRedirectURL(current);
}
 

 

If you want to check all blank fields and add all messages then try this

if (current.field1 == '') {
    gs.addInfoMessage("Please populate Field 1.");
}
if (current.field2 == '') {
    gs.addInfoMessage("Please populate Field 2.");
}
if (current.field3 == '') {
    gs.addInfoMessage("Please populate Field 3.");
}
if (current.field4 == '') {
    gs.addInfoMessage("Please populate Field 4.");
} else {
    gs.addInfoMessage("Record sent for approval.");
    current.status = '8';
    current.update();
    action.setRedirectURL(current);
}
     action.setRedirectURL(current);

 

-Anurag

View solution in original post

5 REPLIES 5

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Try this. This will check one field at a time and only add the error on one.

 

 

if (current.field1 == '') {
    gs.addInfoMessage("Please populate Field 1.");
    action.setRedirectURL(current);
}
else if (current.field2 == '') {
    gs.addInfoMessage("Please populate Field 2.");
    action.setRedirectURL(current);
}
else if (current.field3 == '') {
    gs.addInfoMessage("Please populate Field 3.");
    action.setRedirectURL(current);
}
else if (current.field4 == '') {
    gs.addInfoMessage("Please populate Field 4.");
    action.setRedirectURL(current);
} else {
    gs.addInfoMessage("Record sent for approval.");
    current.status = '8';
    current.update();
    action.setRedirectURL(current);
}
 

 

If you want to check all blank fields and add all messages then try this

if (current.field1 == '') {
    gs.addInfoMessage("Please populate Field 1.");
}
if (current.field2 == '') {
    gs.addInfoMessage("Please populate Field 2.");
}
if (current.field3 == '') {
    gs.addInfoMessage("Please populate Field 3.");
}
if (current.field4 == '') {
    gs.addInfoMessage("Please populate Field 4.");
} else {
    gs.addInfoMessage("Record sent for approval.");
    current.status = '8';
    current.update();
    action.setRedirectURL(current);
}
     action.setRedirectURL(current);

 

-Anurag

Ah, the else ifs worked perfectly, thank you!

JoeSD
Tera Expert

Hi  Ishenferi,

 

Are these fields already flagged as mandatory? 

 

If not, you could use a UI policy instead to make these mandatory instead of checking this in your UI action.

 

Thank you,

Joe

Ishenferi
Tera Contributor

The issue was they want no mandatory fields in a Draft state, and only when clicking the UI Action do they become mandatory.