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

That makes sense. You can use UI policies to only make those fields mandatory when they are not in a draft state. 

 

In the example below, the On Hold Reason field is only mandatory when the state is On Hold. You can use this same method but change the criteria to is no Draft to get the result you're looking for.

 

JoeSD_0-1715713672785.png