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

Andrew_TND
Mega Sage
Mega Sage

Hi @vinaykumar 

 

It looks pretty bob on if I’m honest. Only thing I can think is if you’re adding the cmdb_ci field as rhe trigger on the client script. 

Please mark as helpful or if it’s resolved the issue, CORRECT!

Madhan somesh
Mega Guru

Hi @vinaykumar ,

 

What was the issue that you're facing here?

Are you able to pass & retrieve values from the Script include?

 

Please add them once & try. Do let me know if it's still not working so that I could look furter.

 

Thanks & Regards,

Madhan Somesh.
Can you confirm it by adding few logs/infos in your client script & the script Include.

@Madhan somesh  yes, we are able to retrieve values from script include. problem here is when i try to edit or clear fields - supplementary approver and business approver  and save then its not saving and retaining values from script include. Also when copy change UI action is triggered these 2 fields should populate based on CI selected which is retrieved from script include

Hi @vinaykumar ,

 

As UI actions are server-side they won't execute on client-side changes.

But you can trigger the client script based on UI action click using the below code in the client script.

 

if (g_form.getActionName() == "UI_action_name"

 

 

Thanks & Regards,

Madhan Somesh.