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

@costa1 

 

I have seen in the client script you have updated the line, please take it as it is:

var certi = this.getParameter("sysparm_id");

 

And  add an alert in client script as below:

 

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);
g_form.setValue("YOUR_FIELD_NAME",answer.fieldName);
}

 

After doing above modifications, please provide a certificate ID on the form 

 

PrinceArora_0-1681477748860.png

 

and track the alert what has came up, provide me the response 

 

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

 

 

 

i get this as the alert;

Screenshot 2023-04-14 at 14.36.52.png

 

but nothing shows on the actual variable field:

 

Screenshot 2023-04-14 at 14.37.59.png

@costa1 ,

 

I am not sure what you are getting as response, snippet shared by you is not understandable

Please execute the script in the background script and copy paste the complete response here!

I need complete data for parsing the response data and storing into the fields

here's the response;

 

*** Script: 200
*** Script: {"Id":8171,"Thumbprint":"BB64D8FA24EFCE07C43A05B5C7518E4AEE3A4F77","SerialNumber":"2500000F4F4F83B48095331780000000000F4F","IssuedDN":"CN=test.com","IssuedCN":"test.com","ImportDate":"2023-04-13T07:48:34.13Z","NotBefore":"2023-04-13T07:38:32Z","NotAfter":"2025-04-12T07:38:32Z","IssuerDN":"CN=Keyfactor Demo Drive CA 1,O=Keyfactor Inc","PrincipalId":null,"TemplateId":35,"CertState":1,"KeySizeInBits":2048,"KeyType":1,"RequesterId":39,"IssuedOU":null,"IssuedEmail":null,"KeyUsage":160,"SigningAlgorithm":"SHA-256withRSA","CertStateString":"Active (1)","KeyTypeString":"RSA","RevocationEffDate":null,"RevocationReason":0,"RevocationComment":null,"CertificateAuthorityId":1,"CertificateAuthorityName":"Keyfactor.thedemodrive.com\\Keyfactor Demo Drive CA 1","TemplateName":"DD Web Server 2yr","ArchivedKey":false,"HasPrivateKey":true,"PrincipalName":null,"CertRequestId":null,"RequesterName":"THEDEMODRIVE\\tony","ContentBytes":"MIIGFjCCA/6gAwIBAgITJQAAD09Pg7SAlTMXgAAAAAAPTzANBgkqhkiG9w0BAQsFADA8MRYwFAYDVQQKEw1LZXlmYWN0b3IgSW5jMSIwIAYDVQQDExlLZXlmYWN0b3IgRGVtbyBEcml2ZSBDQSAxMB4XDTIzMDQxMzA3MzgzMloXDTI1MDQxMjA3MzgzMlowEzERMA8GA1UEAxMIdGVzdC5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCnwi4l43k+pPLUgSFeRe60uZQnmRUvlI80QIsxD+BOx5YJcmUpmzoT3BZgVjM1cRuWYuEzqa9HcOb9p4PuODb/baBJolOoVaiWKLcPtoDlAmuetsCpdBuFZca3qn4c58tQDG8bbckxevMQG6CD3Wyqi+GjF1IeAMibNj3xvLtP9vms6FyIV3Nv5OsBgQnYzbzbKFKSlqgRjq0Y71YPNxTSPpnkMlo3ru6d7lFN4hwxf/xbC1mu2qU42QjLZ7wNcOJG4wyUybB2hSb1Lh1RvprYJBuvd+AM3YxDnQDtD6mG7SO1x72t2Xwt7Mkc+Iqt7xA7OapVvCAW3moV8JwOmQ8/AgMBAAGjggI4MIICNDATBgNVHREEDDAKggh0ZXN0LmNvbTAdBgNVHQ4EFgQUo498uYeOBGDzTRanlgPSP0T9mmIwHwYDVR0jBBgwFoAUF9FOO3RG8r3ElGWMnmJPNq+o2o0wVQYDVR0fBE4wTDBKoEigRoZEaHR0cDovL3NlZGVtby50aGVkZW1vZHJpdmUuY29tL0tleWZhY3RvciUyMERlbW8lMjBEcml2ZSUyMENBJTIwMS5jcmwwYAYIKwYBBQUHAQEEVDBSMFAGCCsGAQUFBzAChkRodHRwOi8vc2VkZW1vLnRoZWRlbW9kcml2Z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EHy/HNM2NkbHtSDhk2n+qUKFYcDe/bcp3+L6gbwDzyMwci1XSI2tv6IaxOjXGSoioJY9ZKS5gq59nfpIMu+9zssBTHgT9HQPZ0pvH6qW7s=","ExtendedKeyUsages":[{"Id":1,"Oid":"1.3.6.1.5.5.7.3.1","DisplayName":"Server Authentication"}],"SubjectAltNameElements":[{"Id":217,"Value":"test.com","Type":2,"ValueHash":"E0D1B068180CF7454BD6AB771AA0636E"}],"CRLDistributionPoints":[{"Id":2,"Url":"http://sedemo.thedemodrive.com/Keyfactor%20Demo%20Drive%20CA%201.crl","UrlHash":"BC457F436BBD26C2A49F71736FA77E81"}],"LocationsCount":[],"SSLLocations":[],"Locations":[],"Metadata":{},"CertificateKeyId":9327,"CARowIndex":3919,"CARecordId":"3919","DetailedKeyUsage":{"CrlSign":false,"DataEncipherment":false,"DecipherOnly":false,"DigitalSignature":true,"EncipherOnly":false,"KeyAgreement":false,"KeyCertSign":false,"KeyEncipherment":true,"NonRepudiation":false,"HexCode":"a0"},"KeyRecoverable":false,"Curve":null}

@costa1 

 

Update client script as follows:

 

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);
g_form.setValue("certificate_name", answer.CertificateAuthorityName);
g_form.setValue("certificate_status", answer.CertStateString);
g_form.setValue("certificate_expiration_date",answer.NotAfter);
}

 

 

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