How to show task results in UI

Which IIQ version are you inquiring about?

[8.4]

Please share any images or screenshots, if relevant.

*[

]*

Please share any other relevant files that may be required (for example, logs).

*[


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule created="1735252928732" id="0a00004694041fcb8194052268dc09c5" language="beanshell" modified="1735327372793" name="Test-AuditEventPruner-FromTask" significantModified="1735327372793">
  <Description>An IdentityIQ server-side rule that is executed before the connector's provisioning method is called. This gives the customer the ability to customize or react to anything in the ProvisioningPlan BEFORE it is sent to the underlying connectors used in provisioning. 

    This rule will be called for any application found in a plan that also has a configured 'beforeProvisioningRule' configured.  

    The plan can be updated directly in the rule by reference and does not need to return the plan.</Description>
  <Signature>
    <Inputs>
      <Argument name="config">
        <Description>
          The application object that references this before/after script.
        </Description>
      </Argument>
      <Argument name="log">
      <Description>The log object to use for logging.</Description>
    </Argument>
    <Argument name="context">
      <Description>A sailpoint.api.SailPointContext object that can be used to query and update objects.</Description>
    </Argument>
    <Argument name="auditEventMaxAge" type="int">
      <Description>The maximum age in days for audit events before they are pruned.</Description>
    </Argument>
    </Inputs>
  </Signature>
  <Source>


  import java.util.Calendar;
  import java.util.Date;
  import sailpoint.object.QueryOptions;
  import sailpoint.object.Filter;
  import sailpoint.object.AuditEvent;
  import sailpoint.api.Terminator;
  import sailpoint.api.TaskManager;
  import sailpoint.object.TaskResult;
  import org.apache.log4j.Logger;
  import java.util.HashMap;
  import java.util.Map;

  Logger log = Logger.getLogger("com.local.TestAuditEventPrunerFromTask");

  try {

    // Create a terminator instance to delete old audit events
    Terminator terminator = new Terminator(context);
    int auditEventMaxAge = Integer.parseInt(config.get("auditEventMaxAge"));
    log.debug("TestAuditEventPrunerFromTask logs auditEventMaxAge is : " + auditEventMaxAge);

    // Calculate the threshold date for audit events
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.SECOND, 0);  
    calendar.add(Calendar.DAY_OF_MONTH, -auditEventMaxAge);
    Date date = calendar.getTime();

    // Create query options to filter audit events older than the calculated date
    QueryOptions qo = new QueryOptions();
    qo.addFilter(Filter.lt("created", date));

    Iterator auditEventsToDelete = terminator.getIds(AuditEvent.class, qo);
    int deletedCount = 0;
    while(auditEventsToDelete.hasNext()){
      String auditEventId = auditEventsToDelete.next();
      log.debug("AuditEvent being deleted: " + auditEventId);
      AuditEvent auditEvent = context.getObject(AuditEvent.class, auditEventId);
      terminator.deleteObject(auditEvent);
      deletedCount++;
    }

    // Execute task to prune audit logs
    TaskManager tm = new TaskManager(context);
    Map map = new HashMap();
    map.put("auditEventMaxAge", config.get("auditEventMaxAge"));

    TaskResult result = tm.runSync("Test-PruneAuditlogs-RunRuleTask", map); 

    if (result != null) {
      result.setCompleted(System.currentTimeMillis());
      result.setCompletionStatus(TaskResult.CompletionStatus.Completed);
      result.setHost("admin_dell");
      result.setName("AuditEventPruner");

      // Log completion status
      result.addMessage("Deleted " + deletedCount + " old audit events.");

      // Set custom attributes for the task result
      Map taskAttributes = new HashMap();
      taskAttributes.put("auditEventsDeleted", deletedCount); // Assuming 'deletedCount' is an int or long
      taskAttributes.put("totalDeleted", deletedCount); // Same for total deleted events
      taskAttributes.put("taskStatus", "Completed");

      // Set the attributes to the result
      result.setAttributes(taskAttributes);

      // Log the completion status
      String completionStatus = result.getMessages() != null ? result.getMessages().toString() : "No messages";
      log.debug("Task completed with status: " + completionStatus);
      context.saveObject(result);
``````context.commitTransaction();
    } else {
      log.warn("Task did not return a result.");
    }

  } catch (Exception e) {
    log.error("Exception in task execution or audit pruning: " + e.getMessage(), e);
  }


  </Source>
</Rule>



Task:


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE TaskDefinition PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<TaskDefinition created="1735252118036" id="0a00004694041fcb819405160a1408f6" modified="1735327399614" name="Test-PruneAuditlogs-RunRuleTask" resultAction="Delete" significantModified="1735327399614" subType="task_item_type_generic" type="Generic">
  <Attributes>
    <Map>
      <entry key="TaskDefinition.runLengthAverage" value="24"/>
      <entry key="TaskDefinition.runLengthTotal" value="121"/>
      <entry key="TaskDefinition.runs" value="5"/>
      <entry key="TaskSchedule.host"/>
      <entry key="ruleConfig" value="auditEventMaxAge,55"/>
      <entry key="ruleName" value="Test-AuditEventPruner-FromTask"/>
      <entry key="taskCompletionEmailNotify" value="Disabled"/>
      <entry key="taskCompletionEmailRecipients"/>
      <entry key="taskCompletionEmailTemplate"/>
    </Map>
  </Attributes>
  <Description>A task that can be used to run an arbitrary rule.</Description>
  <Owner>
    <Reference class="sailpoint.object.Identity" id="c0a801be8e161c14818e162d805000ff" name="spadmin"/>
  </Owner>
  <Parent>
    <Reference class="sailpoint.object.TaskDefinition" id="c0a801be8e161c14818e162dabef01e1" name="Run Rule"/>
  </Parent>
</TaskDefinition>


]*

Share all details about your problem, including any error messages you may have received.

*[
How can I verify that the task is running successfully and generating the expected output?"

I am unable to see the results in the UI after running the task.

What steps should I follow to ensure the results display correctly in the UI after executing the task?

]*

Hi @Consultant,

question: dont you see the task result or you want see the result of the rule into the task resultt?

Yes! I do not see the task result in UI after running the task.
I am trying to see the count of audit Events Deleted.

 taskAttributes.put("auditEventsDeleted", deletedCount); // Assuming 'deletedCount' is an int or long
      taskAttributes.put("totalDeleted", deletedCount); // Same for total deleted events
      taskAttributes.put("taskStatus", "Completed");

ok, so the second.
If you want add the result you must use an Attribute object like this:

Attributes attributes=new Attributes();
attributes.put("obj created",String.valueOf(objCreated));
attributes.put("obj deleted",String.valueOf(objUpdated));
taskResult.setAttributes(attributes);

and into the taskDefinition add Signature

<Signature>
    <Returns>
      <Argument name="objCreated" type="string">
        <Prompt>Objects Created</Prompt>
      </Argument>
      <Argument name="objUpdated" type="string">
        <Prompt>Objects Updated</Prompt>
      </Argument>
    </Returns>
  </Signature>
2 Likes

Hi @Consultant

Also, you can add status message in your task result. For example you can show several levels as INFO, WARN or ERROR.

In your case, your are invoking to method addMessage(String), but to show info level you need to invoke method addMessage(Message)

taskResult.addMessage(new Message(Message.Type.Info, "Deleted " + deletedCount + " old audit events.", null ));

From other side, you DON’T need instance result object in code, since taskResult is a global variable in your rule

2 Likes

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