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

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

The quick answer is: because your if condition is checking for a string 'true', and not boolean. It should just be "if(answer)" which will check for boolean true.

 

The longer answer would include an explanation of how to optimize your script include, as it seems running 2 GlideRecord look-up which is not necessary, and also looping through ALL incidents that match the criteria - don't you just want to check the criteria for the one record that is being loaded? You could just pass the created on date of the incident and the department of the caller as parameters to your script include, and then run a GlideAggregate query (better performance compared to GlideRecord if you don't actually need to do anything with the fields of the record) to check if there are any records. I can help you out with an example later if needed.

@Laszlo Balla can you pls share how it will be done in the right and efficient way?

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.

@Laszlo Balla but where is the comparison to the u_operational_activity table?