Based on List field, how to add or remove approvers in approvers Related list using bussiness rule??

_Omkar_sai_
Tera Contributor

Hi ,
I'm Omkar, I have a requirement to do dynamically add or remove approvers in approvers related list based on list field using business rule. Could you please suggest me solution for this requirement . I was able to do insertion but unable to do removing the approvers based on that list field dynamically. If I write GlideRecord.deleteRecord()  or GlideRecord.multiple()   (table is sysapproval_approver), it's showing an error like following picture even I had given  delete  operation access to my scoped application for Removing.

PeddintiOmkars_0-1673619086614.png

 

For Example: 
I added Fred Luddy , Abraham Lincoln, Abel Tuter in the list field , then those should be added in approvers related list . If I remove Abel Tuter from the list field ,then he should be removed from the approvers related list. 

 

Please suggest any solution for this or suggest me any approaches for this requirement.

1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

I would suggest you don't delete the approvers but instead change its state to 'no longer needed'. That would leave the perfect audit trail as well for record keeping.

-Anurag

View solution in original post

5 REPLIES 5

Hemant Kumar Ch
Kilo Sage

Try this below Script 

 

// Define the table and fields
var table = 'incident';
var listField = 'list_field';
var approversField = 'approvers';

// Get the current record
var gr = new GlideRecord(table);
if (gr.get(current.sys_id)) {

  // Get the current list of approvers
  var currentApprovers = gr.getValue(approversField);

  // Get the new list of names from the list field
  var newList = gr.getValue(listField).split(',');

  // Iterate over each name in the new list
  for (var i = 0; i < newList.length; i++) {
    var name = newList[i].trim();

    // Check if the name is already in the approvers list
    if (currentApprovers.indexOf(name) < 0) {

      // Add the name to the approvers list
      currentApprovers.push(name);
    }
  }

  // Iterate over each name in the current approvers list
  for (var i = currentApprovers.length - 1; i >= 0; i--) {
    var name = currentApprovers[i];

    // Check if the name is not in the new list
    if (newList.indexOf(name) < 0) {

      // Remove the name from the approvers list
      currentApprovers.splice(i, 1);
    }
  }

  // Update the record with the new list of approvers
  gr.setValue(approversField, currentApprovers.join(','));
  gr.update();
}

 

Hemant Kumar Ch 

Ciena -Dev