Auto Populate catalog item variable from a table's variable

Shalika
Tera Expert

I have a variable service_application_name which is a reference variable and it is referring to cmdb_ci_appl table. 

The other variable is u_privacy_data which is a multiple choice having values 'Yes' and 'No'.

Now, when I select a value in service_application_name then, it should fetch the data from  cmdb_ci_appl table and autopopulate  "u_privacy_data" variable. 

I am using client script and script include as follows, but it is not working --

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

 if (newValue == '') {
      g_form.setValue('u_privacy_data', '');
   }

    var ga = new GlideAjax('privacydatacheck');
 
    ga.addParam('sysparm_name','getProductCode');
    ga.addParam('sysparm_ci', g_form.getValue('servicenow_application_name'));
    ga.getXML(doPopulate);
}
 
function doPopulate(response) {
 
    var answer = response.responseXML.documentElement.getAttribute("answer");
 
    g_form.setValue('u_privacy_data', answer);
}
 

Script Include ---

var privacydatacheck = Class.create();
 
productCodeCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    getPrivacyData: function() {
 
        var retVal = '';
        var ciID   = this.getParameter('sysparm_ci');        
        var ci  = new GlideRecord('cmdb_ci_appl'); 
        // If Business Service found, return its product code
        if (ci.get(ciID)) {
            if (ci.u_privacy_data != '') {
                retVal = ci.u_privacy_data;
            } 
        }
        return retVal;
    }  
    
});
   

1 ACCEPTED SOLUTION

Then you don't need to write 3 different function for this, you can get all 3 variables via only one function and one client script.

Client Script :

function onChange(control, oldValue, newValue, isLoading) {
 if (newValue == '') {
      g_form.setValue('u_product_code', '');
   }
    var ga = new GlideAjax('productCodeCheck');
    ga.addParam('sysparm_name', 'getProductCode');
    ga.addParam('sysparm_ci', newValue);
    ga.getXML(doPopulate);
// Callback function to process the response returned from the server
function doPopulate(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    answerJson=JSON.parse(answer);
    g_form.setValue('u_product_code', answerJson.u_product_code);
   g_form.setValue("u_privacy_data",answerJson.u_privacy_data);
   g_form.setValue("add email field name herer",answerJson.email);
}
}
 

Script Include Function :

getProductCode: function() {
 
        var productCode= '',privacyData="",email="";
        var ciID   = this.getParameter('sysparm_ci');        
        var ci  = new GlideRecord('cmdb_ci_appl'); 
        // If Business Service found, return its product code
        if (ci.get(ciID)) {
            if (ci.u_product_code != '') {
                productCode= ci.u_product_code;
            } 
         if (ci.u_privacy_data != '') {
                privacyData= ci1.u_privacy_data;
            } 
         if (ci2.u_ict_owner.email != '') {
                email= ci.u_ict_owner.email;
            } 
        }
      var resultData = {
             "u_product_code": productCode,
             "u_privacy_data": privacyData,
              "email":email
        };  
        return JSON.strigify(resultData);
    }  ,


 
Please mark this as Correct or Helpful based on the impact.

Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

15 REPLIES 15

Abhijit4
Mega Sage

Hi,

You can try with no code approach as suggested by Musab. However, if you still want to go with script then below is the problem with your script.

The function name used in below line is wrong 

ga.addParam('sysparm_name','getProductCode');

replace it with below line :

ga.addParam('sysparm_name','getPrivacyData');

Let me know if you have any further questions. 
 
Please mark this as Correct or Helpful based on the impact.

Regards,
Abhijit
Community Rising Star 2022

 

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Hii,

I changed the script but still data is not populating

Try adding callback function inside main onChange function as below,

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

 if (newValue == '') {
      g_form.setValue('u_privacy_data', '');
   }

    var ga = new GlideAjax('privacydatacheck');
 
    ga.addParam('sysparm_name','getProductCode');
    ga.addParam('sysparm_ci', g_form.getValue('servicenow_application_name'));
    ga.getXML(doPopulate);

function doPopulate(response) {
 
    var answer = response.responseXML.documentElement.getAttribute("answer");
 
    g_form.setValue('u_privacy_data', answer);
}


}

If it still doesn't work then try adding alert and check whats coming in answer variable.

Let me know if you have any further questions. 
 
Please mark this as Correct or Helpful based on the impact.

Regards,
Abhijit
Community Rising Star 2022


 

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Nothing is coming in alert 

May be your below if condition is not true which is not setting retVal variable. You can add log and check if it is going inside if condition.

 if (ci.get(ciID)) {
            if (ci.u_privacy_data != '') {

               gs.addInfoMessage("Test"+retVal );
                retVal = ci.u_privacy_data;
            } 

If you are still facing issue, please share your updated code with client script and script include configuration details.

Please mark this as Correct or Helpful based on the impact.

Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP