Set field mandatory using Script Include and Client script

Alon Grod
Tera Expert

Hi,

 

Im trying to set the field Operational Impact as mandatory using Script include and on load client script.

I set an Alert in order to see what Im getting from the script include and lm getting 'false' even tho the conditions are met and I suppose to get true. 

What am I doing wrong?

Screen Shot 2023-01-16 at 10.42.28.png

Screen Shot 2023-01-16 at 10.43.34.png

1 ACCEPTED SOLUTION

This might be easier with a display business rule which does a GlideAggregate look-up and sets a property in the g_scratchpad object.

The script for your display BR would look something like this:

 

 

(function executeRule(current, previous /*null when async*/ ) {

    var department = current.caller_id.department;
    var createdOnDate = current.getValue('sys_created_on').split(' ')[0]; // Gets only the Date as string
	

    var agg = new GlideAggregate('u_operational_activity');
    agg.addQuery('u_departments', department);
    agg.addQuery('u_from_date', '<=', createdOnDate );
    agg.addQuery('u_to_date', '>=', createdOnDate );
    agg.setLimit(1);
    agg.query();
    if (agg.hasNext()) {
        g_scratchpad.operationalActivity = true;
    } else {
        g_scratchpad.operationalActivity = false;
    }

})(current, previous);

 

 

 

And then your client script can be really simple, no need for Script Include or GlideAjax:

 

 

function onLoad() {

    if(g_scratchpad.operationalActivity) {
		g_form.setMandatory('u_operational_impact', true);
	} else {
		g_form.setMandatory('u_operational_impact', false);
	}

}

 

 

 

Please review the code as I didn't have the time to test it, especially that I don't have your exact same custom table either.

View solution in original post

14 REPLIES 14

@Basheer both are referenced fields (in incident and in oprerational activity tables)

@Basheer what should I do if both referenced fields

Try these scripts in background and let me know if any sys_id is matched

 

First Script : It will print you the sys_ids of department 

var inc = new GlideRecord("incident");
inc.query();
while(inc.next()){
gs.print(inc.caller_id.department +" Department of incident");
}
 
Second script
var oper = new GlideRecord("u_operational_activity");
oper.query();
while(oper.next()){
gs.print(oper.u_departments + " Department in operational activity);
}
Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

@Basheer Im not getting anything from inc.caller_id.department

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

isService: function() {

var inc = new GlideRecord("incident");
inc.addQuery('active=true');
inc.query();
while (inc.next()) {
var gdt = new GlideDateTime(inc.sys_created_on);
var date = gdt.getDate();
gs.log('alon' + date);
gs.log('inc dept:'+inc.caller_id.department);
var oper = new GlideRecord('u_operational_activity');
oper.addEncodedQuery('u_departments=' + inc.caller_id.department + '^u_from_date<=' + date + '^u_to_date>=' + date);
oper.query();
return oper.hasNext();

}
},

 

Screen Shot 2023-01-16 at 13.09.50.png

type: 'OperUtil'
});

@jaheerhattiwale