Onchange Catalog client script not working

costa1
Tera Expert

Trying to create an onChange ServiceNow catalog client scripts that update variable fields based on external REST API data.

 

The fields should auto populate after entering a certificate ID number. The response should include the Common name, Status and expiry date of the certificate, which is based on the certificate ID entered on the field

 

Script used;

 

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

var certificateId = g_form.getValue('certificate_id');
var url = 'https://test.demotest.com/KeyfactorAPI/certificates/' + 'certificateId';
var headers = {'Authorization': 'Basic ' + btoa("username:Password")};
var gr = new GlideHTTPRequest('GET', url);
gr.setHeaders(headers);
gr.get(function(response) {
var responseBody = response.getBody();
var parsedResponse = JSON.parse(responseBody);
g_form.setValue('certificate_name', parsedResponse.IssuedCN);
g_form.setValue('certificate_status', parsedResponse.CertStateString);
g_form.setValue('certificate_expiration_date', parsedResponse.NotAfter);
});
}

 

The catalog item form

costa1_0-1681458898594.png

 

Tried the above script but nothing happens... help needed please

4 ACCEPTED SOLUTIONS

Prince Arora
Tera Sage
Tera Sage

@costa1 ,

 

Can you try below script in background script first if you will get response will move it to your client side:

 

 

 

var request = new sn_ws.RESTMessageV2();
var certi = "" ; //please provide here some certificate ID hardcoded for testing
request.setEndpoint('https://test.demotest.com/KeyfactorAPI/certificates/' + certi); request.setHttpMethod('GET');
var
user = 'admin'; //provide here user name var password = 'admin'; //provide here password request.setBasicAuth(user,password); request.setRequestHeader("Accept","application/json"); request.setRequestHeader('Content-Type','application/json'); var response = request.execute();
gs.info(response.getStatusCode()); gs.log(response.getBody());

Please try this script in Background script first and let me know if you got the response!

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

 

View solution in original post

Oh nice, that seemed to show the respected backend field data, i get all the results;

costa1_0-1681554829096.png

costa1_1-1681554862526.png

 

costa1_2-1681554876326.png

 

But the catalog item variable fields are still not populated (empty)

View solution in original post

@costa1 ,

 

Please Accept the solution if it has worked for you 🙂

View solution in original post

@costa1 ,

 

Update the last three lines as:

 

g_form.setValue("certificate_name", answer[0].CertificateAuthorityName);
g_form.setValue("certificate_status", answer[0].CertStateString);
g_form.setValue("certificate_expiration_date",answer[0].NotAfter);

 If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

View solution in original post

28 REPLIES 28

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

if (isLoading || newValue === ''){

return;

}

var url = 'https://test.demotest.com/KeyfactorAPI/certificates/' + g_form.getValue('certificate_id');

var headers = {'Authorization': '<REST_API_AUTHORIZATION_HEADER>'}; // Make an HTTP request to the REST API

var gr = new GlideHTTPRequest('GET', url);

gr.setHeaders(headers);

gr.get(function(response) {

var responseBody = response.getBody(); var parsedResponse = JSON.parse(responseBody);  g_form.setValue('certificate_name', parsedResponse.common_name);

g_form.setValue('certificate_status', parsedResponse.status);

g_form.setValue('certificate_expiration_date', parsedResponse.expiry_date);

});

}

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

Prince Arora
Tera Sage
Tera Sage

@costa1 ,

 

Can you try below script in background script first if you will get response will move it to your client side:

 

 

 

var request = new sn_ws.RESTMessageV2();
var certi = "" ; //please provide here some certificate ID hardcoded for testing
request.setEndpoint('https://test.demotest.com/KeyfactorAPI/certificates/' + certi); request.setHttpMethod('GET');
var
user = 'admin'; //provide here user name var password = 'admin'; //provide here password request.setBasicAuth(user,password); request.setRequestHeader("Accept","application/json"); request.setRequestHeader('Content-Type','application/json'); var response = request.execute();
gs.info(response.getStatusCode()); gs.log(response.getBody());

Please try this script in Background script first and let me know if you got the response!

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

 

hi Prince,

 

Thanks, i got a response from the script,.

 

how can use that to troubleshoot my script include and client script ?

@costa1 ,

 

Please write a script include - client callable checked as mentioned below:

 

Client Script:

var ga = new GlideAjax('global.testsample');
ga.addParam('sysparm_name', 'getResponse');
ga.addParam('sysparm_id', newValue);
ga.getXML(getData);

}

function getData(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.parse(answer);
g_form.setValue("YOUR_FIELD_NAME",answer.fieldName);
}

 Screenshot:

 

PrinceArora_0-1681474632747.png

 

Script Include:

 

PrinceArora_1-1681474709485.png

 

code:

 

getResponse: function() {
var request = new sn_ws.RESTMessageV2();
var certi = this.getParameter("sysparm_id"); //please provide here some certificate ID hardcoded for testing

request.setEndpoint('https://test.demotest.com/KeyfactorAPI/certificates/' + certi);
request.setHttpMethod('GET');

var user = 'admin'; //provide here user name
var password = 'admin'; //provide here password

request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');

var response = request.execute();
gs.info(response.getStatusCode());
gs.log(response.getBody());
return JSON.stringify(response.getBody());
},

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

created the below Script include;

costa1_0-1681476858113.png

 

Client Script;

costa1_1-1681476976453.png

 

But nothing shows on the catalog item;

costa1_2-1681477033102.png

 

also to add i am applying the script under on the Catalog Item Client Script ;

 

costa1_0-1681478511835.png