Client script with GlideAjax script include

leachy23
Kilo Expert

Hi all,

Wondering if you can assist with some logic Im struggling to overcome on our change form.

The requirement is to have a default CAB set against certain Business services and for that default CAB to be auto populated in a CAB Approval group field on the change form when a corresponding business service is selected.

***Note the CAB approval field is only visible if the CAB Required box is ticked.

If the user were to change the business service to one that didnt have a default CAB against it, the CAB approval group field will be emptied, the CAB required box will be un-ticked and the CAB Approval group field will disappear.

To set this up I have;

Created a reference field on the Business Service table called u_default_cab which references the sys_user_group table with a reference qualifier for type CAB.

To populate the CAB Approval group I've implemented an onChange client script with a GLideAjax call to a GetDFcab script includes which checks the value of the default cab against the current business service, I've tested and these work fine but only alongside a basic client script that auto checks the cab required box when the business service field isn't empty. 

Ideally I need this logic to check and uncheck the box dependant on the result of the Business Service's default CAB being empty or not.

Please see the current scripts below, any assistance would be really helpful!

Client Script to populate CAB approval group is

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

//Type appropriate comment here, and begin script below

var ga = new GlideAjax('GetDFcab');
ga.addParam('sysparm_name', 'getDFcab');
ga.addParam('sysparm_sys_id', newValue);
ga.getXML(GetDFcabParse);

function GetDFcabParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_cab_approval_group', answer);

}



}

GetDFcab script includes is

var GetDFcab = Class.create();
GetDFcab.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDFcab:function() {

// Pulls the business service sys id from the change and finds the Support groups default CAB

var bsid = this.getParameter('sysparm_sys_id');
var gr = new GlideRecord('cmdb_ci_service');
gr.addQuery('sys_id', bsid);
gr.query();

if(gr.next()) {

var cab = gr.u_default_cab;
return cab;



}

Client script to Auto check CAB required

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

//Type appropriate comment here, and begin script below
if (newValue != ''){
g_form.setValue('cab_required', true);
}
else{
g_form.setValue('cab_required', false);
}
}

 

1 REPLY 1

Dubz
Mega Sage

Hi Leachy,

 

Can you not build the checkbox requirement into the same client script as your glide ajax? When the business service changes the client script is going to call your script include, you can make a slight change to your script include to always return a value and if the business service field changes to empty or doesn't have an associated default CAB that value will be empty. Than you can just say if answer == '' untick the cab required box or vice versa.

 

Cheers

Dave