Help with a Catalog Client script

jonathangilbert
Kilo Sage

Hi All

 

I seem to have an issue with this client script not executing correctly on a Catalog Item. The script is looking up a user on the sys_user table using a callback function. Then depending on if the field on the looked up user record has a value in it, will then make a variable on the catalog item hidden or visible. As it currently stands the form is hidding variables even when there is no entry on the looked up field, whereas it shoudl be displaying the variabIe.

Ithink i am missing something between each of the "if" statements. Can anyone assist?

 

 

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {
      return;
   }
   
    var usr = g_form.getReference('name_of_existing_colleague',callBack);
   
}
function callBack(usr){
   

     if (usr.u_dunsaperpprodusername != "") {
     g_form.setDisplay('sap_ecc_production',false);
    } else
    g_form.setDisplay('sap_ecc_production',true);
 

//AM I MISSING SOMETHING TO SEPERATE THE ABOVE FROM THE BELOW  "IF" STATEMENT 
 

    if (usr.u_dunsaperppreprodusername != "") {
   g_form.setDisplay('sap_ecc_pre_production',false);
    } else
    g_form.setDisplay('sap_ecc_pre_production',true);


    if (usr.u_dunsaperpdev100username != "") {
     g_form.setDisplay('sap_ecc_development_100',false);
    } else
    g_form.setDisplay('sap_ecc_development_100',true);


    if (usr.u_dunsaperpdev102username != "") {
   g_form.setDisplay('sap_ecc_development_102',false);
    } else
    g_form.setDisplay('sap_ecc_development_102',true);


    if (usr.u_dunsaperpqausername != "") {
     g_form.setDisplay('sap_ecc_qa',false);
    } else
    g_form.setDisplay('sap_ecc_qa',true);


    if (usr.u_dunsaperpsandboxusername != "") {
   g_form.setDisplay('sap_ecc_sandbox',false);
    } else
    g_form.setDisplay('sap_ecc_sandbox',true);


    if (usr.u_dunsapewmprodusername != "") {
     g_form.setDisplay('sap_ewm_production',false);
    } else
    g_form.setDisplay('sap_ewm_production',true);


    if (usr.u_dunsapewmpreprodusername != "") {
   g_form.setDisplay('sap_ewm_pre_production',false);
    } else
    g_form.setDisplay('sap_ewm_pre_production',true);


    if (usr.u_dunsapewmdevusername != "") {
     g_form.setDisplay('sap_ewm_development',false);
    } else
    g_form.setDisplay('sap_ewm_development',true);


    if (usr.u_dunsapewmqausername != "") {
   g_form.setDisplay('sap_ewm_qa',false);
    } else
    g_form.setDisplay('sap_ewm_qa',true);


    if (usr.u_dunsapewmsandboxusername != "") {
     g_form.setDisplay('sap_ewm_sandbox',false);
    } else
    g_form.setDisplay('sap_ewm_sandbox',true);


    if (usr.u_dunsappoprodusername != "") {
   g_form.setDisplay('sap_po_production',false);
    } else
    g_form.setDisplay('sap_po_production',true);


    if (usr.u_dunsappopreprodusername != "") {
     g_form.setDisplay('sap_po_pre_production',false);
    } else
    g_form.setDisplay('sap_po_pre_production',true);


    if (usr.u_dunsappodevusername != "") {
   g_form.setDisplay('sap_po_development',false);
    } else
    g_form.setDisplay('sap_po_development',true);


    if (usr.u_dunsappoqausername != "") {
     g_form.setDisplay('sap_po_qa',false);
    } else
    g_form.setDisplay('sap_po_qa',true);


    if (usr.u_dunsapposandboxusername != "") {
   g_form.setDisplay('sap_po_sandbox',false);
    } else
    g_form.setDisplay('sap_po_sandbox',true);


    if (usr.u_dunsaphrprodusername != "") {
     g_form.setDisplay('sap_hr_production',false);
    } else
    g_form.setDisplay('sap_hr_production',true);


    if (usr.u_dunsaphrdevusername != "") {
   g_form.setDisplay('sap_hr_development',false);
    } else
    g_form.setDisplay('sap_hr_development',true);


    if (usr.u_dunsaphrqausername != "") {
     g_form.setDisplay('sap_hr_qa',false);
    } else
    g_form.setDisplay('sap_hr_qa',true);


    if (usr.u_dunsaphrsandboxusername != "") {
   g_form.setDisplay('sap_hr_sandbox',false);
    } else
    g_form.setDisplay('sap_hr_sandbox',true);


    if (usr.u_dunsapbwprodusername != "") {
     g_form.setDisplay('sap_bw_production',false);
    } else
    g_form.setDisplay('sap_bw_production',true);


    if (usr.u_dunsapbwpreprodusername != "") {
   g_form.setDisplay('sap_bw_pre_production',false);
    } else
    g_form.setDisplay('sap_bw_pre_production',true);


    if (usr.u_dunsapbwdevusername != "") {
     g_form.setDisplay('sap_bw_development',false);
    } else
    g_form.setDisplay('sap_bw_development',true);


    if (usr.u_dunsapbwqausername != "") {
   g_form.setDisplay('sap_bw_qa',false);
    } else
    g_form.setDisplay('sap_bw_qa',true);


    if (usr.u_dunsapbwsandboxusername != "") {
     g_form.setDisplay('sap_bw_sandbox',false);
    } else
    g_form.setDisplay('sap_bw_sandbox',true);


    if (usr.u_dunsapbatchmanprodusername != "") {
   g_form.setDisplay('sap_batchman_prod',false);
    } else
    g_form.setDisplay('sap_batchman_prod',true);


    if (usr.u_dunsapbatchmanpreprodusername != "") {
     g_form.setDisplay('sap_batchman_pre_production',false);
    } else
    g_form.setDisplay('sap_batchman_pre_production',true);


    if (usr.u_dunsapbatchmandevusername != "") {
   g_form.setDisplay('sap_batchman_dev',false);
    } else
    g_form.setDisplay('sap_batchman_dev',true);


    if (usr.u_dunsapbatchmanqausername != "") {
     g_form.setDisplay('sap_batchman_qa',false);
    } else
    g_form.setDisplay('sap_batchman_qa',true);


    if (usr.u_dunsapsolmanprodusername != "") {
    g_form.setDisplay('sap_solman_production',false);
     g_form.setDisplay('active_control',false);
    } else
    g_form.setDisplay('sap_solman_production',false);
    g_form.setDisplay('active_control',true);

    if (usr.u_dunsapsolmandevusername != "") {
     g_form.setDisplay('sap_solman_development',false);
    } else
    g_form.setDisplay('sap_solman_development',true);



}
1 ACCEPTED SOLUTION

