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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

changes to your code

Rest all looks good

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

Regards
Ankur

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

thanks for your response

 

the form load but the state does not update still .

Hi,

it means some issue in your server side code

Was it working fine previously?

was the UI action previously only server side and was working fine

Regards
Ankur

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

yes it was only server side & was working fine .