Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Using confirm popup alert on UI action

Chetan26
Mega Contributor

I have a button on a form " Retire" that I would like to add a confirmation popup box for when it is pressed. 

 

confirm("Are you sure you want to retire the KB Article?")

curently the alert comes up but it does not work  when I hit OK.

 

I have checked this article but my Server side code does not seem to work , Can some one help me this 

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

 

 

The UI action Code 

function RetireKB(){
//Getting confirmation to cancel
var answer=confirm("Are you sure you want to cancel this record?");
if (answer==true){//If 'ok' is pressed, proceed
gsftSubmit(null, g_form.getFormElement(),'retire'); //MUST call the 'Action name' set in this UI Action
}
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
serverCancel();
function serverCancel(){


if(new KBKnowledge().canRetire(current)){

if (current.kb_knowledge_base.kb_version == "3" && new KBWorkflow().startWorkflow(current, "retire_workflow")) {
if(current.workflow_state != 'retired'){
current.update();
}
gs.addInfoMessage(new KBKnowledge().getStateMessage(current.getValue("workflow_state")));
} else if (current.kb_knowledge_base.kb_version == "2") {
current.workflow_state = 'retired';
current.update();
gs.addInfoMessage(new KBKnowledge().getStateMessage(current.getValue("workflow_state")));
}
if(typeof RP !== 'undefined'){
if(RP.getParameterValue("sysparm_referring_url").indexOf('kb_view.do') >= 0){
gs.setRedirect("kb_view.do?sys_kb_id=" + current.sys_id);
}
}
}
else if(current.workflow_state == 'published'&&gs.getProperty('fis.domain')&&(gs.getUser().hasRole('itil') || gs.getUser().hasRole('knowledge_coach'))){
current.workflow_state = 'retired';
gs.addInfoMessage(gs.getMessage('FIS.Kb_retired'));
current.update();
gs.setRedirect("kb_knowledge.do?sys_id="+current.sys_id);
}
}

 

11 REPLIES 11

Hi,

So considering this was working fine

Just making it this should also work

I assume you have this configured

Client - true

OnClick - RetireKB

Action name - retire

Script:

function RetireKB(){
//Getting confirmation to cancel
var answer = confirm("Are you sure you want to cancel this record?");
if (answer == true){//If 'ok' is pressed, proceed
gsftSubmit(null, g_form.getFormElement(),'retire'); //MUST call the 'Action name' set in this UI Action
}
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
serverCancel();

function serverCancel(){
if(new KBKnowledge().canRetire(current)){
if (current.kb_knowledge_base.kb_version == "3" && new KBWorkflow().startWorkflow(current, "retire_workflow")) {
if(current.workflow_state != 'retired'){
current.update();
gs.addInfoMessage(new KBKnowledge().getStateMessage(current.getValue("workflow_state")));
}
} else if (current.kb_knowledge_base.kb_version == "2") {
current.workflow_state = 'retired';
current.update();
gs.addInfoMessage(new KBKnowledge().getStateMessage(current.getValue("workflow_state")));
}
if(typeof RP !== 'undefined'){
if(RP.getParameterValue("sysparm_referring_url").indexOf('kb_view.do') >= 0){
gs.setRedirectURL("kb_view.do?sys_kb_id=" + current.sys_id);
}
}
}
else if(current.workflow_state == 'published'&&gs.getProperty('fis.domain')&&(gs.getUser().hasRole('itil') || gs.getUser().hasRole('knowledge_coach'))){
current.workflow_state = 'retired';
gs.addInfoMessage(gs.getMessage('FIS.Kb_retired'));
current.update();
gs.setRedirectURL("kb_knowledge.do?sys_id="+current.sys_id);
}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Chetan26
Mega Contributor

Hi, 

yes this is the configuration I am using, but it still did not work

is there any other way of doing it?