The CreatorCon Call for Content is officially open! Get started here.

UI Action Scripting

devr
Kilo Contributor

Hi All

I am trying to write UI Action script for below scenario:

when Demand manager go to an existing   demand that is in draft status and do NOT fill in any/all   of the below fields and click on"Submit demand"   button then there should be an error message that reads "The following mandatory fields are not filled in :   <field name1> , <field name2>":
Business Unit
Primary Queue
Value Driver
Proposed Start Year

I have written UI Action as:

Table: IT Demand [tsp1_demand]

Active: True

Show update: True

Onclick: checkRequired()

Client: True

Condition: current.state==1 && gs.hasRole('demand_manager')

Script:

function checkRequired() {
var bunit = g_form.getValue('u_portfolio_bu');
var pqueue = g_form.getValue('u_primary_queue');
var vdriver = g_form.getValue('u_initiative');
var psuser = g_form.getValue('u_start_year');}
if (bunit == '' && pqueue == '' && vdriver == '' && puser == '') {
g_form.setMandatory('u_portfolio_bu', true);
g_form.setMandatory('u_primary_queue', true);
g_form.setMandatory('u_initiative', true);
g_form.setMandatory('u_start_year', true);
g_form.showFieldMsg('u_portfolio_bu','u_primary_queue','u_initiative', 'u_start_year','is required to submit.', 'error');
}

Above script is not working, user is able to Sumbit demand and there is no error message.

This is the first time I am writing script, please help me in correcting this script.

Thanks and Regards

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

why did you close the function block. before the if condition.



function checkRequired() {


var bunit = g_form.getValue('u_portfolio_bu');


var pqueue = g_form.getValue('u_primary_queue');


var vdriver = g_form.getValue('u_initiative');


var psuser = g_form.getValue('u_start_year');


if (bunit == '' && pqueue == '' && vdriver == '' && puser == '') {


g_form.setMandatory('u_portfolio_bu', true);


g_form.setMandatory('u_primary_queue', true);


g_form.setMandatory('u_initiative', true);


g_form.setMandatory('u_start_year', true);


g_form.showFieldMsg('u_portfolio_bu','u_primary_queue','u_initiative', 'u_start_year','is required to submit.', 'error');


}


gsftSubmit(null, g_form.getFormElement(), '<ui Action action name>');     //please mention your ui action "action name"


}


View solution in original post

6 REPLIES 6

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello,



Please refer the below blog and adjust your code accordingly.


https://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/


Shishir Srivast
Mega Sage

Please use the return false; to stop the form submission.


Also I think showFieldMsg() syntax is wrong, please refer: http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#showFieldMsg


Harsh Vardhan
Giga Patron

why did you close the function block. before the if condition.



function checkRequired() {


var bunit = g_form.getValue('u_portfolio_bu');


var pqueue = g_form.getValue('u_primary_queue');


var vdriver = g_form.getValue('u_initiative');


var psuser = g_form.getValue('u_start_year');


if (bunit == '' && pqueue == '' && vdriver == '' && puser == '') {


g_form.setMandatory('u_portfolio_bu', true);


g_form.setMandatory('u_primary_queue', true);


g_form.setMandatory('u_initiative', true);


g_form.setMandatory('u_start_year', true);


g_form.showFieldMsg('u_portfolio_bu','u_primary_queue','u_initiative', 'u_start_year','is required to submit.', 'error');


}


gsftSubmit(null, g_form.getFormElement(), '<ui Action action name>');     //please mention your ui action "action name"


}


Shishir Srivast
Mega Sage

Also, writing the Hello Love Me is quite fascinating