How are we supposed to export results to csv file

i tried filewriter but i am getting error so

        QueryOptions qo = new QueryOptions();
        Iterator it = context.search(Bundle.class, qo);
	 String roleNames = "";

        while (it.hasNext()) {
            Bundle bundle = it.next();
            List profiles = bundle.getProfiles();

            for (Profile profile : Util.safeIterable(profiles)) {
                List constraints = profile.getConstraints();

                for (Filter cons : Util.safeIterable(constraints)) {
                    Application app = profile.getApplication();
                    if (app != null) {
                        Schema accountSchema = app.getAccountSchema();

                        if (accountSchema != null) {
                            if (cons instanceof Filter.LeafFilter) {
                                String property = cons.getProperty();
                                      Object value = ((Filter.LeafFilter) cons).getValue();
                        	
                        	List valueList = new ArrayList();        	
                        // Check if the value is a List or String
                        if (value instanceof List) {
                            valueList.addAll((List) value);
                        } else if (value instanceof String) {
                            valueList.add((String) value);
                        }
                        for (String entName : valueList) {
                            if (property != null && entName != null) {
                                // Check if the entitlement exists in the ManagedAttribute catalog
                                ManagedAttribute ent = ManagedAttributer.get(context, app, property, entName);
                                if (ent == null) {
                                    // Entitlement is missing in the catalog
                                    roleNames += String.format("%s - Missing Entitlement: %s\n", bundle.getName(), entName);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Hi @autorun6464,

can you share the error?
I cant see the filewriter.
Are close correctly all the {}?

1 Like

ok here my code with file writer. but the error i get is

ERROR sailpoint.api.TaskManager:1007 - Exception: [sailpoint.tools.GeneralException: BeanShell script error: bsh.ParseException: Parse error at line 21, column 5. Encountered: ( BSF info: AIZ-TaskRule-RoleEntitlementMissing at line: 0 column: columnNo]
java.lang.Exception: sailpoint.tools.GeneralException: BeanShell script error: bsh.ParseException: Parse error at line 21, column 5. Encountered: ( BSF info: AIZ-TaskRule-RoleEntitlementMissing at line: 0 column: columnNo

QueryOptions qo = new QueryOptions();
Iterator it = context.search(Bundle.class, qo);

// CSV file name
String fileName ="exported_roles.csv";

try (FileWriter writer = new FileWriter(fileName)) {
    // Write CSV header
    writer.append("Role Name,Missing Entitlement\n");

    while (it.hasNext()) {
        Bundle bundle = it.next();
        List profiles = bundle.getProfiles();

        for (Profile profile : Util.safeIterable(profiles)) {
            List constraints = profile.getConstraints();

            for (Filter cons : Util.safeIterable(constraints)) {
                Application app = profile.getApplication();
                if (app != null) {
                    Schema accountSchema = app.getAccountSchema();

                    if (accountSchema != null) {
                        if (cons instanceof Filter.LeafFilter) {
                            String property = cons.getProperty();
                            Object value = ((Filter.LeafFilter) cons).getValue();

                            List valueList = new ArrayList();
                            // Check if the value is a List or String
                            if (value instanceof List) {
                                valueList.addAll((List) value);
                            } else if (value instanceof String) {
                                valueList.add((String) value);
                            }
                            for (String entName : valueList) {
                                if (property != null && entName != null) {
                                    // Check if the entitlement exists in the ManagedAttribute catalog
                                    ManagedAttribute ent = ManagedAttributer.get(context, app, property, entName);
                                    if (ent == null) {
                                        // Write the missing entitlement to the CSV
                                        writer.append(bundle.getName()).append(",").append(entName).append("\n");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    System.out.println("CSV file created successfully on your desktop!");

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


return "CSV file created and saved to desktop!";

Hi @autorun6464,

Please add the logic which will export result to CSV file.

	if (!roleNames.isEmpty()) {
    try (BufferedWriter writer = new BufferedWriter(new FileWriter("missing_entitlements.csv"))) {
        writer.write("Bundle Name, Missing Entitlement\n"); // Header row
        writer.write(roleNames);
        writer.flush(); 
    } catch (IOException e) {
        e.printStackTrace();
       
    }
} else {
    System.out.println("No missing entitlements found.");
}
}

Regards,
Arun

1 Like

look in AIZ-TaskRule-RoleEntitlementMissing rule at line 21, you are parsing wrong a variable

1 Like

Try this code

QueryOptions qo = new QueryOptions();
Iterator it = context.search(Bundle.class, qo);

// CSV file name
String fileName ="exported_roles.csv";

try {
 FileWriter writer = new FileWriter(fileName);
    // Write CSV header
    writer.append("Role Name,Missing Entitlement\n");

    while (it.hasNext()) {
        Bundle bundle = it.next();
        List profiles = bundle.getProfiles();

        for (Profile profile : Util.safeIterable(profiles)) {
            List constraints = profile.getConstraints();

            for (Filter cons : Util.safeIterable(constraints)) {
                Application app = profile.getApplication();
                if (app != null) {
                    Schema accountSchema = app.getAccountSchema();

                    if (accountSchema != null) {
                        if (cons instanceof Filter.LeafFilter) {
                            String property = cons.getProperty();
                            Object value = ((Filter.LeafFilter) cons).getValue();

                            List valueList = new ArrayList();
                            // Check if the value is a List or String
                            if (value instanceof List) {
                                valueList.addAll((List) value);
                            } else if (value instanceof String) {
                                valueList.add((String) value);
                            }
                            for (String entName : valueList) {
                                if (property != null && entName != null) {
                                    // Check if the entitlement exists in the ManagedAttribute catalog
                                    ManagedAttribute ent = ManagedAttributer.get(context, app, property, entName);
                                    if (ent == null) {
                                        // Write the missing entitlement to the CSV
                                        writer.append(bundle.getName()).append(",").append(entName).append("\n");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    System.out.println("CSV file created successfully on your desktop!");

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


return "CSV file created and saved to desktop!";

still same error

An unexpected error occurred: java.lang.Exception: sailpoint.tools.GeneralException: BeanShell script error: bsh.ParseException: Parse error at line 75, column 3. Encountered: catch BSF info: AIZ-TaskRule-RoleEntitlementMissing at line: 0 column: columnN

Could you please share the entire code here?

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule created="1726144527865" id="0a641a1f91b819bc8191e63b3df95554" language="beanshell" modified="1726249246250" name="AIZ-TaskRule-RoleEntitlementMissing">
  <Description>Rule to check for roles that contain an entitlement which does not exist in the entitlement catalog</Description>
  <Signature returnType="Map">
    <Inputs>
      <Argument name="log">
        <Description>The log object associated with the SailPointContext.</Description>
      </Argument>
      <Argument name="context">
        <Description>A sailpoint.api.SailPointContext object that can be used to query the database if necessary.</Description>
      </Argument>
      <Argument name="taskResult">
        <Description>A sailpoint.object.TaskResult object that can be used to report task results to IIQ.</Description>
      </Argument>
    </Inputs>
    <Returns>
      <Argument name="roleNames">
        <Description>Role names that are missing entitlements</Description>
      </Argument>
    </Returns>
  </Signature>
  <Source>

import sailpoint.object.TaskResult;
import sailpoint.object.QueryOptions;
import sailpoint.object.Identity;
import sailpoint.tools.Message;
import java.util.List;
import java.util.Map;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import org.apache.log4j.Logger;
import sailpoint.object.*;
import java.util.*;
import sailpoint.object.Bundle; 
import sailpoint.api.ManagedAttributer;

QueryOptions qo = new QueryOptions();
Iterator it = context.search(Bundle.class, qo);

// CSV file name
String fileName ="exported_roles.csv";

try {
 FileWriter writer = new FileWriter(fileName);
    // Write CSV header
    writer.append("Role Name,Missing Entitlement\n");

    while (it.hasNext()) {
        Bundle bundle = it.next();
        List profiles = bundle.getProfiles();

        
    if (profiles != null) { // Safe null check for profiles
        for (Object objProfile : profiles) {
            Profile profile = (Profile) objProfile;
            List constraints = profile.getConstraints();

            if (constraints != null) { // Safe null check for constraints
                for (Object objCons : constraints) {
                    Filter cons = (Filter) objCons;
                    Application app = profile.getApplication();
                    
                    if (app != null) {
                        Schema accountSchema = app.getAccountSchema();

                        if (accountSchema != null &amp;&amp; cons instanceof Filter.LeafFilter) {
                            String property = cons.getProperty();
                            Object value = ((Filter.LeafFilter) cons).getValue();
                            
                            List valueList = new ArrayList();
                            // Check if the value is a List or String
                            if (value instanceof List) {
                                valueList.addAll((List) value);
                            } else if (value instanceof String) {
                                valueList.add((String) value);
                            }
                            for (String entName : valueList) {
                                if (property != null &amp;&amp; entName != null) {
                                    // Check if the entitlement exists in the ManagedAttribute catalog
                                    ManagedAttribute ent = ManagedAttributer.get(context, app, property, entName);
                                    if (ent == null) {
                                        // Write the missing entitlement to the CSV
                                        writer.append(bundle.getName()).append(",").append(entName).append("\n");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    System.out.println("CSV file created successfully on your desktop!");

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


return "CSV file created and saved to desktop!";


  
  </Source>
</Rule>

i manually iterate here instead of safeIterable() because it gave this error

An unexpected error occurred: java.lang.Exception: sailpoint.tools.GeneralException: BeanShell script error: bsh.EvalError: Sourced file: inline evaluation of: ``import sailpoint.object.TaskResult; import sailpoint.object.QueryOptions; import . . . ‘’ : Attempt to resolve method: safeIterable() on undefined variable or class name: Util : at Line: 22 : in file: inline evaluation of: import sailpoint.object.TaskResult; import sailpoint.object.QueryOptions; import . . . ‘’ : Util .safeIterable ( profiles ) BSF info: AIZ-TaskRule-RoleEntitlementMissing at line: 0 column: columnNo`

Please try after adding this statement.

import sailpoint.tools.Util;

Thanks

Add the below import statement to your code.

import sailpoint.tools.Util;

it says success but i dont see file in the Desktop. i even try to change it to C:\exported_roles.csv but i dont see file there even after success

Add this command before the System.out.println:

writer.close();

where you declare the path of file?

nope still not… the path is "\Desktop\exported_roles.csv…

String fileName = System.getProperty("user.home") + 
                 (System.getProperty("os.name").toLowerCase().contains("win") ? "\\Desktop\\exported_roles.csv" : "/Desktop/exported_roles.csv");

The user which exucutes sailpoint have the permission to write in this folder?

try to save in sailpoint folder, if it works is a permission problem.

also in your code a } is missing, the try is never closed

so in sailpoint folder u mean like this

String fileName =“\\cead.prd\data\Groups\MIA0\Groups1\OIM\SailPoint\Missing_Entitlements.csv”;

coz even this didnt work i didnt see the file there after the success of task result

depeds on your installation. Sailpoint home is the folder where you can find identityiq folder.

Also, in your rule you are declaring this:

String fileName ="exported_roles.csv";
and you use it into the costructor of FileWriter without specifying the path. Without the path the rule will create the file in own folder(this is java not sailpoint) and I dont know where.

try to put the entire path and close the FileWriter.

it in sailpoint home folder is in cloud … any way we can export that to our local… like giving C:\ direct isnot not working

It doesn’t work like that. I image you have installed SP in Azure, AWS or other. There you a VM with linux or windows. When you exucute the rule, you are exucuting on that machine, if put C:\folderblabla, the will save the on that cloud machine not in yours.

For save locally, you must connect the cloud to your pc. You can do that sharing folder betweet your pc and cloud machine, but that is little complex theme doesnt depends of SP.