GlideAjax issue getAttribute("answer")

Luca9
Tera Contributor

Hi,

On Indicator Task form I have to set result = Passed when state changes and if attachments are present.

I created a client script on sn_grc_indicator_task table and a script include but I cant understand why it doesn't work. Somebody helps me?

I try to create client script and script include in GRC: Profile and Global scopes but nothing changed.

client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var targetType = g_scratchpad.target_type;
if (targetType != 'percentage' && targetType != 'count') {
var ga = new GlideAjax('Count_attachment');
ga.addParam('sysparm_name', 'getRowCount');
ga.addParam('table_name', g_form.getTableName());
ga.addParam('table_sys_id', g_form.getUniqueValue());
ga.getXML(updateResult);

function updateResult(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
jslog('answer' + answer); // answer is null

if (answer) {
var returneddata = answer.evalJSON(true);
var rowcount = returneddata.rowcount;
jslog('rowcount '+rowcount);
if(rowcount >0)
g_form.setValue('result', 'passed');

} else {
g_form.setValue('result', 'failed');
}
}
}
}

script include

name: Count_attachment

client callable: True

script:

var Count_attachment = Class.create();
Count_attachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRowCount: function() {
var rowcount = 0;
var table_name = this.getParameter('table_name');
var table_sys_id = this.getParameter('table_sys_id');
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", table_name);
attachment.addQuery("table_sys_id", table_sys_id);
attachment.query();
while (attachment.next()) {
rowcount++;
}
var results = {
"rowcount": rowcount
};
return json.encode(results);
},

//type: 'Count_attachment'
});

 

 

 

1 ACCEPTED SOLUTION

Maik Skoddow
Tera Patron
Tera Patron

Hi

in your Script Include there is an error at the end. Instead of

 return json.encode(results);

write

 return JSON.stringify(results);

 

In case there are more errors, I recommend using my GlideAjax Test Client

Kind regards
Maik

View solution in original post

4 REPLIES 4

Maik Skoddow
Tera Patron
Tera Patron

Hi

in your Script Include there is an error at the end. Instead of

 return json.encode(results);

write

 return JSON.stringify(results);

 

In case there are more errors, I recommend using my GlideAjax Test Client

Kind regards
Maik

Ahmed Drar
Tera Guru
Tera Guru

Hi, I don't think you need a client script and ajax to achieve that. you can simply create a business rule that runs on state changes and a GlideAggrergate attachment table to see whether your indicator has attachment

 

	var IndGA = new GlideAggregate('sys_attachement');
	IndGA.addEncodedQuery('table_name=sn_grc_indicator_task^table_sys_id=' + current.sys_id);
	IndGA.addAggregate('COUNT');
	IndGA.query();
	var IndAttach = 0;
	if (IndGA.next())
	 IndAttach = count.getAggregate('COUNT');

	if (IndAttach > 0) {
	    current.result ='pass';
	}

Please mark my answer as ✅ Correct / Helpful based on the Impact.

Luca9
Tera Contributor

Hi Ahmed,

in my case I do not have to set value in the table, but only in the form in order to permit users to change the value.

Thank you for your contribution!

that makes sense if you want to do it on the client-side.