Best practice for setting identity attributes through code

Hi all,

What is the best way to iterate over all identities and set an attribute through a Beanshell rule in IIQ

Can someone help me with the code as well.

Currently I doing the below:

  1. Using incrementaliterator and flushing at the end
  2. I am setting the attribute and saving using saveObject and commitTransaction

Is there anything that I need to add to this process.

Thanks in advance

Hi @rishavghoshacc

I have listed down some best practices:

  • IncrementalObjectIterator is a good choice for processing all identities.
  • Commit changes in batches of approximately 100–500 identities.
  • Do not commit after every identity because that creates excessive database transactions.
  • Do not wait until every identity is processed because the Hibernate cache and transaction can become very large.
  • Call context.decache() after each batch commit to release cached Identity objects.
  • Put Util.flushIterator() in a finally block so the database cursor is released even when an exception occurs.
  • Use context.rollbackTransaction() when processing fails.
  • Avoid saving identities whose attribute already has the required value.

Your approach is generally correct. some additional recommendations from my side would be:

  • Use an iterator to process identities one at a time instead of loading everything into memory.
  • Save changes in small batches rather than after every identity.
  • Clear cached objects periodically to keep memory usage low.
  • Update only the identities that actually need a change.
  • Add basic logging so you can track progress and identify failures.
  • Handle exceptions properly and roll back changes if an error occurs.
  • Test the rule on a small set of identities before running it against the entire population.

Hi, here’s an example…

import sailpoint.api.IncrementalObjectIterator;
import sailpoint.object.Filter;
import sailpoint.object.Identity;
import sailpoint.object.QueryOptions;
import sailpoint.tools.Util;

int BATCH_SIZE = 50;
int processed = 0;
int updated = 0;
int pending = 0;

QueryOptions qo = new QueryOptions();
qo.addFilter(Filter.eq("workgroup", new Boolean(false)));
qo.setOrderBy("name");

IncrementalObjectIterator it = null;

try {
    it = new IncrementalObjectIterator(context, Identity.class, qo);

    while (it.hasNext()) {
        Identity identity = (Identity) it.next();
        processed++;

        String desired = "MY_VALUE";     // your logic here
        
        identity.setAttribute("myAttr", desired);
        context.saveObject(identity);
        updated++;
        pending++;

        if (pending >= BATCH_SIZE) {
            context.commitTransaction();
            context.decache();            // must come AFTER the commit
            pending = 0;
        }
    }

    if (pending > 0) {
        context.commitTransaction();
        context.decache();
    }
}
catch (Exception e) {
    context.rollbackTransaction();
    throw e;
}
finally {
    Util.flushIterator(it);               // always release the cursor
}

return "processed=" + processed + ", updated=" + updated;

Hello Rishav. The iterator approach you have looks right for a bulk update. One additional thing I would check before running it is whether this attribute has a source configured under Global Settings → Identity Mappings.

If the attribute is derived from an Application Attribute, Application Rule, or Global Rule, I would first confirm whether updating it directly is the intended design. IIQ uses those source mappings to derive identity attribute values, and Refresh identity attributes can update the Identity Cube based on those mappings.

If the value itself should be calculated by IIQ, it may be cleaner to implement that logic in the appropriate identity source mapping and let the normal aggregation/refresh processing maintain it.

If this is simply a custom identity attribute that you intentionally maintain through code and it has no source mapping that will recalculate it, then the batched IncrementalObjectIterator approach suggested above should be a good fit.

hi @rishavghoshacc please check below article, it will give you more idea on this. Also please use decache after every 50 or 100 record.
Generally if you are saving also then sailpoint recommend to lock the identity object and then save and then unlock. If lock then wait for some time.
Check this community Page SailPoint Compass Community

@rishavghoshacc What attribute are you setting? For one of the project, we did this using IdentityRefresh prerefresh rule.. You can create a population and assign to the task on which you need to set the attributes, then just use setAttribute to set the value.. no need to iterate or saveObject or commit transaction, and don’t return any thing. just do setAttribute that’s it..

Another advantage is, as it is refresh it can be partitioned to sped up the execution.

@neel193 Where can I find the prerefresh rule?

@rishavghoshacc Please check this article: Rules in IdentityIQ - Compass. Check Page no. 44 for the sample.