- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2022 05:03 AM
We have a Catalog Item with various Reference and List Collector variables that all pull values from the same underlying tables (each have different criteria). So for example, let's say that we have the following three variables in our Catalog Item (in actuality, there are more fields):
- Field1 (Reference field from Table1)
- Field2 (List Collector field from Table1)
- Field3 (List Collector field from Table1)
To simplify work I need to do in the workflow, I would like to dynamically create a single variable called "AllSelections", which combines the selections made from all the fields listed above into one single list (this variable will obviously also be a List Collector from Table1). So the plan is to create an "OnSubmit" Catalog Client Script that will dynamically populate this "AllSelections" List Collector variable with all the selections from each of the three fields listed above at the time that they submit the request.
However, I am unsure the best way of combining them all into a single variable, whether I should be working with an array, or convert the values to strings and then try to combine them. Can anyone provide any assistance on how to do this?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2022 06:04 AM
Server scripts are always more powerful and easier to work with than client scripts. In this case, the code might not be that much different, but since you don't need to see/work with the combined value on the client session / before the form is submitted, I would recommend doing this with a workflow Run Script as the first activity. The other advantage to this approach is that you can populate a workflow.scratchpad variable with the new (string) value then use that throughout the script, so you don't need to create a new Catalog Item variable, unless you really want to see the combined value on the RITM or a Catalog Task. In any event, each of these three variable values is stored as a sys_id of (a) record(s) on the same table, with the List Collector values being a string of comma-separated sys_ids when more than one record is selected, so all you need to do is push each value into a new array, then use that array to populate your workflow variable, or your new Catalog Item List Collector or text variable. Here's one way to do that:
var newArr = [];
newArr.push(current.variables.var1.toString()); //add Field1 reference value to array
var field2 = current.variables.var2.toString();
field2 = field2.split(',');
var field3 = current.variables.var3.toString();
field3 = field3.split(',');
for (var i = 0; i < field2.length; i++) {
newArr.push(field2[i]); //add Field2 list collector records to array
}
for (var j = 0; j < field3.length; j++) {
newArr.push(field3[j]); //add Field2 list collector records to array
}
workflow.scratchpad.allSelections = newArr.join(','); //populate a variable that can be used throughout the workflow
//current.variables.allSelections = newArr.join(','); //use this line instead to populate a Catalog Item variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2022 06:04 AM
Server scripts are always more powerful and easier to work with than client scripts. In this case, the code might not be that much different, but since you don't need to see/work with the combined value on the client session / before the form is submitted, I would recommend doing this with a workflow Run Script as the first activity. The other advantage to this approach is that you can populate a workflow.scratchpad variable with the new (string) value then use that throughout the script, so you don't need to create a new Catalog Item variable, unless you really want to see the combined value on the RITM or a Catalog Task. In any event, each of these three variable values is stored as a sys_id of (a) record(s) on the same table, with the List Collector values being a string of comma-separated sys_ids when more than one record is selected, so all you need to do is push each value into a new array, then use that array to populate your workflow variable, or your new Catalog Item List Collector or text variable. Here's one way to do that:
var newArr = [];
newArr.push(current.variables.var1.toString()); //add Field1 reference value to array
var field2 = current.variables.var2.toString();
field2 = field2.split(',');
var field3 = current.variables.var3.toString();
field3 = field3.split(',');
for (var i = 0; i < field2.length; i++) {
newArr.push(field2[i]); //add Field2 list collector records to array
}
for (var j = 0; j < field3.length; j++) {
newArr.push(field3[j]); //add Field2 list collector records to array
}
workflow.scratchpad.allSelections = newArr.join(','); //populate a variable that can be used throughout the workflow
//current.variables.allSelections = newArr.join(','); //use this line instead to populate a Catalog Item variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2022 06:29 AM
It doesn't really matter to me whether this is done at the Catalog Item level, or the workflow level, so this works perfectly. Thank you very much!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2022 07:52 AM
Brad, I have come across a minor issue that I cannot seem to figure out how to fix. Not all these fields are required, so some of the List Collectors may be empty. Thus, I am sometimes getting values with multiple blanks in them (note the three commas in a row in the middle of the string), i.e.
975ff4b21b779d54f22a99ba234bcbee,,,5f5ff4b21b779d54f22a99ba234bcbf6,d75ff4b21b779d54f22a99ba234bcbf6
I thought I could avoid that by wrapping each loop if an IF statement, but it did not seem to work. Here is my current code:
var newArr = [];
newArr.push(current.variables.add_type_of_access_requested.toString());
var field2 = current.variables.add_folder_access.toString();
field2 = field2.split(',');
var field3 = current.variables.add_data_source_access.toString();
field3 = field3.split(',');
var field4 = current.variables.add_secured_data_access.toString();
field4 = field4.split(',');
if(field2.length>0){
for (var i = 0; i < field2.length; i++) {
newArr.push(field2[i]); //add Field2 list collector records to array
}
}
if(field3.length>0){
for (var j = 0; j < field3.length; j++) {
newArr.push(field3[j]); //add Field2 list collector records to array
}
}
if(field4.length>0){
for (var k = 0; k < field4.length; k++) {
newArr.push(field4[k]); //add Field2 list collector records to array
}
}
workflow.scratchpad.allSelections = newArr.join(','); //populate a variable that can be used throughout the workflow
gs.log(workflow.scratchpad.allSelections, 'JOE99'); //return built string to log file
Do you now how I can adjust my code so it doesn't return empty values like that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2022 08:00 AM
I think I figured it out. Changing each instance of ">0" to ">1" seemed to fix the issue.
Thanks