CSV file email Attachment error

so i am getting parse error and email is not being sent… any solutions to it?

<![CDATA[
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 org.apache.log4j.Logger;
import java.util.ArrayList;
import java.util.*;
import sailpoint.object.*;
import sailpoint.object.Bundle; 
import sailpoint.api.ManagedAttributer;
import sailpoint.tools.Util;
import org.apache.commons.io.FileUtils;
import sailpoint.object.EmailFileAttachment;

public class ExportMissingEntitlements {
    public String execute(Map<String, Object> context) {
        // CSV file name
        String fileName = "exported_roles.csv";

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

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

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

                for (Profile profile : Util.safeIterable(profiles)) {
                    List<Filter> 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<String> 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");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
 writer.close();

            System.out.println("CSV file created successfully !");
            
                       // Create a new EmailFileAttachment
            EmailFileAttachment attachment = new EmailFileAttachment();
            attachment.setFileName(fileName);
            attachment.setMimeType("text/csv");
            attachment.setFilePath(fileName);

            // Send email with CSV attachment
            Message msg = new Message();
            msg.setTo("[email protected]");
            msg.setSubject("Missing Entitlements Report");
            msg.setBody("Please find attached the report of missing entitlements.");

 

            // Add the attachment to the message
            msg.addAttachment(attachment);

            // Send the email
            context.sendEmailNotification(msg);

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

        return "CSV file has been exported!";
    }
}
]]>

Hi @autorun6464,

can you share the error too?

Hi @autorun6464 ,

Parse error means there must be some syntax error. Could you please share the error message or provide some logs at which place it is throwing, etc., something like this?

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

i have posted it above …parse error …i guess may be some import missing idk

Can you try by removing <String, Object> in the Map at line 18 ?

    public String execute(Map context) {

And also use other object reference instead of context. if you are using it in the rule, then it might issue, because the rule itself is having context object reference by default.

2 Likes

nope too many error i will just add my raw code can you email my generated csv to an email… i am getting too much error my way

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 org.apache.log4j.Logger;
import java.util.ArrayList;
import java.util.*;
import sailpoint.object.*;
import sailpoint.object.Bundle; 
import sailpoint.api.ManagedAttributer;
import sailpoint.tools.Util;

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 &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!");
    writer.close();

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

return "CSV file has been export!";

thiis is my code without csv… maybe i am generating csv wrong way… actually i needed that csv to an email coz i couldnt specify path to export because we are doing it in cloud and we dont knw where is it saving

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 org.apache.log4j.Logger;
import java.util.*;
import sailpoint.object.*;
import sailpoint.object.Bundle; 
import sailpoint.api.ManagedAttributer;
import sailpoint.tools.Util;

        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 &amp;&amp; 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);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

    TaskResult taskResult = context.getObjectByName(TaskResult.class, "AIZ-Missingentitlementreport");
    if (taskResult != null) {
        taskResult.setAttribute("roleNames", "Roles with missing entitlements:\n" + roleNames);
        taskResult.setCompletionStatus(TaskResult.CompletionStatus.Success);
        context.saveObject(taskResult);
    }

    // Return the list of role names
    return roleNames;
  

Hi @autorun6464,

This code will send an email without writing to file.
Replace the To address and templateName in this code and try.


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 org.apache.log4j.Logger;
import java.util.*;
import sailpoint.object.*;
import sailpoint.object.Bundle; 
import sailpoint.api.ManagedAttributer;
import sailpoint.tools.Util;

        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 &amp;&amp; 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);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
    
	  String toAddresses="[email protected]";      //change the To address
      EmailOptions options = new EmailOptions(toAddresses, null);      
      byte[] postData = roleNames.getBytes();
      EmailFileAttachment fileAttachment=new EmailFileAttachment("Test.csv", EmailFileAttachment.MimeType.MIME_CSV,postData);
      options.addAttachment(fileAttachment);   
      EmailTemplate et = context.getObjectByName(EmailTemplate.class, "templateName");    //Replace with actual TemplateName
      context.sendEmailNotification(et, options);

    TaskResult taskResult = context.getObjectByName(TaskResult.class, "AIZ-Missingentitlementreport");
    if (taskResult != null) {
        taskResult.setAttribute("roleNames", "Roles with missing entitlements:\n" + roleNames);
        taskResult.setCompletionStatus(TaskResult.CompletionStatus.Success);
        context.saveObject(taskResult);
    }

   
    return "Task completed successfully";
1 Like

so i am getting email with fille attached but one issue is its sending email to all sailpoint admins…shouldnt it be only sending to the address in the script?

  <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 org.apache.log4j.Logger;
import java.util.*;
import sailpoint.object.*;
import sailpoint.object.Bundle; 
import sailpoint.api.ManagedAttributer;
import sailpoint.tools.Util;
  import sailpoint.object.EmailFileAttachment;
  import sailpoint.object.EmailOptions;
  import sailpoint.object.EmailTemplate;
  import sailpoint.tools.EmailException;


        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 &amp;&amp; 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);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
    
	  String toAddresses="[email protected]";      //change the To address
      EmailOptions options = new EmailOptions(toAddresses, null);      
      byte[] postData = roleNames.getBytes();
      EmailFileAttachment fileAttachment=new EmailFileAttachment("Test.csv", EmailFileAttachment.MimeType.MIME_CSV,postData);
      options.addAttachment(fileAttachment);   

// Construct an email template on the fly
EmailTemplate customTemplate = new EmailTemplate();
customTemplate.setName("Dynamic Missing Entitlements Report");  // Give a name
customTemplate.setSubject("Missing Entitlements Report");       // Set subject
customTemplate.setBody("Dear Recipient,\n\nPlease find attached the missing entitlements report for roles.\n\n" +
                       "Roles with missing entitlements:\n" + roleNames +
                       "\n\nBest regards,\nYour Team");

// Send email with the custom template
context.sendEmailNotification(customTemplate, options);

// Send email
context.sendEmailNotification(options);
    TaskResult taskResult = context.getObjectByName(TaskResult.class, "AIZ-Missingentitlementreport");
    if (taskResult != null) {
        taskResult.setAttribute("roleNames", "Roles with missing entitlements:\n" + roleNames);
        taskResult.setCompletionStatus(TaskResult.CompletionStatus.Success);
        context.saveObject(taskResult);
    }

   
    return "Task completed successfully";
  </Source>
</Rule>

Check the email settings in IdentityIQ configuration.
If the email notification type is set to redirection, it will send an email to the addresses specified in the redirection email address.

1 Like

thanxx yes it was the issue now it worked…

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