Java Code Data Policy Examples/Update Opportunity with Data Calculated from a Related Object Code Sample

Java Code Data Policy Examples/Update Opportunity with Data Calculated from a Related Object Code Sample

From LongJump Support Wiki

Java Code Data Policy Examples/Update Opportunity with Data Calculated from a Related Object Code Sample
Jump to: navigation, search

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: Update Opportunity with Data Calculated from a Related Object Code Sample.

import com.platform.api.*;

public class DataPolicyExamples
{
   /**
    * This method is called when an Expense Item is added or updated.
    * It updates the Opportunity with a new total for billable expenses.
    */

   public void updateOpportunity(Parameters requestParams)
   throws Exception
   {
     // Search Expense_Items for billable expenses related to the Opportunity
     String opportunity_id = requestParams.get("opportunity_lookup");
     Result result = Functions.searchRecords(
         "Expense_Items",
         "record_id,amount",
         "( (opportunity_lookup equals '" + opportunity_id + "')"
          + " and billable equals 'true') )");
       
     //getCode() returns the number of records found in the search
     int resultCode = result.getCode();
     if(resultCode < 0)
     {
       // Error scenario.
       String msg = "Expense Report: Error searching for expenses";
       Functions.debug(msg + ":\n" + result.getMessage());  // Log details
       Functions.throwError(msg + ".");                     // Error dialog
     }
     else if(resultCode == 0)
     {
       // No records found. (Not possible when the data policy
       // is triggered by adding or updating an expense record.)
       Functions.debug("Expense Report:Unexpected policy triggering action");
     }
     else
     {
       // Records retrieved successfully.
       Functions.debug(
           "Expense Report: " + resultCode + " billable records found");

       // Loop on the list of records in the search results
       // For each record, add the amount of the billable expense.
       Float totalExpenses = 0.0f;
       ParametersIterator iterator = result.getIterator();
       while(iterator.hasNext())
       {
           Parameters params = iterator.next();  // next record
           totalExpenses += params.getFloat("amount",0.0f);
       }
       Functions.debug("Expense Report: Total = " + totalExpenses);

       // Update the opportunity record with the total expenses calculated
       // from all the related expenses items records. No further action is
       // needed, so turn off any Opportunity data policies that might fire.
       Parameters updateParams = Functions.getParametersInstance();
       updateParams.add("total_expenses", totalExpenses);
       updateParams.add(PLATFORM.PARAMS.RECORD.DO_NOT_EXEC_DATA_POLICY, "1");      
       Result updateResult = Functions.updateRecord(
           "OPPORTUNITY_V2", opportunity_id, updateParams
       );
       Functions.debug("Opportunity update:" + updateResult.getMessage());
     }
   } // end method
 }// end class
Personal tools