- 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-27-2024 05:49 AM
HI @Aniket Chavan ,
Thanks for the reply
can you please let me know have written BR In sc_req_item table and in script what is the sysid because I'm bit confused my seeing the script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 05:57 AM
Hello @suuriya ,
The business rule should be on Requested Item (sc_req_item) table only for after insert and in the business rule script in the tcmc variable you can add the sys_id of your catalog item and then just replace the variable name with yours in place of "name" after that set the field values based on your requirement.
Let me know if you still have any doubts or confusion, so that we can discuss that accordingly.
Thanks & Regards,
Aniket
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2024 06:53 AM
HI @Aniket Chavan ,
Thanks it worked based on the number of options selected in list collector only that many ritms triggered but if you see in list collector all selected options are present in all ritm....
and i also see for one ritm requested for and short desc field is empty
script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2024 04:28 AM
Hello @suuriya ,
There are few mistakes in the code, so I rectified and now please try with the code below and see how it works for you,
(function executeRule(current, previous /*null when async*/ ) {
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 06:48 AM
HI @Aniket Chavan ,
Thank you so much it is working as expected.
But my only thing is in the catalog form there is field called type (add/modify/remove/password) but this spliting of ritms should only work for add and modify can you please let me know how can we add this in script and also can we get the variable details in "when to run" condition of BR? so that i can add the condition in when to run if not can you please help me with script