# Approval for deleting Account

**URL:** <https://developer.sailpoint.com/discuss/t/approval-for-deleting-account/104873>\
**Category:** IIQ Discussion and Questions\
**Tags:** accounts, workflows, access-requests, rules, attributes, identityiq, provisioning\
**Created:** [March 19, 2025, 7:16pm UTC](https://developer.sailpoint.com/discuss/t/approval-for-deleting-account/104873 "2025-03-19T19:16:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shubhangani\_Kharayat](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@Shubhangani\_Kharayat](https://developer.sailpoint.com/discuss/u/Shubhangani_Kharayat)\
**Post date:** [March 19, 2025, 7:16pm UTC](https://developer.sailpoint.com/discuss/t/approval-for-deleting-account/104873/1 "2025-03-19T19:16:30Z")

</div>

Want to send approval (as of now i m sending approval to random users of the deleting identity’s department) when i m deleting a account.  
Below is the code plz do the needfull correction in this:  
import sailpoint.object._;  
import sailpoint.api._;  
import sailpoint.tools._;  
import java.util._;  
import ProvisioningPlan.\*;

log.error(“Executing SelectTwoRandomUsersFromDepartment Rule”);

// Obtain SailPoint context  
SailPointContext context = sailpoint.api.SailPointFactory.getCurrentContext();

// Fetch the current provisioning plan  
//Map args = context.getArguments();

if (args == null) {  
log.error(“Args map is null. Rule execution stopped.”);  
return null;  
}  
ProvisioningPlan plan = getArgs(“plan”);  
if (plan == null) {  
log.error(“No provisioning plan provided.”);  
return null;  
}

// Check if there is an AccountRequest for Active Directory with DELETE operation  
boolean isDeleteOperation = false;  
for (AccountRequest acctReq : plan.getAccountRequests()) {  
if (“Active Directory”.equals(acctReq.getApplication()) &&  
AccountRequest.Operation.Delete.equals(acctReq.getOperation())) {  
isDeleteOperation = true;  
break;  
}  
}

if (!isDeleteOperation) {  
log.error(“Skipping rule execution as the operation is not DELETE.”);  
return null;  
}

// Fetch the current identity  
Identity currentIdentity = context.getObject(Identity.class, plan.getIdentity());  
if (currentIdentity == null) {  
log.error(“Current identity not found.”);  
return null;  
}

log.error("Current identity: " + currentIdentity.getName());

// Fetch all links for the identity and find AD account  
List links = currentIdentity.getLinks();  
Link adAccount = null;  
for (Link link : links) {  
if (“Active Directory”.equals(link.getApplicationName())) {  
adAccount = link;  
break;  
}  
}

if (adAccount == null) {  
log.error("AD account not found for identity: " + currentIdentity.getName());  
return null;  
}

log.error("AD account found for identity: " + currentIdentity.getName());

// Retrieve department from AD account  
String departmentName = adAccount.getAttribute(“department”);  
if (Util.isEmpty(departmentName)) {  
log.error("Department name is missing for identity: " + currentIdentity.getName());  
return null;  
}

// Query identities in the same department  
QueryOptions qo = new QueryOptions();  
qo.addFilter(Filter.eq(“department”, departmentName));

List identityList = new ArrayList();  
Iterator iterator = context.search(Identity.class, qo);  
while (iterator.hasNext()) {  
Identity identity = iterator.next();  
if (!identity.getName().equals(currentIdentity.getName())) {  
identityList.add(identity);  
}  
}

log.error(“Total identities found in department '” + departmentName + "': " + identityList.size());

// Ensure at least two identities exist  
if (identityList.size() \< 2) {  
log.error("Not enough identities found in department: " + departmentName);  
return null;  
}

// Shuffle and select two random identities  
Collections.shuffle(identityList);  
Identity selectedIdentity1 = identityList.get(0);  
Identity selectedIdentity2 = identityList.get(1);

log.error("Selected identity 1: " + selectedIdentity1.getName());  
log.error("Selected identity 2: " + selectedIdentity2.getName());

// Return the selected identities for approval workflow  
List selectedIdentities = new ArrayList();  
selectedIdentities.add(selectedIdentity1.getName());  
selectedIdentities.add(selectedIdentity2.getName());

log.error("Selected identities: " + selectedIdentities);

return selectedIdentities;

Please revert back me the correct code.

---

<div class="post-metadata">

**Author:** ![HemantSingh](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/hemantsingh/32/6737_2.png) [@HemantSingh](https://developer.sailpoint.com/discuss/u/HemantSingh)\
**Post date:** [March 20, 2025, 2:25am UTC](https://developer.sailpoint.com/discuss/t/approval-for-deleting-account/104873/2 "2025-03-20T02:25:27Z")

</div>

what correction you are looking for. I do not see any code in this which is sending approval , its looking for some users and then selecting randomly 2 users and what’s its doing with those is not clear in this code. do you want this for all users..?

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sailpoint/original/2X/f/f2136700ed5e3703e0b85e02f6be799dacca7735.png) [@system](https://developer.sailpoint.com/discuss/u/system)\
**Post date:** [May 19, 2025, 2:25am UTC](https://developer.sailpoint.com/discuss/t/approval-for-deleting-account/104873/3 "2025-05-19T02:25:45Z")

</div>

Closed on May 19, 2025, 2:25am UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
