workday integration with ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2024 02:44 AM
I created scripted Api for workday integration to create service request , request getting created but I can't able to get variable values in requested item please check my below code and gave suggestion to get variables section.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var requestBody = request.body.data;
// Extract necessary fields from the requestBody
var employee_formal_name = requestBody.employee_formal_name;
var employee_s_personal_email = requestBody.employee_s_personal_email;
var start_Date = requestBody.start_Date;
var employee_last_name = requestBody.employee_last_name;
var your_contact_info = requestBody.your_contact_info;
var goes_by = requestBody.goes_by;
var prinmary_business_area_individual_work_under = requestBody.prinmary_business_area_individual_work_under;
var title = requestBody.title;
var new_hire_s_mobile_number = requestBody.new_hire_s_mobile_number;
var employment_type = requestBody.employment_type;
var does_this_position_work_remotely = requestBody.does_this_position_work_remotely;
var reporting_to = requestBody.reporting_to;
var apartment = requestBody.apartment;
var address = requestBody.address;
var department_name_office_location = requestBody.department_name_office_location;
var cost_center = requestBody.cost_center;
var city = requestBody.city;
var state = requestBody.state;
var zip = requestBody.zip;
// var u_category = requestBody.u_category;
// var u_subcategory = requestBody.u_subcategory;
// Define the catalog item sys_id for the Onboarding form
var catalogItemSysId = 'ffe88bf91bd321903638ea80604bcbba';
// Define the category and subcategory sys_id for the Onboarding form
var category = 'Employee';
var subCategory = 'New Hire';
//var stage = 'request_approved';
// Create the catalog request
var requestGr = new GlideRecord('sc_request');
requestGr.initialize();
requestGr.setValue('requester', ''); // Set to a default requester or based on incoming data
//var requestSysId = requestGr.insert();
var requestNumber = requestGr.insert();
// Create the catalog request item
var itemGr = new GlideRecord('sc_req_item');
itemGr.initialize();
itemGr.setValue('request', requestNumber);
itemGr.setValue('cat_item', catalogItemSysId);
itemGr.setValue('u_category', category);
itemGr.setValue('u_subcategory', subCategory);
//itemGr.setValue('stage', stage);
itemGr.setValue('quantity', 1);
var itemSysId = itemGr.insert();
// Set variables for the catalog item using GlideRecord for varaible sets
var variables = {
employee_formal_name : employee_formal_name,
employee_s_personal_email: employee_s_personal_email,
start_Date: start_Date,
employee_last_name : employee_last_name,
your_contact_info : your_contact_info,
goes_by : goes_by,
prinmary_business_area_individual_work_under : prinmary_business_area_individual_work_under,
title : title,
new_hire_s_mobile_number : new_hire_s_mobile_number,
employment_type : employment_type,
does_this_position_work_remotely : does_this_position_work_remotely,
reporting_to : reporting_to,
apartment : apartment,
address : address,
department_name_office_location : department_name_office_location,
cost_center : cost_center,
city : city,
state : state,
zip : zip
};
for (var variable in variables){
//var varGr = new GlideRecord('sc_item_option_mtom');
var varGr = new GlideRecord('u_cdw_data')
varGr.initialize();
varGr.setValue('request_item', itemSysId);
varGr.setValue('item_option', getVariableSysId(variable));
varGr.setValue('value', variable[variable]);
varGr.insert();
}
// Helper function to get the variable sys_id based on name
function getVariableSysId(variableName) {
var varGr = new GlideRecord('item_option_new');
varGr.addQuery('name', variableName);
varGr.query();
if(varGr.next()){
return varGr.sys_id.toString();
}
return '';
}
// Trigger the workflow for the request item
var wf = new Workflow();
wf.startFlow('665fc67a1bd8d1503638ea80604bcb6a', itemSysId);
// var workflowId = 'fb0fad83db330610084c2d89139619fd';
// var workflowContext = wf.startFlow(workflowId, itemSysId);
// if(!workflowContext){
// throw new Error("Workflow did not start");
// }
//Trigger the approval process
// Respond with the request Sys ID
response.setStatus(201);
response.setBody({
requestNumber: requestGr.number
});
})(request, response);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2024 03:28 AM
Hi @BHARATHS ,
Replace your code with following changed one & test, also let me know if any issue:
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body.data;
// Extract necessary fields from the requestBody
var variables = {
employee_formal_name : requestBody.employee_formal_name,
employee_s_personal_email: requestBody.employee_s_personal_email,
start_Date: requestBody.start_Date,
employee_last_name : requestBody.employee_last_name,
your_contact_info : requestBody.your_contact_info,
goes_by : requestBody.goes_by,
prinmary_business_area_individual_work_under : requestBody.prinmary_business_area_individual_work_under,
title : requestBody.title,
new_hire_s_mobile_number : requestBody.new_hire_s_mobile_number,
employment_type : requestBody.employment_type,
does_this_position_work_remotely : requestBody.does_this_position_work_remotely,
reporting_to : requestBody.reporting_to,
apartment : requestBody.apartment,
address : requestBody.address,
department_name_office_location : requestBody.department_name_office_location,
cost_center : requestBody.cost_center,
city : requestBody.city,
state : requestBody.state,
zip : requestBody.zip
};
// Define the catalog item sys_id for the Onboarding form
var catalogItemSysId = 'ffe88bf91bd321903638ea80604bcbba';
// Define the category and subcategory sys_id for the Onboarding form
var category = 'Employee';
var subCategory = 'New Hire';
// Create the catalog request
var requestGr = new GlideRecord('sc_request');
requestGr.initialize();
requestGr.setValue('requester', gs.getUserID()); // Use the current user's ID
var requestSysId = requestGr.insert();
// Create the catalog request item
var itemGr = new GlideRecord('sc_req_item');
itemGr.initialize();
itemGr.setValue('request', requestSysId);
itemGr.setValue('cat_item', catalogItemSysId);
itemGr.setValue('u_category', category);
itemGr.setValue('u_subcategory', subCategory);
itemGr.setValue('quantity', 1);
var itemSysId = itemGr.insert();
// Set variables for the catalog item using GlideRecord for item options
for (var variableName in variables) {
if (variables.hasOwnProperty(variableName)) {
var varSysId = getVariableSysId(variableName);
if (varSysId) {
var varGr = new GlideRecord('sc_item_option');
varGr.initialize();
varGr.setValue('request_item', itemSysId);
varGr.setValue('item_option', varSysId);
varGr.setValue('value', variables[variableName]);
varGr.insert();
}
}
}
// Helper function to get the variable sys_id based on name
function getVariableSysId(variableName) {
var varGr = new GlideRecord('item_option_new');
varGr.addQuery('name', variableName);
varGr.query();
if (varGr.next()) {
return varGr.sys_id.toString();
}
return '';
}
// Trigger the workflow for the request item
var wf = new Workflow();
wf.startFlow('665fc67a1bd8d1503638ea80604bcb6a', itemSysId);
// Respond with the request Sys ID
response.setStatus(201);
response.setBody({
requestNumber: requestGr.number
});
})(request, response);
i hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.
THANK YOU
rajesh chopade.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2024 04:32 AM
I didn't get variable section in requested item, could you please let me know what changes you done that also not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2024 04:39 AM
Please respond and provide real help instead of just putting AI generated, untested scripts as answers that don't work.
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark