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,

please update as this

I made some changes

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){
action.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();
action.setRedirectURL("kb_knowledge.do?sys_id="+current.sys_id);
}
}

Regards
Ankur

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

Thank you for marking my response as helpful.

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

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

Chetan26
Mega Contributor

Hi Ankur

 

Still it loads but the server side code does not work .

 

Regards,

Chetan

Hi,

please share the earlier working code which was only server side

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

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('IS.Kb_retired'));
current.update();
gs.setRedirect("kb_knowledge.do?sys_id="+current.sys_id);
}