UI action Alert to fill "Comments" field

Chinmayee1
Giga Contributor

I have a record producer , where I have a "Reject" Button . When I click on Reject the "status " field changes to "Rejected" . requirement is to add a validation , if "comments" field is empty then It will not change the state and there will be an alert saying "please enter the reason in comments field".

Below is my code  which doesn't work .

if(g_form.getValue ('u_comments') == "")
{
gs.addErrorMessage('please enter the reason in comments field' );
action.setRedirectURL(current);

}
else {
current.u_state="Rejected";

current.update();

gs.addInfoMessage('You have Rejected the Request' );
action.setRedirectURL(current);

}

I have also tried if(current.u_comments== "") that didn't work too .

 

Please help me on this

3 REPLIES 3

Snehal2
Kilo Guru

Hi there,

 Try out this one... 

if(g_form.getValue ('u_comments') == ''){

gs.addErrorMessage('please enter the reason in comments field' ); 

return false;   //this will abort the action
}

Mark it correct if worked for u as well so that it will be helpful to others as well

 

Regards,

Snehal M

Alikutty A
Tera Sage

Hello,

You can add a before update business rule for this scenario just as how it works OOB. 

There is an OOB business rule which does the same on Approval table, Please refer to "Make comments required" on sysapproval_approver table.

URL: Replace your instance_name

https://instance_name.service-now.com/nav_to.do?uri=sys_script.do?sys_id=21b4e945d7010300bbc783e80e6...

 
Condition: State changes to Rejected

Condition String: current.comments.nil()

Script: 

(function executeRule(current, previous /*null when async*/) {

gs.addErrorMessage(gs.getMessage('please enter the reason in comments field'));
current.setAbortAction(true);

})(current, previous);

 

find_real_file.png

 

Devyani_6
Mega Guru

Hi,

Try with the below script in UI action.

Do the following configuration:

Client checkbox: unchecked

Form Button: Checked

if(current.description.nil())         // put you field name instead of description
{
    gs.addErrorMessage(gs.getMessage('please enter the reason in comments field'));
    current.setAbortAction(true);
    action.setRedirectURL(current);
}
else		
{
    current.state = '6';           // put backend value of choice you want to set
    current.update();
    action.setRedirectURL(current);
			
}

Mark Correct/Helpful, if this helps you.

Regards,

Devyani