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  Thank you so much!! can you please just explain me how the comparison to u_operational_activity table fields is made without mentioning the name of the table?

Apologies, there is an error in the code. The GlideAggregate should actually be on the 'u_operational_activity' table and not on incident. That's how 😄 I will correct my previous post.

Basheer
Mega Sage

Hi @Alon Grod ,

Let us debug this like this:

Go to background scripts

paste the code whatever you've written in script include into background script:

var inc = new GlideRecord("incident");
inc.addQuery("active",true);
inc.query();
while(inc.next()){
var gdt = new GlideDateTime(inc.sys_created_on);
gs.log(gdt +" : created on time");
gdt = gdt.getDate();
gs.log(gdt + " : created on date);
//Write your extra code here 
instead of return oper.hasnext(); write as below
var recordPresent = oper.hasnext();
gs.log(recordPresent);
}

 

Let us know what are the logs printed after running the background script, from there we can analyse more.

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 hi i think the problem is that Im not getting the department correctly using inc.caller_id.department.
how can i get the department of the caller of the incident?

Department is a reference field. It will provide the sys_id of the department.

Is u_operational_activtiy has u_departments as reference field???

 

If u_departments is not reference field then you need to get caller_id.department.name 

 

 

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.