Client Script not working

aagman4
Tera Contributor
aagman4_0-1695632027200.png

 

function onLoad() {
g_form.addInfoMessage('hi');
var arr = [];
var associatedRisks = new GlideRecord('sn_risk_m2m_risk_control');
associatedRisks.addQuery('sn_compliance_control', g_form.getUniqueValue());
associatedRisks.query();
while(associatedRisks.next()){
arr.push(associatedRisks.sn_risk_risk.sys_id.toString());
}
g_form.addInfoMessage('hello');
var inherentRisk = new GlideRecord('sn_risk_advanced_risk_assessment_instance');
inherentRisk.addQuery('risk.sys_id', 'IN' ,arr);
inherentRisk.addQuery('state', '7');
inherentRisk.orderByDesc('final_inherent_score');
inherentRisk.query();
if(inherentRisk.next()){
var riskScore = inherentRisk.summary_inherent_risk_score;
if(riskScore.indexOf('High') > -1){
g_form.setValue('u_overall_risk_rating','High');
}
else if(riskScore.indexOf('Moderate') > -1 || riskScore.indexOf('Medium') > -1){
g_form.setValue('u_overall_risk_rating','Medium');
}
else{
g_form.setValue('u_overall_risk_rating','Low');
}
}
 
This onLoad client script is not working. I know, we cannot use GlideRecord in client scripts. So, I need help to convert this code working. Can someone help to convert this code using script include.
 
Thanks!
5 REPLIES 5

Peter Bodelier
Giga Sage

Hi @aagman4,

 

Try this:

function onLoad() {
	g_form.addInfoMessage('hi');
	var arr = [];
	var associatedRisks = new GlideRecord('sn_risk_m2m_risk_control');
	associatedRisks.addQuery('sn_compliance_control', g_form.getUniqueValue());
	associatedRisks.query(getAssociatedRisks);
	
}

function getAssociatedRisks(associatedRisks){
	while(associatedRisks.next()){
		arr.push(associatedRisks.sn_risk_risk.sys_id.toString());
	}
	g_form.addInfoMessage('hello');
	var inherentRisk = new GlideRecord('sn_risk_advanced_risk_assessment_instance');
	inherentRisk.addQuery('risk.sys_id', 'IN' ,arr);
	inherentRisk.addQuery('state', '7');
	inherentRisk.orderByDesc('final_inherent_score');
	inherentRisk.query(getInherentRisk);
	
}

function getInherentRisk(inherentRisk){
	
	if(inherentRisk.next()){
		var riskScore = inherentRisk.summary_inherent_risk_score;
		if(riskScore.indexOf('High') > -1){
			g_form.setValue('u_overall_risk_rating','High');
		}
		else if(riskScore.indexOf('Moderate') > -1 || riskScore.indexOf('Medium') > -1){
			g_form.setValue('u_overall_risk_rating','Medium');
		}
		else{
			g_form.setValue('u_overall_risk_rating','Low');
		}
	}	
}

 

This can work fine this way, but it would be better to use a GlideAjax next time. Worth reading into 😉


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Its not working.

What is not working?

Are you getting some of the infomessages or not?
Are you sure your queries are correct?
Are your if statements correct? riskScore might be a getDisplayValue instead of your value for example.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Ankur Bawiskar
Tera Patron
Tera Patron

@aagman4 

I would suggest using GlideAjax or query business rule on that table.

What did you start with and where are you stuck?

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