Hi, I have found the issue. It was due to an ACL on the sys_user table, which prevented the results of the client script from being displayed. Thankyou for you assistance anyway, it was very kind of you 

 

View solution in original post

14 REPLIES 14

The above script doesnt work properly it just returns "undefined". I have tested that results are beign returned by the callback, by getting the result to populate a text field on the form. I have no other client scripts running on the cat item

Hi, I have found the issue. It was due to an ACL on the sys_user table, which prevented the results of the client script from being displayed. Thankyou for you assistance anyway, it was very kind of you 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@jonathangilbert 

try this optimized code once

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

    g_form.getReference('name_of_existing_colleague', callBack);
}

function callBack(usr) {
    var fields = [
        { username: 'u_dunsaperpprodusername', field: 'sap_ecc_production' },
        { username: 'u_dunsaperppreprodusername', field: 'sap_ecc_pre_production' },
        { username: 'u_dunsaperpdev100username', field: 'sap_ecc_development_100' },
        { username: 'u_dunsaperpdev102username', field: 'sap_ecc_development_102' },
        { username: 'u_dunsaperpqausername', field: 'sap_ecc_qa' },
        { username: 'u_dunsaperpsandboxusername', field: 'sap_ecc_sandbox' },
        { username: 'u_dunsapewmprodusername', field: 'sap_ewm_production' },
        { username: 'u_dunsapewmpreprodusername', field: 'sap_ewm_pre_production' },
        { username: 'u_dunsapewmdevusername', field: 'sap_ewm_development' },
        { username: 'u_dunsapewmqausername', field: 'sap_ewm_qa' },
        { username: 'u_dunsapewmsandboxusername', field: 'sap_ewm_sandbox' },
        { username: 'u_dunsappoprodusername', field: 'sap_po_production' },
        { username: 'u_dunsappopreprodusername', field: 'sap_po_pre_production' },
        { username: 'u_dunsappodevusername', field: 'sap_po_development' },
        { username: 'u_dunsappoqausername', field: 'sap_po_qa' },
        { username: 'u_dunsapposandboxusername', field: 'sap_po_sandbox' },
        { username: 'u_dunsaphrprodusername', field: 'sap_hr_production' },
        { username: 'u_dunsaphrdevusername', field: 'sap_hr_development' },
        { username: 'u_dunsaphrqausername', field: 'sap_hr_qa' },
        { username: 'u_dunsaphrsandboxusername', field: 'sap_hr_sandbox' },
        { username: 'u_dunsapbwprodusername', field: 'sap_bw_production' },
        { username: 'u_dunsapbwpreprodusername', field: 'sap_bw_pre_production' },
        { username: 'u_dunsapbwdevusername', field: 'sap_bw_development' },
        { username: 'u_dunsapbwqausername', field: 'sap_bw_qa' },
        { username: 'u_dunsapbwsandboxusername', field: 'sap_bw_sandbox' },
        { username: 'u_dunsapbatchmanprodusername', field: 'sap_batchman_prod' },
        { username: 'u_dunsapbatchmanpreprodusername', field: 'sap_batchman_pre_production' },
        { username: 'u_dunsapbatchmandevusername', field: 'sap_batchman_dev' },
        { username: 'u_dunsapbatchmanqausername', field: 'sap_batchman_qa' },
        { username: 'u_dunsapsolmanprodusername', field: 'sap_solman_production' },
        { username: 'u_dunsapsolmandevusername', field: 'sap_solman_development' }
    ];

    fields.forEach(function(item) {
        if (usr[item.username]) {
            g_form.setDisplay(item.field, false);
        } else {
            g_form.setDisplay(item.field, true);
        }
    });

    // Special case for 'sap_solman_production' and 'active_control'
    if (usr.u_dunsapsolmanprodusername) {
        g_form.setDisplay('active_control', false);
    } else {
        g_form.setDisplay('active_control', true);
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur, thank for the script, but it is displaying variables that shoud be hidden as the call back field has a value in it

@jonathangilbert 

did you try to add alert for each field name?

are you sure the field names are correct?

the script I shared should work fine provided the field name is correct

 

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader