Java Code Data Policy Examples/Add Task On Field Changes Sample Code

From LongJump Support Wiki

This code is intended for use as part of a Data Policy. It is a complex example of the use of Java code to take a series of actions, each based on the previous code section. For information on the use of this code sample, see: Add Task On Field Changes.


Sample 1: Simple Task Addition

import com.platform.api.*;


public class DataPolicyExamples {

  public void Functions.addTask(Parameters requestParams) {
    String desc = requestParams.get("description");
    Parameters params= Functions.getParametersInstance();
    params.add("reference_id", requestParams.get("record_id"));
    params.add("reference_type", requestParams.get("object_id"));
    Result result = Functions.addTask("Mail invoice!" + desc, new Date(),
                            Functions.getEnv(ENV.USER.ID), params);
    if(result.getCode() != -1)
    {
      // success
      Functions.debug("Task has been added to mail an invoice..\n"
                     + result.getMessage());
    }
    else
    {
      Functions.debug("Error in adding a task." + result.getMessage());
    }
  } // end method
}// end class

Sample 2: Task Addition with Reminder, Repeat, End flag and Invitee List

  1. Modify the following sample to retrieve record IDs for contact and invitee list
  2. See the comments in the sample code below to make further modifications
import com.platform.api.*;


public class DataPolicyExamples {

  public void addTaskWithOptions(Parameters requestParams) {

    String taskRecordID = null;
    Parameters addTaskParams = Functions.getParametersInstance();
    addTaskParams.add("action_type", "Email");
    addTaskParams.add("calendar_frequency", "1");
    addTaskParams.add("assigned_id", Functions.getEnv(ENV.USER.ID));
    addTaskParams.add("calendar_type", "5");
    addTaskParams.add("created_id", Functions.getEnv(ENV.USER.ID));

    //To associate the Task to a specific Contact then search for the contact
    //Get the record ID of the contact and add the key-value pair:
    //    contact_id : record_id
    //For example, to associate with a contact with record ID: 1353634624
    //Add the following line to the 'addTaskParameters' object
    addTaskParams.add("contact_id", "1353634624");
    
    addTaskParams.add("date_created", new Date());
    addTaskParams.add("description", 
      "Expense reports submitted needs to approved and sent to Payment Records!");
    addTaskParams.add("notify_complete", "In Progress");
    addTaskParams.add("percentage_complete", "10");
    addTaskParams.add("priority", "High");
    addTaskParams.add("reference_id", requestParams.get("id") );
    addTaskParams.add("reference_type", requestParams.get("object_id"));
    addTaskParams.add("status", "In Progress");
    addTaskParams.add("subject", "Verify Payment Records!! - " + new Date());
    addTaskParams.add("repeat_flag", "true");
    addTaskParams.add("reminder_duration", "24");

    //Add an invitee list to the task
    //Search for users in the account object and populate this key-value pair
    addTaskParams.add("invitee_list", "109d9835638846219ce5a297d559da03");
    addTaskParams.add("modified_id", Functions.getEnv(ENV.USER.ID));

    //Set set end_flag in the key-value params in order to set end_date
    addTaskParams.add("end_flag", "true");
    Date endDate = new Date();
    endDate.setDate(28);
    endDate.setMonth(7);
    addTaskParams.add("end_date", endDate);
    Result addTaskResult = Functions.addTask("Payment Records verification - " 
                         + requestParams.get("record_id"), new Date(),
                             Functions.getEnv(ENV.USER.ID), addTaskParams);
    Functions.debug("Result of addTask" + addTaskResult .getMessage());

    //Retrieve the task ID and use it to add more logic based on your requirements
    taskRecordID = addTaskResult.getID();
  } // end method
}// end class