Need help with OnLoad modal in portal

john_duchock
Kilo Guru

I am trying to create a portal pop-up window (modal) that displays when a user accesses a CLOSED task record in portal (incident, request item, change, etc.).  This modal should instruct the user that the record they are accessing is closed and provide them with instructions on what to do next.

I am unable to craft the proper onLoad client script to trigger the modal.  I am wondering if anyone else has something similar they could share...

Thanks

 

1 ACCEPTED SOLUTION

Here you go.  This is tested an working on an INC record.

Server Script:

(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */

var id = '';
if(input.action === 'checkRec'){
id = $sp.getParameter('sys_id');
}

data.active_record = isRecordActive(id);

function isRecordActive(sysID){
var active = '';
var task = new GlideRecord('task');
task.get(sysID);
active = task.active;
return active.toString();
}

})();

 

Client Script:

function(spModal) {
/* widget controller */
var c = this;

var activeRecord = '';

c.server.get({
action: 'checkRec'
}).then(function(response){
activeRecord = response.data.active_record;

if(activeRecord == 'false'){
spModal.open({
title: 'This is a test title for false record',
message: 'This is a test message.',
buttons: [{label:'OK', primary:true}]
});
}
});

}

 

View solution in original post

11 REPLIES 11

Might need to tweak this.  Give me a bit.  I'm close.

Here you go.  This is tested an working on an INC record.

Server Script:

(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */

var id = '';
if(input.action === 'checkRec'){
id = $sp.getParameter('sys_id');
}

data.active_record = isRecordActive(id);

function isRecordActive(sysID){
var active = '';
var task = new GlideRecord('task');
task.get(sysID);
active = task.active;
return active.toString();
}

})();

 

Client Script:

function(spModal) {
/* widget controller */
var c = this;

var activeRecord = '';

c.server.get({
action: 'checkRec'
}).then(function(response){
activeRecord = response.data.active_record;

if(activeRecord == 'false'){
spModal.open({
title: 'This is a test title for false record',
message: 'This is a test message.',
buttons: [{label:'OK', primary:true}]
});
}
});

}

 

Hi John,

 

Were trying to display a modal on load when viewing an article that is NOT Published. I can display the Confirm modal, but when I click ok or cancel, it does this (See the line inside the RED bracket)
 
Is there a way to return to the article, after they click OK?
 
 
find_real_file.png
 
 
 
 
HTML: Empty
Server Side:
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
 
//var article_sysid = $sp.getParameter('sys_kb_id');
//data.isNotPublished = false;
//gs.log("JMC Modal sysid: " + article_sysid);

var id;
if(input.action === 'checkRec'){
id = $sp.getParameter('sys_kb_id');
}
 
data.isNotPublished = isNotPublished(id);
data.status;

function isNotPublished(article_sysid){
//Check if the article state is Draft or Retired
var gkb = new GlideRecord('kb_knowledge');
gkb.addQuery('sys_id',article_sysid);
gkb.query();
 
if(gkb.next())
{
data.status = gkb.workflow_state;
//gs.log("JMC Modal record found! " + article_sysid);
if(gkb.workflow_state != 'published')
{
return true;
}
else
return false;

}
 

}
gs.log("JMC Modal isDraftOrRetired: " + data.isNotPublished + ", workflow state: " + data.status);

})();
 
 
 
Client Side:
function(spModal) {
/* widget controller */
var c = this;
 
var isNotPublished;
var status;
 
c.server.get({
action: 'checkRec'
}).then(function(response)
{
isNotPublished = response.data.isNotPublished;
status = response.data.status;

if(isNotPublished){
// spModal.open({
// message: 'This is a test message.',
// buttons: []
// });
spModal.confirm("This article is in " + c.data.status + " status."
);
}
});
 
}

 

I was also trying to dynamically display the status of the article on the confirmation itself, is that possible?

 

Thank you!

You were really close.  You are getting a blank modal because you don't need both spModal.open and spModal.confirm just one or the other.  API docs has good examples here, https://developer.servicenow.com/dev.do#!/reference/api/quebec/client/SPModal-API ,  but I also included example below.  I changed around some of the vars and what not but I hope this helps.   It is possible to dynamically display the KB state, also tested this in Rome in a personal dev and is working.  I included a redirect example to bring the user back to kb_home I'm just not sure where you want to navigate them or why someone would be clicking on a retired article but it should be possible using any client side redirect.  You can also concatenate a url somehow if that's what you are looking for.

 

SERVER

(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */

//var article_sysid = $sp.getParameter('sys_kb_id');
//data.isNotPublished = false;
//gs.log("JMC Modal sysid: " + article_sysid);

var id;
//if(!input && input.action === 'checkRec'){
id = $sp.getParameter('sys_id');
//}

data.workflowState = workflowState(id);

data.published = true;

if(data.workflowState != 'Published'){
data.published = false;
}

function workflowState(article_sysid){
var arr = [];
//Check if the article state is Draft or Retired
var gkb = new GlideRecord('kb_knowledge');
gkb.addQuery('sys_id',article_sysid);
gkb.query();

if(gkb.next())
{
return gkb.workflow_state.getDisplayValue();
//gs.log("JMC Modal record found! " + article_sysid);
}
}
gs.info("JMC Modal State: " + data.workflowState + ", Published? " + data.published);

})();

 

CLIENT

function(spModal, $location) {
/* widget controller */
var c = this;

c.confirmed = "asking";
var warn = '<i class="fa fa-warning" aria-hidden="true"></i>';
spModal.open({
title: 'Knowledge Article',
message: warn + ' This article is in ' + c.data.workflowState + ' status.'
}).then(function(confirmed) {
c.confirmed = confirmed;
$location.search('id=kb_home')
})

}

Thank you, John! Will try this out!