Value is not passing on the RITM record using the script below depending on company of user catalog

Atheher Fathima
Mega Guru

Hi All, 

 

The Requirement is that if a users is from billable company x & y, then the reference variable cost_center_xy should be visible. It should also populate the cost center for the user from the sys_user table. If user is from any other billable company then the text variabl cost_center_other should be visible. I have written the below script, the is setting the value fine in catalog form however when in RITM, the cost center variable is blank if the user is from xy company. it is copying the value to cost center field for other companies. I have written the below onchange catalog client script and also have selected applies on catalog task, catalog item and ritm check boxes. the delivery location is copying fine, issue is only when user is from x or y company the cost center is showing blank in RITM

 

function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading) { // Check if the field change is not due to initial loading
var reference = g_form.getReference('requested_for', function(reference) {
caller(reference); // Call the 'caller' function with the reference object
});
}
}

function caller(reference) {
if (reference) {
var RequestedFor = reference.u_billable_company;

if (RequestedFor === 'a170cd88873f241092d696883cbb3508' || RequestedFor === 'b312b0531b5f2410635a4332dd4bcb75') {
// Show "cost_center_xy" and hide "cost_center_otherbu"
g_form.setDisplay("cost_center_xy", true);
g_form.setDisplay("cost_center_otherbu", false);

var sys_user = new GlideRecord("sys_user");
sys_user.addQuery("sys_id", newValue);
sys_user.addQuery("cost_center", "!=", "");
sys_user.addQuery("cost_center.valid_from", "<=", "javascript&colon;gs.endOfYesterday()");
sys_user.addQuery("cost_center.valid_toISEMPTY^ORcost_center.valid_to", ">=", "javascript&colon;gs.endOfToday()");
sys_user.setLimit(1);
sys_user.query(function(sys_user) {
if (sys_user.next()){
g_form.setValue("cost_center_nca_hanz", sys_user.cost_center);
} else {
// Hide "cost_center_xy" and show "cost_center_otherbu"
g_form.setDisplay("cost_center_xy", false);
g_form.setDisplay("cost_center_otherbu", true);
}
});

var sys_user_loc = new GlideRecord("sys_user");
sys_user_loc.addQuery("sys_id", newValue);
sys_user_loc.setLimit(1);
sys_user_loc.query(function(sys_user_loc) {
if (sys_user_loc.next())
g_form.setValue("requested_for_delivery_location", sys_user_loc.location);
});
}
}
}

 

can anyone please suggest what i am missing here or how i can proceed to troubleshoot this?

1 REPLY 1

OlaN
Giga Sage
Giga Sage

Hi,

I would take this entire script, and put it in a Script Include instead, and call the script include from the Client Script using GlideAjax, then set the values in the form using whatever you need in return from the GlideAjax call.

Here is a good article to get you started using GlideAjax.

Start there, and ask if you need additional help.