Java Code Data Policy Examples/Search for an Account and Update the Primary Contact for that Account Code Sample

Java Code Data Policy Examples/Search for an Account and Update the Primary Contact for that Account Code Sample

From LongJump Support Wiki

Java Code Data Policy Examples/Search for an Account and Update the Primary Contact for that Account 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: Search for an Account and Update the Primary Contact for that Account.

import com.platform.api.*;


public class DataPolicyExamples {

  public void updatePrimaryContact(Parameters requestParams) {

    Functions.debug(
      "Object : Account \n Script : Search For Account And Update Contact");
    Parameters params = Functions.getParametersInstance();
    //Modify the filter string to search based on your Account names
    Result result = Functions.searchRecords("ACCOUNT",
      "record_id,name,number,primary_contact_id",
      "name contains 'Challenge'");
    int resultCode = result.getCode();
    Functions.debug(
      " Account:Result message for searchRecord is " + result.getMessage());
    Functions.debug(
      " Account:Result object's getCode() returns the number of records"
     +" from the search: " + resultCode);

    if(resultCode < 0)
    {
      // Some error happened.
      String msg = "Search failed";
      Functions.debug(msg + ":\n" + result.getMessage());  // Log details
      Functions.throwError(msg + ".");                     // Error dialog
    }
    else if(resultCode == 0)
    {
      // No records found.
      Functions.debug(
        "Account:No records found using the search function in ACCOUNT.");
    }
    else
    {
      //Records retrieved successfully
      Functions.debug(
        "Account:Number of records found using search function in Account:"
       + resultCode);
      ParametersIterator iterator = result.getIterator();
     
      while(iterator.hasNext())
      {
        //Get the records using the Iterator object in to Parameter object
        Parameters params = iterator.next();
        String accountID = params.get("record_id");
        Functions.debug(" Account:Result message for accountID is " + accountID);
        String accountName = params.get("name");
        Functions.debug(" Result message for account Name is " + accountName);
        String number = params.get("number");
        Functions.debug(" Result message for acount number is " + number);

        //Get the Primary Contact Id's record id from the params object
        String contactRecordId = params.get("primary_contact_id");
        Functions.debug("Account:Primary contact record id: " + contactRecordId);

        //Add information to parameter object to update the Primary Contact record
        params.add("phone", "566-555-1212");
        params.add("description",
          "Updating Primary Contact of an Account after successful search.");
     
        Result contactResult = Functions.updateRecord("CONTACT", contactRecordId, params);
        if(contactResult.getCode() == 0)
        {
           Functions.debug(
             "Account:Contact of the Account record updated successfully\n"
             + contactResult.getMessage());
        }
     
        else
        {
           String msg = "Account record Contact could not be updated";
           Functions.debug(msg + ":\n" + contactResult.getMessage());  // Log details
           Functions.throwError(msg + ".");                     // Error dialog
       }
       requestParams.add("primary_contact_id", contactResult.getID());
     }
    }
  } // end method
}// end class
Personal tools