(functionexecuteRule(current, previous /*null when async*/) {
try {
if (current.u_appconfiguration_tag == "appconfig-workflow" && (current.state == -1 || current.state == -5 || current.state == 4)) {
//creating the glider Encrypt objectvar glideEncrypt = newGlideEncrypter();
//fetching the sys_property that contains the IAM keyvar encryptedIbmIAMKey = gs.getProperty('<Add System property name which holds the IBM IAM key (Case Sensitive)>');
//example: var encryptedKey = gs.getProperty('WorkflowAppConfigIamKey');//Decrypting the keyvar decryptedIAMKey = glideEncrypt.decrypt(encryptedIbmIAMKey);
gs.addInfoMessage("IBM KEY DECRYPTION COMPLETED");
if (Object.keys(decryptedIAMKey).length !== 0) {
gs.addInfoMessage("CHANGE REQUEST IDENTIFIED AS WORKFLOW RELATED");
//Adding the decrypted key to the IAM token generation bodyvar bodyContent = "grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=" + decryptedIAMKey;
//Making POST call to IBM IAM to generate tokenvar ibmIamTokenRequest = new sn_ws.RESTMessageV2();
ibmIamTokenRequest.setHttpMethod('POST');
ibmIamTokenRequest.setEndpoint('https://iam.cloud.ibm.com/identity/token');
ibmIamTokenRequest.setRequestBody(bodyContent);
ibmIamTokenResponse = ibmIamTokenRequest.execute();
var ibmIamTokenResponseCode = ibmIamTokenResponse.getStatusCode();
gs.addInfoMessage("IBM IAM TOKEN CREATION HTTP RESPONSE CODE: " + ibmIamTokenResponseCode);
if (ibmIamTokenResponseCode == 200) {
//parsing json response to extract the access_tokenvar ibmIamTokenResponseData = JSON.parse(ibmIamTokenResponse.getBody());
//extracting the access_tokenvar access_token = ibmIamTokenResponseData.access_token;
var encryptedInstanceId = gs.getProperty('<Add System property name which holds the AppConfig InstanceId (Case Sensitive)>');
//example: var encryptedInstanceId = gs.getProperty('WorkflowIbmAppConfigInstanceId');var decryptedInstanceId = glideEncrypt.decrypt(encryptedInstanceId);
gs.addInfoMessage("IBM APP CONFIG INSTANCE ID DECRYPTION COMPLETED");
//preparing webhook request to forwardvar webHookRequest = new sn_ws.RESTMessageV2();
//Refer link https://cloud.ibm.com/apis/app-configuration#endpoints-urls for more info on base URL//Choose the base url from below based on your AppConfig instance region//Dallas: https://us-south.apprapp.cloud.ibm.com//Washington DC: https://us-east.apprapp.cloud.ibm.com//London: https://eu-gb.apprapp.cloud.ibm.com//Sydney: https://au-syd.apprapp.cloud.ibm.com//Frankfurt: https://eu-de.apprapp.cloud.ibm.com//Madrid: https://eu-es.apprapp.cloud.ibm.com
webHookRequest.setEndpoint('{{AppConfigRegionBaseURL}}/apprapp/workflow/v1/instances/' + decryptedInstanceId + '/crevents');
webHookRequest.setRequestHeader('Authorization', 'Bearer ' + access_token);
webHookRequest.setHttpMethod('POST');
webHookRequest.setRequestHeader("Accept", "application/json");
webHookRequest.setRequestHeader("Content-Type", "application/json");
//preparing the data for the webhook requestvar webHookReqDataObject = newObject();
//BELOW ARE THE DATA THAT IS NEEDED TO PROCESS THE REQUEST, MODIFYING OR ALTERING THE DATA OR THE ATTRIBUTE WILL RESULT IN WEBHOOK REQUEST FAILURE.
webHookReqDataObject.operation = String(current.operation());
webHookReqDataObject.short_description = String(current.short_description);
webHookReqDataObject.change_request_id = String(current.number);
webHookReqDataObject.description = String(current.description);
webHookReqDataObject.cr_state = String(current.state);
webHookReqDataObject.cr_approval_assignment_group = String(current.assignment_group);
webHookReqDataObject.appconfiguration_tag = String(current.u_appconfiguration_tag);
webHookReqDataObject.implementation_time = String(current.work_start);
var webHookJsonStringData = JSON.stringify(webHookReqDataObject);
gs.addInfoMessage(webHookJsonStringData);
webHookRequest.setRequestBody(webHookJsonStringData);
//checking the change request has the tag, we only accept the CR with the below mentioned tag id
gs.addInfoMessage("SENDING REQUEST TO APP CONFIGURATION WEBHOOK HANDLER");
var webHookResponse = webHookRequest.execute();
httpResponseStatus = webHookResponse.getStatusCode();
gs.addInfoMessage("APP CONFIGURATION WEBHOOK HANDLER RESPONSE CODE: " + httpResponseStatus);
if (httpResponseStatus == 200) {
gs.addInfoMessage("SUCCESSFULLY EXECUTED THE WEBHOOK CALL");
} else {
//try once more
webHookResponse = webHookRequest.execute();
httpResponseStatus = webHookResponse.getStatusCode();
gs.addInfoMessage("WEBHOOK CALL RETRY STATUS CODE: " + httpResponseStatus);
gs.addInfoMessage(webHookResponse.getBody());
}
} else {
gs.addInfoMessage("IBM IAM TOKEN GENERATION FAILED");
}
} else {
gs.addInfoMessage("DECRYPTED IAM KEY IS EMPTY OR NOT FOUND");
}
} else {
gs.addInfoMessage("CR STATE : " + current.state + " CR APPCONFIGURATION TAG VALUE : " + current.u_appconfiguration_tag);
gs.addInfoMessage("CHANGE REQUEST DOES NOT BELONG TO WORKFLOW INTEGRATION OR STATE OF THE CR IS NOT VALID");
}
} catch (ex) {
var message = ex.getMessage();
gs.info(message);
}
})(current, previous);
IBM IAM トークンまたは App Configuration サービス資格情報の API キーと App Configuration インスタンス ID を使用して、コピーしたスクリプトを変更します。
「いつ実行するか」 タブにナビゲートし、 「更新」 チェック・ボックスを選択します。
ServiceNow
「 送信 」をクリックして詳細を保存してください。
Webhook スクリプトの IBM IAM トークンまたは App Configuration サービス資格情報の API キーを追加する方法
Webhook スクリプトには、Webhook 呼び出し用の IBM IAM トークン、またはトークンを生成するための API キーを保管できる App Configuration サービス資格情報の API キーが必要です。
以下の手順を実行します。
ServiceNow インスタンスに管理者としてログインします。
「すべて」 タブをクリックし、 sys_properties.LIST を検索して、 Enter キーを押します。 あるいは、 https://{{instanceid}}.service-now.com/sys_properties_list.doにアクセスすることもできます。ここで、 instanceid は ServiceNow インスタンス ID
です。