OnChange if CI Autopopulate Supplimentary and Business Approver and should be retained when edited.

vinaykumar
Tera Contributor
Hi Team, 
 
Can anyone help me in correcting below script to satisfy the logic - 
onchange client script - on change of CI fields - supplementary approver and business approver should auto populate based and user can even edit or remove the autopopulated values and save.
Also it should work when copy change is performed, then CI should be copied and based on CI  - supplementary approver and business approver should autopopulate. here is the script -
 
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (newValue === '') {
        return;
    }

 

    // Assuming 'populateApprovers' is a function that fetches and sets the approver fields based on the CI
    populateApprovers(newValue, isLoading);
}

 

function populateApprovers(ciSysId, isLoading) {
    var ga = new GlideAjax("ITSMAjaxUtils");
    ga.addParam("sysparm_name", "getSupportGroup");
    ga.addParam("sysparm_sys_id", ciSysId);
    ga.getXML(function(response) {
        var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));

 

        // Get current values of the approver fields
        var currentSuppAppr = g_form.getValue('u_supplementary_customer_approver');
        var currentBussAppr = g_form.getValue('u_business_approver');

 

        // Handle multiple approvers, user edits, and clearing:
        // Prioritize user edits and overwrite only when both fields are empty:
        if (isLoading && (!currentSuppAppr || !currentBussAppr)) {
            // Set values only if both are initially empty
            g_form.setValue('u_supplementary_customer_approver', answer.suppAppr);
            g_form.setValue('u_business_approver', answer.bussAppr);
        }
    });
}
1 ACCEPTED SOLUTION

nishanthk
Tera Expert

Dear @vinaykumar

 

try below script in onchange client script - it gives solution for Copy change UI action performed as Copy change will always creates new record and we are checking whether new record or not - !g_form.isNewRecord()

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
g_form.addInfoMessage('g_form.isNewRecord() : '+ g_form.isNewRecord());
if ((isLoading || newValue === '') && !g_form.isNewRecord()) {
return;
}

var ga = new GlideAjax("ITSMAjaxUtils");
ga.addParam("sysparm_name", "getSupportGroup");
ga.addParam("sysparm_sys_id", newValue);
ga.getXML(response);

function response(response) {
var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
var businessApprList = g_form.getValue('u_business_approver');
var businessApproversArray = businessApprList.split(',');
var bussAppfromCI = answer.bussAppr;

// if (businessApproversArray !== bussAppfromCI && businessApproversArray != '') {
// return;
// }
g_form.setValue('u_applicable_cab', answer.appCi);
g_form.setValue('u_supplementary_customer_approver', answer.suppAppr);
g_form.setValue('u_business_approver', answer.bussAppr);
}
}

View solution in original post

7 REPLIES 7

i have tried get action name but still i am getting empty values .

So, are you saying when the client script executes & when you logged in values you were able to receive the correct values from the script include but using UI action it is not working?
or the client script itself is not working

 
 
 

nishanthk
Tera Expert

Dear @vinaykumar

 

try below script in onchange client script - it gives solution for Copy change UI action performed as Copy change will always creates new record and we are checking whether new record or not - !g_form.isNewRecord()

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
g_form.addInfoMessage('g_form.isNewRecord() : '+ g_form.isNewRecord());
if ((isLoading || newValue === '') && !g_form.isNewRecord()) {
return;
}

var ga = new GlideAjax("ITSMAjaxUtils");
ga.addParam("sysparm_name", "getSupportGroup");
ga.addParam("sysparm_sys_id", newValue);
ga.getXML(response);

function response(response) {
var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
var businessApprList = g_form.getValue('u_business_approver');
var businessApproversArray = businessApprList.split(',');
var bussAppfromCI = answer.bussAppr;

// if (businessApproversArray !== bussAppfromCI && businessApproversArray != '') {
// return;
// }
g_form.setValue('u_applicable_cab', answer.appCi);
g_form.setValue('u_supplementary_customer_approver', answer.suppAppr);
g_form.setValue('u_business_approver', answer.bussAppr);
}
}