The CreatorCon Call for Content is officially open! Get started here.

Gliderecord deleteRecord deleting multiple files

julienrouge
Tera Contributor

I'm working on a business rule that activates when a field value is changed. What it should do is add the user to a group if it fulfills a certain condition and removes the previous user if there name is no longer on any field on the table. The script adds the user to the group successfully but, when it comes to removing the previous record, it succeeds but then continues to remove  groups from  a lot of random users unrelated to the query. Below is what is currently written for the removal script, bear in mind this is after modifying it to try and ensure it only removes one record to no avail. Is there something wrong with the script or is this an issue with the deleteRecord function itself?

  if (!previousRec.hasNext()) {
        gs.addInfoMessage("User has no other programs, removing them from the system");
        var removeQuery = new GlideRecord("sys_user_grmember");
        removeQuery.addQuery('user', previous.u_primary_lead);
        removeQuery.addQuery('group', DashboardRole);
        removeQuery.setLimit(1);
        removeQuery.query();
        gs.log("BLT: removeQuery " +  removeQuery.hasNext());
        if (removeQuery.hasNext()) {
            removeQuery.next();
            gs.log("BLT: removing user " + previous.u_primary_lead)
            gs.addInfoMessage("removing user " + previous.u_primary_lead + " from Group " + removeQuery.group.getDisplayValue() );
            if( removeQuery.group == DashboardRole && removeQuery.user == previous.u_primary_lead ){
                removeQuery.deleteRecord();
                removeQuery.update();
            }
        }
    }

 

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

Assuming DasboardRole is correctly declared and assigned earlier in the script, simplify the end by removing the unnecessary update(), hasNext(), and if statement:

...
removeQuery.query();
if (removeQuery.next()) {
            gs.log("BLT: removing user " + previous.u_primary_lead)
            gs.addInfoMessage("removing user " + previous.u_primary_lead + " from Group " + removeQuery.group.getDisplayValue() );
            removeQuery.deleteRecord();
}

 

Could you explain further as to why this could fix the issue? It risky to test it due to the data loss so I want to make sure it'll work if we implement this