Lazy Initialization Error

Hi All,

I’m encountering an issue while executing a workflow script that processes inactive identities. Specifically, I’m receiving a LazyInitializationException when attempting to access the workgroups collection of an Identity object. Below are the details of my implementation and the error message I’m facing.

Objective:

The goal of my script is to:

  1. Retrieve all inactive identities.
  2. For each inactive identity with an endDate matching today’s date:
  • Fetch associated applications, managed attributes, bundles, steward identities, and manager identities.
  • Process workgroups to identify those with exactly one member or more.
  • Launch a workflow (Owner Update WF Test) if relevant data is present.

Code i have tried:

  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.HashMap;
  import sailpoint.api.SailPointContext;
  import sailpoint.api.Workflower;
  import sailpoint.object.Workflow;
  import sailpoint.object.WorkflowLaunch;
  import org.apache.log4j.Logger;
  import org.apache.log4j.Level;
  import sailpoint.object.Identity;
  import sailpoint.object.Application;
  import sailpoint.object.ManagedAttribute;
  import sailpoint.object.Bundle;
  import sailpoint.api.ObjectUtil;
  import sailpoint.tools.GeneralException;
  import sailpoint.object.QueryOptions;
  import sailpoint.object.Filter;
  import java.util.List;
  import java.util.Iterator;
  import java.util.ArrayList;
  import java.util.Arrays;
  import org.hibernate.HibernateException;

  Logger logs = Logger.getLogger("com.rule.Test");
  logs.setLevel(Level.DEBUG);
  logs.debug("Test Success");

  try {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    String today = sdf.format(new Date());
    logs.debug("Today's Date: " + today);

    QueryOptions qp = new QueryOptions();
    qp.addFilter(Filter.eq("inactive", true));

    List identities = context.getObjects(Identity.class, qp);

    for (Identity iden : identities) {
      try {
        String endDate = (String) iden.getAttribute("endDate");

        if (endDate != null && endDate.equals(today)) {
          logs.debug("Processing identity with endDate matching today: " + iden.getName());

          try {
            QueryOptions appQueryOptions = new QueryOptions();
            appQueryOptions.addFilter(Filter.eq("owner", iden));
            List applicationsList = context.getObjects(Application.class, appQueryOptions);

            QueryOptions attrQueryOptions = new QueryOptions();
            attrQueryOptions.addFilter(Filter.eq("owner", iden));
            List managedAttributesList = context.getObjects(ManagedAttribute.class, attrQueryOptions);

            QueryOptions bundleQueryOptions = new QueryOptions();
            bundleQueryOptions.addFilter(Filter.eq("owner", iden));
            List bundlesList = context.getObjects(Bundle.class, bundleQueryOptions);

            String managerName = iden.getName();
            QueryOptions stewardQueryOptions = new QueryOptions();
            stewardQueryOptions.addFilter(Filter.eq("steward.name", managerName));
            stewardQueryOptions.addFilter(Filter.eq("type", "Service"));
            List stewardIdentities = context.getObjects(Identity.class, stewardQueryOptions);

            QueryOptions managerQueryOptions = new QueryOptions();
            managerQueryOptions.addFilter(Filter.eq("manager.name", managerName));
            managerQueryOptions.addFilter(Filter.eq("type", "Service"));
            List managerIdentities = context.getObjects(Identity.class, managerQueryOptions);

            List singleMemberWorkgroups = new ArrayList();
            List removeFromWorkgroups = new ArrayList();
            List workgroups = iden.getWorkgroups();

            if (workgroups != null && !workgroups.isEmpty()) {
              for (Identity workgroup : workgroups) {
                if (workgroup.isWorkgroup()) {
                  try{
                    Iterator wrkGrpmembers = ObjectUtil.getWorkgroupMembers(context, workgroup, null);
                    int memberCount = 0;

                    while (wrkGrpmembers.hasNext()) {
                      wrkGrpmembers.next();
                      memberCount++;
                    }

                    if (memberCount == 1) {
                      singleMemberWorkgroups.add(workgroup.getName());
                      logs.debug("Workgroup " + workgroup.getName() + " has exactly 1 member.");
                    }
                    else{
                      removeFromWorkgroups.add(workgroup.getName());
                      log.error("workgroup has more than 1 member");
                    }
                  }
                  catch(Exception e){
                    log.error("workgroup failed" + workgroup.getName());
                  }
                }
              }
            }

            boolean hasData = false;
            List listsToCheck = Arrays.asList(
              applicationsList, 
              managedAttributesList, 
              bundlesList, 
              stewardIdentities, 
              managerIdentities, 
              singleMemberWorkgroups
            );

            for (List list : listsToCheck) {
              if (list != null && !list.isEmpty()) {
                hasData = true;
                break;
              } else {
                logs.error("No data found.");
              }
            }

            if (hasData) {
              logs.debug("Launching workflow for identity: " + iden.getName());

              Identity WI_Owner = iden.getManager();
              Identity requester = context.getObjectByName(Identity.class, "spadmin");

              if (WI_Owner == null || requester == null) {
                logs.error("WI_Owner or requester is null for identity: " + iden.getName());
                continue;
              }

              String targetIdentity = iden.getName();
              String owner = WI_Owner.getName();
              String req = requester.getName();

              String targetIdentityEmail = iden.getEmail();
              String launcherEmail = WI_Owner.getEmail();
              String requesterEmail = requester.getEmail();

              HashMap launchArgsMap = new HashMap();
              launchArgsMap.put("requester", req);
              launchArgsMap.put("launcher", owner);
              launchArgsMap.put("targetIdentity", targetIdentity);
              launchArgsMap.put("requesterEmail", requesterEmail);
              launchArgsMap.put("launcherEmail", launcherEmail);
              launchArgsMap.put("targetIdentityEmail", targetIdentityEmail);

              Workflow wf = (Workflow) context.getObjectByName(Workflow.class, "Owner Update WF Test");

              if (wf == null) {
                logs.error("Workflow 'Owner Update WF Test' not found.");
                continue;
              }

              WorkflowLaunch wflaunch = new WorkflowLaunch();
              wflaunch.setWorkflowName(wf.getName());
              wflaunch.setWorkflowRef(wf.getName());
              wflaunch.setCaseName("Owner Update WF Test");
              wflaunch.setVariables(launchArgsMap);

              Workflower workflower = new Workflower(context);
              WorkflowLaunch launch = workflower.launch(wflaunch);

              logs.debug("Launched workflow for targetIdentity: " + targetIdentity + " with new owner: " + owner);
            } else {
              logs.debug("Skipping workflow launch for identity: " + iden.getName());
            }
          } catch (GeneralException e) {
            logs.error("Error during workflow launch process: " + e.getMessage(), e);
          }
        }
      } catch (GeneralException e) {
        logs.error("Error during processing for identity: " + iden.getName() + " - " + e.getMessage(), e);
      } catch (HibernateException e) {
        logs.error("Error fetching workgroups for identity: " + iden.getName() + " - " + e.getMessage(), e);
      } catch (Exception e) {
        logs.error("Unexpected error for identity: " + iden.getName() + " - " + e.getMessage(), e);
      }
    }
  } catch (GeneralException e) {
    logs.error("Error fetching inactive identities: " + e.getMessage(), e);
  }

Error Log:

lazy error.log (13.5 KB)

@shruthi_m there are multiple reason for Lazy error. you have to avoid looping on objects. better to get Ids or name then loop it and get objects inside loop. but before you can try to make a small change like below

singleMemberWorkgroups.add(workgroup); // store identity Vs name.

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