Rule to select 2 random people from the same department

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&lt;Identity> identityList = new ArrayList&lt;>();

    try {
        QueryOptions qo = new QueryOptions();
        qo.addFilter(QueryOptions.Filter.eq("Dept_name", departmentName));  // Ensure correct attribute name

        Iterator&lt;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() &lt; 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&lt;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

Can anyone help in this regard

Hi @uditsahntl01
Can you provide xml file?
thanks

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

2 Likes

Hello @uditsahntl01 ,

I tested the use case with the following code, and it worked for me. I noticed a few glitches in your code.

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule created="" id="" language="beanshell" modified="" name="SIVA10" significantModified="">
  <Source>
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;

  import sailpoint.object.Identity; 
  import sailpoint.object.QueryOptions;
  import sailpoint.object.Filter;

  import java.util.List; 
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Iterator;

  List&lt;Identity> identityList = new ArrayList&lt;Identity>();
  try {
    // 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


    QueryOptions qo = new QueryOptions(); 


    qo.addFilter(Filter.eq("Dept_name", departmentName));  // Ensure correct attribute name

    List identityList = context.getObjects(Identity.class,qo);

    // Ensure at least two identities exist
    if (identityList.size() &lt; 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&lt;Identity> selectedIdentities = identityList.subList(0, 2);

    // Log selected identities
    log.info("Selected Identities: " + selectedIdentities);

    // Return selected identities
    return selectedIdentities;

  } catch(Exception e) {
    e.printStackTrace();
  }

  </Source>
</Rule>

You may use this code if it fits your use case. Please mark this as a solution if it resolves your question.

Thanks
@SivaLankapalli

1 Like

@uditsahntl01 -

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;

Hope this helps.

1 Like

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

Hi @uditsahntl01,

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.

Thanks,
@SivaLankapalli

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