@Learner
If I understand correctly you need the code of Advanced Policy rule to restrict below conditions
User shouldn’t have more than one entitlement at given point of time, irrespective of creation of account, account modification. below code will help you on that
import sailpoint.object.Policy;
import sailpoint.object.PolicyViolation;
import sailpoint.object.Identity;
import sailpoint.object.Entitlement;
import sailpoint.object.Filter;
import sailpoint.object.Apllication;
import sailpoint.object.QueryOptions;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Locale;
import sailpoint.object.Link;
public List getIdentityEntitlements(Identity identity,String appName) {
List applicationLinks = identity.getLinks();
List entitlementList = new ArrayList();
if(applicationLinks != null && applicationLinks.size() >0 ) {
for (Link link:applicationLinks) {
if(appName.equals(link.getApplicationName())) {
entitlementList = link.getEntitlements(Locale.getDefault(),"");
}
}
}
return entitlementList;
}
public boolean isLinkPresent(String identityName, String appName) {
boolean isLinkPresent = false;
QueryOptions qo = new QueryOptions();
qo.addFilter(Filter.eq(“identity.name”, identityName));
qo.addFilter(Filter.eq(“application.name”, appName));
Iterator itr = context.search(Link.class, qo);
if (itr.hasNext()) {
isLinkPresent = true;
}
return isLinkPresent;
}
PolicyViolation policyViolation = null;
Identity expectedIdentity = identity;
Identity oldIdentity = context.getObjectByName(Identity.class, identity.getName());
String appName= “Your App Name”;
List expectedEnt = getIdentityEntitlements(expectedIdentity, appName);
List existingEnt = getIdentityEntitlements(oldIdentity, appName);
boolean isViolation = false;
boolean isLinkPresent = isLinkPresent(identity.getName(), appName);
if (expectedEnt != null && expectedEnt.size() >1) {
isViolation = true;
}
//if identity have the more than required entitlements then flag violation
if (isViolation) {
policyViolation = new PolicyViolation();
policyViolation.setActive(true);
policyViolation.setIdentity(identity);
policyViolation.setPolicy(policy);
policyViolation.setStatus(sailpoint.object.PolicyViolation.Status.Open);
}
return policyViolation;
Please mark the solution so that this helps others as well if this resolves your issue