Hey All ! . The requirement is to write a code to select 2 random people from the same department.Can someone help me with a rule i made a rule as
<?xml version='1.0' encoding='UTF-8'?>
Selects two random identities from a specified department.
import sailpoint.api.SailPointContext;
import sailpoint.object.Identity;
import sailpoint.tools.GeneralException;
import sailpoint.object.QueryOptions;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
// Log start of execution
log.info("Executing SelectTwoRandomFromDepartment Rule");
// SailPointContext is already available in rules, no need for request.getContext()
// Department Name (Set dynamically or statically)
String departmentName = "IT"; // Change as needed
// Query identities based on department
List<Identity> identityList = new ArrayList<>();
try {
QueryOptions qo = new QueryOptions();
qo.addFilter(QueryOptions.Filter.eq("Dept_name", departmentName)); // Ensure correct attribute name
Iterator<Identity> iterator = context.search(Identity.class, qo);
while (iterator.hasNext()) {
identityList.add(iterator.next());
}
} catch (GeneralException e) {
log.error("Error fetching identities: " + e.getMessage());
}
// Ensure at least two identities exist
if (identityList.size() < 2) {
log.warn("Not enough identities found in department: " + departmentName);
return null; // Not enough identities
}
// Shuffle and select two random identities
Collections.shuffle(identityList);
List<Identity> selectedIdentities = identityList.subList(0, 2);
// Log selected identities
log.info("Selected Identities: " + selectedIdentities);
// Return selected identities
return selectedIdentities;
but this returns an error : Exception running rule: BeanShell script error: bsh.ParseException: Parse error at line 18, column 53. Encountered: > BSF info: SelectTwoRandomFromDepartment2 at line: 0 column: columnNo
below line u need to coorect like this qo.add(Filter.eq(“Dept_name”,“departmentname”));
replace below line with above line.
qo.addFilter(QueryOptions.Filter.eq(“Dept_name”, departmentName)); // Ensure correct attribute name
Also it seems you are using"Dept_name" , i doubt u have name with this.
As you just need 2 identies from same department simple project query where pass filter option in searchable department name
In SailPoint IdentityIQ, rules are typically written as BeanShell scripts. The line:
List<Identity> identityList = new ArrayList<>();
is causing the parser to choke on the angle brackets (< and >). BeanShell (especially the version used by IdentityIQ) does not always support the Java 7 “diamond” operator (<>) and also requires that generics be handled carefully (or sometimes avoided).
How to Fix the Error
Avoid the Diamond Operator
BeanShell (especially older versions used by IdentityIQ) may not fully support Java 7+ syntax like the diamond operator. Instead of:
List<Identity> identityList = new ArrayList<>();
use:
List identityList = new ArrayList();
since BeanShell can be finicky about generics, you can drop them completely:
Final Working Example
import sailpoint.api.SailPointContext;
import sailpoint.object.Identity;
import sailpoint.tools.GeneralException;
import sailpoint.object.QueryOptions;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
log.info("Executing SelectTwoRandomFromDepartment Rule");
String departmentName = "IT"; // or pass in as a variable, etc.
List identityList = new ArrayList();
try {
QueryOptions qo = new QueryOptions();
qo.addFilter(QueryOptions.Filter.eq("Dept_name", departmentName));
Iterator it = context.search(Identity.class, qo);
while (it.hasNext()) {
identityList.add(it.next());
}
} catch (GeneralException e) {
log.error("Error fetching identities: " + e.getMessage());
}
if (identityList.size() < 2) {
log.warn("Not enough identities found in department: " + departmentName);
return null; // Or however you want to handle it
}
// Shuffle and select two random identities
Collections.shuffle(identityList);
List selectedIdentities = identityList.subList(0, 2);
log.info("Selected Identities: " + selectedIdentities);
return selectedIdentities;
Yes i also tried this works on fine thanks much , but if we want to take the user department instead of manually giving it as HR or whatever , so what shall i use in such case since String departmentName = args.get(“department”); is not working in this case
Yes i also tried this works on fine thanks much , but if we want to take the user department instead of manually giving it as HR or whatever , so what shall i use in such case since String departmentName = args.get(“department”); is not working in this case
Thank you for the update. I would appreciate it if you could accept my proposed resolution code as a solution if that addresses your code issue.
Additionally, if you could share the XML file, I would be able to assist you in sending dynamic values. you can remove any sensitive information from the XML file before sharing it here.