- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 03:50 AM - edited 02-27-2024 03:57 AM
HI Community,
I have a requirement, in catalog form there is list collector field called select csos environment if i select 3 options then 3 ritms need to be created..so i have written this script in run script activity in workflow it worked but it created 21 ritms for one ritm it shows all 3 options in list collector variable and rest of the ritms there is no value present in list collector field
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2024 07:31 AM
Hello @suuriya ,
Got it, please try with the modified code below and see how it works for you, also don't forget to replace the backend name of your variable "What type of request is this?" and also values of the choices for "add or modify"
(function executeRule(current, previous /*null when async*/ ) {
// Check if "What type of request is this?" is set to "add" or "modify"
var requestType = current.variables.what_type_of_request_is_this;
if (requestType == 'add' || requestType == 'modify') {
var tcmc = '9a415a5187e0ca14755b33773cbb3516';
var name = current.variables.select_csos_environment.toString();
var employeeName = name.split(',');
var name1 = current.variables.select_csos_environment.getDisplayValue();
var employeeName1 = name1.split(',');
for (var i = 1; i < employeeName.length; i++) {
var gr1 = new GlideRecord('sc_req_item');
gr1.initialize();
gr1.cat_item = current.cat_item;
gr1.request = current.request;
gr1.short_description = "Termination: " + employeeName1[i];
gr1.requested_for = current.variables.requested_for; //replace variable name per your variable
gr1.insert();
GlideSysAttachment.copy('sc_req_item', current.sys_id, 'sc_req_item', gr1.sys_id);
var newRITM = gr1.sys_id;
var sc_item_option_mtom = new GlideRecord('sc_item_option_mtom');
sc_item_option_mtom.addQuery('request_item', current.sys_id);
sc_item_option_mtom.query();
while (sc_item_option_mtom.next()) {
var sc_item_option = new GlideRecord('sc_item_option');
sc_item_option.addQuery('sys_id', sc_item_option_mtom.sc_item_option);
sc_item_option.query();
var j = 0;
if (sc_item_option.next()) {
var sc_item_option_insert = new GlideRecord('sc_item_option');
sc_item_option_insert.initialize();
sc_item_option_insert.item_option_new = sc_item_option.item_option_new;
if (sc_item_option.item_option_new == "7fb719f21b208e9045fc99fa234bcb8b") { //list collector variable sys_id
var names = sc_item_option.value;
sc_item_option_insert.value = employeeName[i];
} else {
sc_item_option_insert.value = sc_item_option.value;
}
sc_item_option_insert.order = sc_item_option.order;
sc_item_option_insert.insert();
var sc_item_option_mtom_insert = new GlideRecord('sc_item_option_mtom');
sc_item_option_mtom_insert.initialize();
sc_item_option_mtom_insert.request_item = newRITM;
sc_item_option_mtom_insert.sc_item_option = sc_item_option_insert.sys_id;
sc_item_option_mtom_insert.insert();
j++;
}
}
}
current.short_description = "Termination: " + employeeName1[0];
current.requested_for = current.variables.requested_for; //replace variable name per your variable
current.update();
var sc_item_option_mtom1 = new GlideRecord('sc_item_option_mtom');
sc_item_option_mtom1.addQuery('request_item', current.sys_id);
sc_item_option_mtom1.query();
while (sc_item_option_mtom1.next()) {
var sc_item_option1 = new GlideRecord('sc_item_option');
sc_item_option1.addQuery('sys_id', sc_item_option_mtom1.sc_item_option);
sc_item_option1.addEncodedQuery('item_option_new=7fb719f21b208e9045fc99fa234bcb8b'); //list collector variable sys_id
sc_item_option1.query();
if (sc_item_option1.next()) {
sc_item_option1.value = employeeName[0];
sc_item_option1.update();
}
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2024 07:00 AM
Hello @suuriya ,
Glad to hear that it worked fine for you.🤗
To address your requirement, let me confirm if I understand correctly: you wish to generate multiple RITMs only when the "type" variable in the catalog form is selected as "add" or "modify," and not for the other two options, namely "remove" and "password." If this interpretation is accurate, I can certainly modify the script accordingly.
Please share your thoughts, and I'll proceed accordingly.
Thanks & Regards,
Aniket.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2024 07:10 AM
HI @Aniket Chavan ,
Exactly.
In catalog form there is select box field called What type of request is this?(add/modify/remove/password) if user selects add or modify and then if he select multiple options in select environment (list collector) then the ritms should split....if user select remove or pass and then if he select multiple options in select environment then only one ritm should be raised (oob expected)
split
no split
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2024 07:31 AM
Hello @suuriya ,
Got it, please try with the modified code below and see how it works for you, also don't forget to replace the backend name of your variable "What type of request is this?" and also values of the choices for "add or modify"
(function executeRule(current, previous /*null when async*/ ) {
// Check if "What type of request is this?" is set to "add" or "modify"
var requestType = current.variables.what_type_of_request_is_this;
if (requestType == 'add' || requestType == 'modify') {
var tcmc = '9a415a5187e0ca14755b33773cbb3516';
var name = current.variables.select_csos_environment.toString();
var employeeName = name.split(',');
var name1 = current.variables.select_csos_environment.getDisplayValue();
var employeeName1 = name1.split(',');
for (var i = 1; i < employeeName.length; i++) {
var gr1 = new GlideRecord('sc_req_item');
gr1.initialize();
gr1.cat_item = current.cat_item;
gr1.request = current.request;
gr1.short_description = "Termination: " + employeeName1[i];
gr1.requested_for = current.variables.requested_for; //replace variable name per your variable
gr1.insert();
GlideSysAttachment.copy('sc_req_item', current.sys_id, 'sc_req_item', gr1.sys_id);
var newRITM = gr1.sys_id;
var sc_item_option_mtom = new GlideRecord('sc_item_option_mtom');
sc_item_option_mtom.addQuery('request_item', current.sys_id);
sc_item_option_mtom.query();
while (sc_item_option_mtom.next()) {
var sc_item_option = new GlideRecord('sc_item_option');
sc_item_option.addQuery('sys_id', sc_item_option_mtom.sc_item_option);
sc_item_option.query();
var j = 0;
if (sc_item_option.next()) {
var sc_item_option_insert = new GlideRecord('sc_item_option');
sc_item_option_insert.initialize();
sc_item_option_insert.item_option_new = sc_item_option.item_option_new;
if (sc_item_option.item_option_new == "7fb719f21b208e9045fc99fa234bcb8b") { //list collector variable sys_id
var names = sc_item_option.value;
sc_item_option_insert.value = employeeName[i];
} else {
sc_item_option_insert.value = sc_item_option.value;
}
sc_item_option_insert.order = sc_item_option.order;
sc_item_option_insert.insert();
var sc_item_option_mtom_insert = new GlideRecord('sc_item_option_mtom');
sc_item_option_mtom_insert.initialize();
sc_item_option_mtom_insert.request_item = newRITM;
sc_item_option_mtom_insert.sc_item_option = sc_item_option_insert.sys_id;
sc_item_option_mtom_insert.insert();
j++;
}
}
}
current.short_description = "Termination: " + employeeName1[0];
current.requested_for = current.variables.requested_for; //replace variable name per your variable
current.update();
var sc_item_option_mtom1 = new GlideRecord('sc_item_option_mtom');
sc_item_option_mtom1.addQuery('request_item', current.sys_id);
sc_item_option_mtom1.query();
while (sc_item_option_mtom1.next()) {
var sc_item_option1 = new GlideRecord('sc_item_option');
sc_item_option1.addQuery('sys_id', sc_item_option_mtom1.sc_item_option);
sc_item_option1.addEncodedQuery('item_option_new=7fb719f21b208e9045fc99fa234bcb8b'); //list collector variable sys_id
sc_item_option1.query();
if (sc_item_option1.next()) {
sc_item_option1.value = employeeName[0];
sc_item_option1.update();
}
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2024 11:14 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 04:25 AM
Please try below updated code:
var opt = current.variables.select_csos_environment.toString().split(',');
for (var i = 0; i < opt.length; i++) {
if (opt[i].trim() !== '') { // Check if the option is selected
createRequest(opt[i].trim());
}
}
function createRequest(selectedOption) {
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('9a415a5187e0ca14755b33773cbb3516', 1);
cart.setVariable(item, 'employee_name', current.variables.employee_name);
cart.setVariable(item, 'what_type_of_request_is_this', current.variables.what_type_of_request_is_this);
cart.setVariable(item, 'select_csos_environment', selectedOption); // Set the selected option
var rc = cart.placeOrder();
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.