- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 12:57 AM
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
Tried the above script but nothing happens... help needed please
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 04:56 AM - edited 04-14-2023 04:57 AM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2023 03:36 AM
Oh nice, that seemed to show the respected backend field data, i get all the results;
But the catalog item variable fields are still not populated (empty)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2023 04:56 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2023 08:48 AM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 08:26 AM
Thanks a lot for the help... i just tried all the above and it still is not populating the fields
catalog item form;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 08:30 AM
@costa1 ,
Can you modify this line script include:
FROM
return JSON.stringify(response.getBody());
TO:
return response.getBody();
Also check the backend name of fields, I am assuming it from my end
If my answer solved your issue, please mark my answer as ✅Correct & 👍Helpful based on the Impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 10:18 AM
still the same, i also cross checked the backend fields (they all look correct)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 11:22 AM
Can you add alerts as mentioned below, lets trace the values
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");
alert(answer);
answer = JSON.parse(answer);
alert(answer.CertificateAuthorityName);
alert(answer.CertStateString);
alert(answer.NotAfter);
g_form.setValue("certificate_name", answer.CertificateAuthorityName);
g_form.setValue("certificate_status", answer.CertStateString);
g_form.setValue("certificate_expiration_date",answer.NotAfter);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2023 03:36 AM
Oh nice, that seemed to show the respected backend field data, i get all the results;
But the catalog item variable fields are still not populated (empty)