Remove user from List Type Field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2024 02:38 AM
Hi,
I wanted to remove user from list type field, but facing some issue. I have written an after update Business Rule and the code is here.
(function executeRule(current, previous /*null when async*/ ) {
var ReqFor = current.contact;
gs.addInfoMessage(ReqFor);
var BuyID = current.getValue('u_buy_id');
gs.addInfoMessage(BuyID);
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', ReqFor);
gr.query();
while (gr.next()) {
gs.addInfoMessage('record found');
var role = gr.role; //Will give the sys_id of the roles
//gs.addInfoMessage(role);
if (role == '9d30a5b11bc331100f52a797b04bcb9e') {
gs.addInfoMessage('role found');
var bu = new GlideRecord('sn_customerservice_buying_records');
//bu.addQuery('number', BuyID);
bu.addEncodedQuery('sys_id=1ae066101bc88210ba40eca6bd4bcbed');
bu.query();
while (bu.next()) {
gs.addInfoMessage('currently in buying event');
//Remove Supplier user from buying id
var ary1 = bu.u_supplier_user.toString().split(',');
gs.addInfoMessage(ary1);
if (ary1.indexOf(ReqFor) > -1) {
gs.addInfoMessage('inside array if');
var index1 = ary1.indexOf(ReqFor);
ary1.splice(index1, 1);
bu.u__supplier_user = ary1.toString();
bu.update();
}
}
}
}
})(current, previous);
the line "gs.addInfoMessage('inside array if');" is no showing the records from the array. it is showing some message as below.
Thanks
Aruna
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2024 03:19 AM
To remove a value from array you can do something like this:
var list = "Testvalue,Testvalue2,Test".split(",");
gs.info(list);
for(var i in list){
if(list[i] == "Testvalue2"){
list.splice(i,i);
}
}
gs.info(list);
Try that as a background script to see how it works.
Now lets see what you're trying to do. You're checking the role for certain user and based on that you take the value from field "bu.u_supplier_user" which I assume is a list field containing a comma separated list of users. You're trying to update field " bu.u__supplier_user" instead of " bu.u_supplier_user".
I made some changes to your script, but didn't test it so please check it carefully and see if it gives you an idea how to work with it.
I changed some variables, added the role to the role query and changed how you remove the reqFor from the array.
I also added a check that compares the old value and new values, but you could also just use indexOf to check it the new list contains the user.
(function executeRule(current, previous /*null when async*/ ) {
var reqFor = current.getValue('contact');
gs.addInfoMessage(reqFor);
var buyID = current.getValue('u_buy_id');
gs.addInfoMessage(buyID);
var grRole = new GlideRecord('sys_user_has_role');
grRole.addQuery('user', reqFor);
grRole.addQuery('role', "9d30a5b11bc331100f52a797b04bcb9e");
grRole.query();
if (grRole.next()) {
gs.addInfoMessage('Role record found for ' + reqFor);
var bu = new GlideRecord('sn_customerservice_buying_records');
bu.addEncodedQuery('sys_id=1ae066101bc88210ba40eca6bd4bcbed');
bu.query();
while (bu.next()) {
gs.addInfoMessage('Looping buying events');
//Remove Supplier user from buying id
var oldSuppliers = bu.u_supplier_user.toString();
var suppliers = bu.u_supplier_user.toString().split(','); //Array of supplier users.
for (var i in suppliers) {
if (suppliers[i] == reqFor) {
suppliers.splice(i, i);
}
}
if (oldSuppliers != suppliers.join(',')) {
bu.u_supplier_user = suppliers.join(',');
bu.update();
}
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2024 03:31 AM - edited 01-29-2024 03:31 AM
I tried your code, just check below things
gs.addInfoMessage(ary1);
just replaced with gs.addInfoMessage("Array Value-----"+ary1);
because value in array show morzilla javascript.
Try once and code will surely execute forward.
Please check and let me know if it will help you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2024 03:53 AM - edited 01-29-2024 05:51 AM
@Weird , Thanks for the guidance. It works fine!
Just a small issue. I have 2 users, I tested with both the users but it is removing user1, not the user2 even the conditions met. What might be the reason?
Note: I'm getting the InfoMessages for user2 as well.
Thanks