How to return JSON from Script Include

jstocton
Kilo Expert

I have a situation where multiple records can be selected and then a List UI Action can be invoked.  I am passing the list of sys_ids from getChecked() to GlideAjax to be processed on the Server Side.  I am matching records from multiple tables and getting a value from the second table.  It is a true/false field.

IF the answer comes back false, then a UI Page will be invoked for input and update of the selected records.

IF the answer comes back true, then I need to pop a message so that the selected records can not be updated.

This is all working unless I select multiple records and the answer for those records differs.

I think I need to return JSON to the UI Action Client Side and then Parse somehow to see if "true" exists in order to pop the message...but the somehow evades me!!!

Any assistance would be most helpful.  Thanks!!

 

Here is the current Client Script:

function Prevent_Remove() {
tblName = g_list.getTableName();
selSysIds = g_list.getChecked();
var selected_sys_ids = g_list.getChecked();

var ajax = new GlideAjax("mustInclude");
ajax.addParam("sysparm_name","delMustInclude");
ajax.addParam("selected_sys_ids", selected_sys_ids);
ajax.getXML(removeIt);
}

function removeIt (response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert('The Answer is set to: ' +answer);

if(answer == 'false') {
// Initialize and open the Dialog Window
var dialog = new GlideDialogWindow("slc_remove_dialog_window");
dialog.setTitle('Choose your removal reason');
dialog.setPreference("selected_sys_ids", selSysIds);
dialog.setSize(400,300);
dialog.render(); //Open the dialog 
}
else{
alert('You have chosen a Deliverable which can not be removed');
}

}

Here is the current Script Include:

var mustInclude = Class.create();
mustInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

delMustInclude : function () {
var sys_ids = this.getParameter('selected_sys_ids');

var m2m_deliverablesGR = new GlideRecord('u_slc_m2m_deliverables');
m2m_deliverablesGR.addQuery('sys_id', 'IN', sys_ids);
m2m_deliverablesGR.query();

while(m2m_deliverablesGR.next()){
var m2m_del = m2m_deliverablesGR.u_slc_deliverables.getDisplayValue();

//I need to get / match information from a separate table
var slc_deliverableGR = new GlideRecord('u_slc_deliverables');
slc_deliverableGR.get('u_name', m2m_del);
slc_deliverableGR.query();
var delName = slc_deliverableGR.u_name;
var mi = slc_deliverableGR.u_slc_must_include;

while(slc_deliverableGR.next()){
if(mi == true){
return true;
}
else {
return false;
}

}

}
},
type: 'mustInclude'

});

 

 

1 ACCEPTED SOLUTION

jstocton
Kilo Expert

I figured it out!!!!  I was only processing the first record that came back as true and then exiting/returning out of my while...

 

Script Include changed as follows for the fix:

var mustInclude = Class.create();
mustInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

delMustInclude : function () {
var flag2send = false;
var sys_ids = this.getParameter('selected_sys_ids');
var m2m_deliverablesGR = new GlideRecord('u_slc_m2m_deliverables');

m2m_deliverablesGR.addQuery('sys_id', 'IN', sys_ids);
m2m_deliverablesGR.query();


while(m2m_deliverablesGR.next()){
var m2m_del = m2m_deliverablesGR.u_slc_deliverables.getDisplayValue();


//I need to get / match information from a separate table
var slc_deliverableGR = new GlideRecord('u_slc_deliverables');
slc_deliverableGR.get('u_name', m2m_del);
slc_deliverableGR.query();
var delName = slc_deliverableGR.u_name;
var mi = slc_deliverableGR.u_slc_must_include;

while(slc_deliverableGR.next()){
if(mi == true){
flag2send = true;
}

}

}
return flag2send;
},
type: 'mustInclude'

});

View solution in original post

4 REPLIES 4

Priyanka Gupta
Mega Guru

Have you checked what value are you getting in 'sys_ids' in 'Script Include' when you select multiple?

Thanks for jumping in and trying to help.  But I figured it out in a much simpler way than I thought I was going to have to do...

Cool, thats good.

jstocton
Kilo Expert

I figured it out!!!!  I was only processing the first record that came back as true and then exiting/returning out of my while...

 

Script Include changed as follows for the fix:

var mustInclude = Class.create();
mustInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

delMustInclude : function () {
var flag2send = false;
var sys_ids = this.getParameter('selected_sys_ids');
var m2m_deliverablesGR = new GlideRecord('u_slc_m2m_deliverables');

m2m_deliverablesGR.addQuery('sys_id', 'IN', sys_ids);
m2m_deliverablesGR.query();


while(m2m_deliverablesGR.next()){
var m2m_del = m2m_deliverablesGR.u_slc_deliverables.getDisplayValue();


//I need to get / match information from a separate table
var slc_deliverableGR = new GlideRecord('u_slc_deliverables');
slc_deliverableGR.get('u_name', m2m_del);
slc_deliverableGR.query();
var delName = slc_deliverableGR.u_name;
var mi = slc_deliverableGR.u_slc_must_include;

while(slc_deliverableGR.next()){
if(mi == true){
flag2send = true;
}

}

}
return flag2send;
},
type: 'mustInclude'

});