I want to trigger notification when the job fails or other than 200 https status code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2025 02:57 AM
I want to trigger notification when the job fails or other than 200 https status code. I have created event and notification. I am not able to create the notification when it fails.
Below i have added the my scheduled job script
// Step 1: Authenticate and Get Access Token
var oAuthClient = new sn_auth.GlideOAuthClient();
var params = {
grant_type: "password", // Parameters: snowflake username, password, grant type, and scope
username: gs.getProperty('x_roho_infor_0.glide.snowflake.username'), // Fetch username from System Property
password: gs.getProperty('x_roho_infor_0.glide.snowflake.password'), // Fetch password from System Property
scope: 'session:role-any'
};
var json = new global.JSON();
var text = json.encode(params);
var tokenResponse = oAuthClient.requestToken('Informatic Integration', text);
var token = tokenResponse.getToken();
gs.info("EPP AccessToken:" + token.getAccessToken());
// Step 2: Fetch the SQL query from the system property
var query = gs.getProperty('x_roho_infor_0.epp_sql_query');
// Check if the query exists in the system property
if (!query) {
throw new Error("SQL query is not defined in the system property.");
}
try {
// Step 2: Create REST message and make API call
var r1 = new sn_ws.RESTMessageV2('x_roho__infor_0.Informatic', 'EP DS POST'); //Restmessage name and post method name
r1.setRequestHeader('Authorization', 'Bearer ' + token.getAccessToken());
r1.setRequestBody(query); // Set the query as the request body
var response1 = r1.execute();
var responseBody1 = JSON.parse(response1.getBody());
var httpStatus1 = response1.getStatusCode();
if (httpStatus1 !== 200) {
// Trigger the failure notification event if the status is not 200
gs.eventQueue('abc.job.failure', current, 'API request failed', 'HTTP Status Code: ' + httpStatus);
// Log message after triggering the event
gs.info('EPP Job Failure - Event abc.job.failure has been triggered.');
throw new Error('Error in API request: HTTP Status Code ' + httpStatus1);
}
if (!responseBody1.resultSetMetaData || !responseBody1.resultSetMetaData.partitionInfo) {
throw new Error('Expected partitionInfo not found in the API response. Response Body: ' + JSON.stringify(responseBody1));
}
var statement_handler = responseBody1.statementHandle;
var paritition_count = responseBody1.resultSetMetaData.partitionInfo.length;
var paritition_info = responseBody1.resultSetMetaData.partitionInfo;
// gs.info("row count array " + paritition_count + "statement_handler " + statement_handler);
for (var m = 0; m < paritition_count; m++) {
// gs.info("EPP one time -m loop row count array " + m + "statement_handler " + statement_handler);
var endpoint = "https://roche-dia.eu-central-1.snowflakecomputing.com/api/v2/statements/" + statement_handler + "?partition=" + m;
var r = new sn_ws.RESTMessageV2('x_roho_infor_0.Informatic', 'EP DS POST'); // Restmessage name and post method name
r.setRequestHeader('Authorization', 'Bearer ' + token.getAccessToken());
r.setRequestBody(query); // Set the query as the request body
r.setEndpoint(endpoint);
r.setHttpMethod('GET');
var response = r.execute();
var responseBody = JSON.parse(response.getBody());
var httpStatus = response.getStatusCode();
if (httpStatus !== 200) {
// Trigger the failure notification event if the status is not 200
gs.eventQueue('abc.job.failure', current, 'API request failed', 'HTTP Status Code: ' + httpStatus);
// Log message after triggering the event
gs.info('EPP Job Failure - Event abc.job.failure has been triggered.');
throw new Error('Error in API request: HTTP Status Code ' + httpStatus);
}
// Initialize output JSON structure
var outputJSON = {
"data": []
};
// Process the data returned
for (var k = 0; k < responseBody.data.length; k++) {
var rowData = responseBody.data[k];
var rowObject = {};
for (var j = 0; j < rowData.length; j++) {
if (m == 0) {
var resp_body5 = responseBody;
var field = responseBody.resultSetMetaData.rowType[j].name.toString().toLowerCase();
} else {
var field = resp_body5.resultSetMetaData.rowType[j].name.toString().toLowerCase();
}
var value = rowData[j];
rowObject[field] = value;
}
outputJSON.data.push(rowObject);
}
//to see the output as a string
var outputJSONString = JSON.stringify(outputJSON);
// Process records for both tables
for (var i = 0; i < outputJSON.data.length; i++) { // Process all records
// for (var i = 0; i < 3; i++) { // Limit to first 5 for testing
try {
var recordData = outputJSON.data[i];
processRecord('x_roho_infor_0_informatics', recordData); // For first table
processRecord('x_roho_infor_0_data_quality', recordData); // For second table
} catch (insertError) {
gs.error("EPP Error occurred while processing record insert/update: " + insertError.message);
}
}
// Final message after processing all records
gs.info("EP - All records have been processed successfully.");
}
} catch (ex) {
var message = ex.message;
gs.error("EPP one time Job - This is catch message--" + message);
// Trigger the failure notification event in case of an exception
gs.eventQueue('abc.job.failure', current, 'Job failed with exception', message);
// Log message after triggering the event
gs.info('EPP Job Failure - Event abc.job.failure has been triggered.');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2025 07:15 AM
Hi @SumanthBabu
Did you try exploring the Flow Error Handler in this case? I'm not 100% sure it fits, but it's a low-code solution that could potentially help.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************