- 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-15-2023 03:39 AM
Perfect!
Can you show me the backend field name of the variables, please send the screenshot
We get all the data problem is with set the values only 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2023 03:44 AM
i think i got it now;
Never knew that the variable field names were case sensitive 😧
thank you a billion bucks @Prince Arora , i owe you loads of pints 🍺 as we like to say it in the UK.
i will be reaching out to you for more
- 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:17 AM - edited 04-16-2023 08:22 AM
Trying to do similar to what i did yesterday;
here's the supposed response;
[
{ "Id": 0, "Thumbprint": "string", "SerialNumber": "string", "IssuedDN": "string", "IssuedCN": "string", "ImportDate": "2023-04-13T10:36:14.586Z", "NotBefore": "2023-04-13T10:36:14.586Z", "NotAfter": "2023-04-13T10:36:14.586Z", "IssuerDN": "string", "PrincipalId": 0, "TemplateId": 0, "CertState": 0, "KeySizeInBits": 0, "KeyType": 0, "RequesterId": 0, "IssuedOU": "string", "IssuedEmail": "string", "KeyUsage": 0, "SigningAlgorithm": "string", "CertStateString": "string", "KeyTypeString": "string", "RevocationEffDate": "2023-04-13T10:36:14.586Z", "RevocationReason": 0, "RevocationComment": "string", "CertificateAuthorityId": 0, "CertificateAuthorityName": "string", "TemplateName": "string", "ArchivedKey": true, "HasPrivateKey": true, "PrincipalName": "string", "CertRequestId": 0, "RequesterName": "string", "ContentBytes": "string", "ExtendedKeyUsages": [ { "Id": 0, "Oid": "string", "DisplayName": "string" } ], "SubjectAltNameElements": [ { "Id": 0, "Value": "string", "Type": 0, "ValueHash": "string" } ], "CRLDistributionPoints": [ { "Id": 0, "Url": "string", "UrlHash": "string" } ], "LocationsCount": [ { "Type": "string", "Count": 0 } ], "SSLLocations": [ { "StorePath": "string", "AgentPool": "string", "IPAddress": "string", "Port": 0, "NetworkName": "string" } ], "Locations": [ { "StoreMachine": "string", "StorePath": "string", "StoreType": 0, "Alias": "string", "ChainLevel": 0, "CertStoreId": "00000000-0000-0000-0000-000000000000" } ], "Metadata": {}, "CertificateKeyId": 0, "CARowIndex": 0, "CARecordId": "string", "DetailedKeyUsage": { "CrlSign": true, "DataEncipherment": true, "DecipherOnly": true, "DigitalSignature": true, "EncipherOnly": true, "KeyAgreement": true, "KeyCertSign": true, "KeyEncipherment": true, "NonRepudiation": true, "HexCode": "string" }, "KeyRecoverable": true, "Curve": "string" } ]
i am trying to use the thumbprint variable field to populate the Certificate Template name field
when i attempt to enter the thumbprint, this is what i get ;
here's the Client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2023 08:26 AM
the difference between this response and the one that worked yesterday is that this new one starts with "[" and ends with "]" whereas the one from yesterday starts with "{" and ends with "}"