Hi, I am making forms and in a field I need to display assigned roles of an account which is under selected Application.
I am using below script for the same but the roles are not getting displayed in the field.
What am I doing wrong here?
<Field displayName="Remove Roles" dynamic="true" multi="true" name="removeRoles" postBack="true" type="string">
<AllowedValuesDefinition>
<Script>
<Source>
import sailpoint.object.Application;
import sailpoint.object.Bundle;
import sailpoint.object.Identity;
import sailpoint.object.Link;
import sailpoint.object.RoleAssignment;
import sailpoint.tools.Util;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Set roleSet = new HashSet(); // Using Set to avoid duplicates
// Fetch selected application
String appName = form.getField("applicationName").getValue();
if (Util.isNullOrEmpty(appName)) {
return new ArrayList(roleSet); // No application selected
}
Application selectedApp = context.getObjectByName(Application.class, appName);
if (selectedApp == null) {
return new ArrayList(roleSet); // Application does not exist
}
// Fetch selected account
String selectedAccount = form.getField("selectAccount").getValue();
if (Util.isNullOrEmpty(selectedAccount)) {
return new ArrayList(roleSet); // No account selected
}
// Extract the account identifier from "Application Name : Account Name" format
String[] accountParts = selectedAccount.split(" : ");
if (accountParts.length < 2) {
return new ArrayList(roleSet); // Invalid format
}
String accountId = accountParts[1].trim();
// Get identity object (current user)
Identity identity = context.getObjectByName(Identity.class, context.getUserName());
if (identity == null) {
return new ArrayList(roleSet); // Identity not found
}
// Fetch accounts (links) for this identity
List links = identity.getLinks();
if (links != null && links.size() > 0) {
for (int i = 0; i < links.size(); i++) {
Link link = (Link) links.get(i);
if (link.getNativeIdentity().equals(accountId) && link.getApplication().getName().equals(appName)) {
// Fetch assigned roles
List roleAssignments = link.getRoleAssignments();
if (roleAssignments != null) {
for (int j = 0; j < roleAssignments.size(); j++) {
RoleAssignment roleAssignment = (RoleAssignment) roleAssignments.get(j);
Bundle role = roleAssignment.getRole();
if (role != null) {
roleSet.add(role.getName()); // Add assigned role to the set
}
}
}
}
}
}
// Fetch detected roles
List detectedRoles = identity.getDetectedRoles();
if (detectedRoles != null) {
for (int k = 0; k < detectedRoles.size(); k++) {
Bundle detectedRole = (Bundle) detectedRoles.get(k);
if (detectedRole != null) {
String detectedRoleApp = (String) detectedRole.getAttribute("application"); // Get application name
if (detectedRoleApp != null && detectedRoleApp.equals(appName)) {
roleSet.add(detectedRole.getName()); // Add detected role to the set
}
}
}
}
if (roleSet.isEmpty()) {
roleSet.add("No Roles Found");
}
return new ArrayList(roleSet); // Return list of roles (assigned + detected)
</Source>
</Script>
</AllowedValuesDefinition>
</Field>