- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2023 08:13 AM
I have a glide list type field in one form need to remove on value from it without doing manually.
Can any one suggest background script to update same.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2023 10:49 AM - edited ‎01-02-2023 10:52 AM
OK, you just need to set the top 3 variables then:
- table: you want to set this to 'kb_knowledge_block'
- listField: you want to set this to 'can_read_user_criteria'
- whatToRemove: this is something I cannot tell you. Open the user criteria record that you want removed and then right-click, Copy sys_id, then insert into this variable. Make sure the quotes are maintained.
Script updated for your requirements (with additional explanation):
var table = ''; // Name of the table, e.g. kb_knowledge_block
var listField = ''; // Name of the list field to remove an element from, e.g. can_read_user_criteria
var whatToRemove = ''; // Sys_id of the element to remove from the list field, e.g. sys_id of user criteria
var encQ = listField + 'LIKE' + whatToRemove; // encoded query to limit queried record to those that contain the element we want to remove
var listArray = []; // initial array variable declaration
var elementIndex = -1; // initial index variable declaration
/* Run a GlideRecord query to find all records that contain the element to be deleted from the specifid list field */
var listGr = new GlideRecord(table);
listGr.addEncodedQuery(encQ);
listGr.query();
while(listGr.next()) {
listArray = listGr[listField].toString().split(','); // set the array variable based on the List field of the found record
elementIndex = listArray.indexOf(whatToRemove); // search for the element to remove from this particular record
/* Only try to remove the element and update the record if it was found, i.e. not -1 */
if(elementIndex > -1) {
listArray.splice(elementIndex,1);
listGr.setValue(listField,listArray);
listGr.update();
}
/* Reset the initial array related variables */
listArray = [];
elementIndex = -1;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2023 02:13 PM
Hi,
Refer below script
var valueToBeRemoved='anyxyzvalue';//SysID of one item from list that needs to be removed
var t=new GlideRecord('your_table_name_on_which_list_is_present');
t.query();
while(t.next())
{
var listTobeUpdate=t.getValue('backend_name_of_list_type_field');
var arr=listTobeUpdate.split(",");
var index = arr.indexOf(valueToBeRemoved);
if (index > -1) { // only splice array when item is found
arr.splice(index, 1); // 2nd parameter means remove one item only
}
var updatedList=arr.join(",");
t.setValue('backend_name_of_list_type_field',updatedList);
t.update();
}
Thanks and Regards,
Saurabh Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2023 02:13 PM
Hi,
Refer below script
var valueToBeRemoved='anyxyzvalue';//SysID of one item from list that needs to be removed
var t=new GlideRecord('your_table_name_on_which_list_is_present');
t.query();
while(t.next())
{
var listTobeUpdate=t.getValue('backend_name_of_list_type_field');
var arr=listTobeUpdate.split(",");
var index = arr.indexOf(valueToBeRemoved);
if (index > -1) { // only splice array when item is found
arr.splice(index, 1); // 2nd parameter means remove one item only
}
var updatedList=arr.join(",");
t.setValue('backend_name_of_list_type_field',updatedList);
t.update();
}
Thanks and Regards,
Saurabh Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-03-2023 08:46 AM - edited ‎01-03-2023 09:10 AM
Thanks this script seems working as expected.
The script is quire understandable for new bees as well
Thanks for help and proper explanation.
Helps much!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-03-2023 08:47 AM
did not get it
can you please elaborate?
Thanks and Regards,
Saurabh Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2023 06:14 PM
You can use below script
// Replace 'table_name' with the name of your table
// Replace 'field_name' with the name of your Glide List field
// Replace 'value_to_remove' with the value you want to remove from the field
var gr = new GlideRecord('table_name');
gr.addQuery('field_name', 'CONTAINS', 'value_to_remove');
gr.query();
while (gr.next()) {
// Get the current value of the field as a string
var fieldValue = gr.field_name.toString();
// Split the string into an array of values
var fieldValues = fieldValue.split(',');
// Remove the value from the array
var updatedValues = fieldValues.filter(function(val) {
return val !== 'value_to_remove';
});
// Join the array of values back into a string
var updatedValue = updatedValues.join(',');
// Update the field with the new value
gr.field_name = updatedValue;
gr.update();
}
This script will retrieve all records in the specified table where the Glide List field contains the specified value, and it will update the field to remove that value. Note that this script will only work if your Glide List field stores its values as a comma-separated string. If your field stores its values in a different format, you will need to modify the script accordingly.