Data Policy Code Samples
From LongJump Support Wiki
These code samples can be used to construct your own Data Policies. The comments embedded in the code samples explain their functionality.
Log Activity
This example logs an activity for a given object identifier and activity identifier.
Functions.debug("Function: logActivity"); Parameters params = Functions.getParametersInstance(); params.add("subject", "Subject : Adding log call via function"); params.add("description", " This is a log call added via function"); params.add("status", "In Progress"); params.add("sales_notes", "Confirmed the project amount with the contact"); params.add("action_type", "Email"); params.add("contact_id", "2015353331"); //If you had a custom field in your Task object //Go to the link Objects » Tasks to get the field name //The display name of the custom field should not be used to update the field //Get the name of custom field to update the field params.add("custom11", "custom field update using function"); params.add("activity_type", "Fax"); params.add("notes", "Functions - logactivity"); Result result = Functions.logActivity("Adding an activity using logActivity via Custom Code", requestParams.get("object_id"), requestParams.get("id"),getEnv(ENV.USER.ID), params); Functions.debug("Result from logActivity:" + result.getMessage()); return result.getMessage();
Close Tasks with Status Equal to In Progress
This example finds all the tasks whose status field contains "In Progress" for a given record identifier and changes the status to "Completed".
//This function closes tasks related to any record in any object Functions.debug("Object :" + requestParams.get("object_id")+ "\n Function : Search And Update Tasks for :"+ requestParams.get("record_id")); String recId = requestParams.get("record_id"); //Search for all tasks that are associated with the CASE object //The search filter is hard coded here to look for case //But you can pass the search string dynamically to your Parameters //object when you call this function Result result = Functions.searchRecords("TASK", "record_id,subject,status", "reference_type contains 'CASE'"); int resultCode = result.getCode(); Functions.debug(requestParams.get("object_id")+":Result message for searching TASKS is " + result.getMessage()); Functions.debug(requestParams.get("object_id")+" :Result code for searching TASKS is " + resultCode); if(resultCode < 0) { // Some error happened. String msg = "Related Tasks could not be retrieved"; Functions.debug(msg + ":\n" + result.getMessage()); // Log details Functions.throwError(msg + "."); // Error dialog } else if(resultCode == 0) { // No records found. Take action according to your business logic Functions.debug(requestParams.get("object_id")+" : No TASKS found using the search function."); } else { //Records retrieved successfully Functions.debug("Number of tasks found using search function:" + resultCode); //Iterate through the set of records found ParametersIterator iterator = result.getIterator(); //Work with all the records retrieved using searchRecords API in a while loop while(iterator.hasNext()) { // Take action according to your business logic //Get the records using the Iterator object in to Parameter object Parameters params = iterator.next(); String taskID = params.get("record_id"); Functions.debug(" TASK: TASKID is " + taskID); String subjectInfo = params.get("subject"); Functions.debug(" Task Subject is: " + subjectInfo); String taskStatus = params.get("status"); Functions.debug(" Task Status is " + taskStatus); //We need to check for the status of the task //If the task is 'In Progress', we will update it if(taskStatus.equals("In Progress")) { Result getRecordResult = Functions.getRecord("TASK", "subject,status,reference_id,reference_type", taskID); Functions.debug("Result from getRecord:\n" + getRecordResult.getMessage()); Functions.debug("Reference Id from getRecord: " + (getRecordResult.getParameters()).get("reference_id")); params.add("status", "Completed"); params.add("end_flag", 1); params.add("ownerID", Functions.getEnv(ENV.USER.ID)); params.add("assigned_id", Functions.getEnv(ENV.USER.ID)); params.add("description", "Check Activity history for Completed tasks. "); params.add("subject", "Completing Tasks on Case Close!"); params.add("date_modified", new Date()); params.add("action_type", "Email"); //Check to see if the reference ID of the task //Update the task only if the reference Id matches the current record ID if(((getRecordResult.getParameters()).get("reference_id")).equals(recId)) { Functions.debug("Related Task found!"); Result updateTaskResult = Functions.updateRecord("TASK", taskID, params); Functions.debug("Update Task Result:" + updateTaskResult.getMessage()); } } } }
Add and Update an Event
This example adds an event for a given object identifier.
Functions.debug("Function : Add & Update Event"); Parameters params = Functions.getParametersInstance(); params.add("reference_id", requestParams.get("record_id")); params.add("reference_type", requestParams.get("object_id")); params.add("description", "Check your calendar to make sure the event is recorded."); params.add("start_date", new Date()); params.add("subject", "Calender - adding an appointment"); // the following line is optional; // it demonstrates how you can use the getEnv function to retrieve the USER values params.add("assigned_to", Functions.getEnv(ENV.USER.ID)); Result result = Functions.addEvent("Meeting! " + requestParams.get("object_id"), Functions.getEnv(ENV.USER.ID), new Date(), 11, 15, 30, params); Functions.debug("Result of addEvent: " + result.getMessage()); if(result.getCode() != -1) { // Some code to populate the eventID. //eventId is required to use updateEvent String eventID = result.getID(); Functions.debug("Event ID for event added using addEvent:" + eventID); // code to add description to the Event. params.add("subject", "Weekly status meeting!!"); Result result2 = Functions.updateEvent(eventID, params); Functions.debug("Result from updateEvent:" + result2.getMessage()); } else { Functions.debug("Error in adding event" + result.getID()); }
Add an Opportunity
This example adds an opportunity record.
//An opportunity can be added to an account or to a contact or prospect Parameters params = Functions.getParametersInstance(); params.add("name", "Opportunity-" + new Date()); params.add("close_date", new Date()); params.add("stage","Prospecting"); //Associating the opportunity with a contact //Make sure you modify the record ID in the next line //of code to match your CONTACT's record ID params.add("contact_id", "1072726531"); //The following lines of code show how to update lookup fields //Lookup fields can be updated //during an add operation but the key-value pair should be the //lookup field name and the record ID of the record //'lookuptotestcustomizations' is the record identifier of a user-defined lookup field //'account_id' is the record identifier of a system-defined lookup field params.add("lookuptotestcustomizations", "1637926708"); params.add("account_id", "2101896782"); //Relate the opportunity to the ACCOUNT object using the function params.add("related_to_type", "ACCOUNT"); //The record ID of the ACCOUNT object is being hard coded here params.add("related_to_id", "2101896782"); if(!params.equals("")) { Result addRecResult = Functions.addRecord("OPPORTUNITY_V2", params); Functions.debug("Opportunity Add Record:" + addRecResult); if(addRecResult.getCode() != 0) { String msg = "Error adding Opportunity"; Functions.debug(msg + ":\n" + addRecResult.getMessage()); // Log details Functions.throwError(msg + "."); // Error dialog } else { Functions.debug("Success on adding an Opportunity"); requestParams.add("opportunity_id",addRecResult.getID()); } } else { Functions.debug("Parameters object is null."); }
Send an Email Message with a Template
This example shows how to use the sendEmailUsingTemplate Java API call to send an email message with a template.
String attachmentIdList = ""; Functions.debug("Within contract object"); // To associate the sendEmail activity to a particular record //Retrieve the record ID from the requestParams object String recordID = requestParams.get("id"); //Retrieve the object ID from the funtionParams object String objectID = requestParams.get("object_id"); //Log the record ID to the log Functions.debug("Record ID of record from current object : " + recordID); //Make the API call by passing the appropriate parameters //For parameter explanations, check the JAVA API Result result = Functions.sendEmailUsingTemplate(objectID, recordID, "mia@financiocorp.com", "mia@financiocorp.com", "Testing sendEmail Using Template", "1869974057twn1678149854", "", ""); Functions.debug("Post sendEmail API call from current object: " + objectID); if(result.getCode() != 0) Functions.debug("Error in sending email!" + result.getMessage()); else Functions.debug("Success on sendEmail, check inbox : " + result.getMessage());
Add Note and Relate it to a Record
This function adds a note to a record in an object.
Parameters noteParams = Functions.getParametersInstance(); noteParams.add("description", "Very important to keep track of what is going on using Notes."); noteParams.add("title", "Add Notes!"); noteParams.add("reference_id", requestParams.get("record_id")); noteParams.add("reference_type", requestParams.get("object_id"); //Currently the sample is relating the note to an object which has 'name' as an identifier field, for example, the 'name' field in the Account object noteParams.add("reference_name", requestParams.get("name"); Result addNote = Functions.addRecord("NOTE", noteParams); Functions.debug("Result of adding a NOTE:" + addNote.getMessage()); Functions.debug("Code from adding a note:" + addNote.getCode());