Thursday, June 29, 2017

Trigger and it's handler class structure for your organisation.

This is a sample structure to make your organization's triggers and their's handler class in a maintainable way. Check the coding example in next post.


Scheduler class example for batch apex.

Assume that our ORG has a custom object API named as "Product_Complaint__c".
Our aim is to schedule a batch apex which will responsible for make a select query on Product_Complaint__c object.

/**
 * This is a batch class.
 * https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
 **/
global class BatchClassExample implements Database.Batchable<sObject>{
  global String query;

  global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope){
       List<Product_Complaint__c> lstOfPC = (List<Product_Complaint__c>)scope;

       // Call any helper calss to do the logic.
       System.debug('Test PC Size ::::::::::::: ');
       System.debug('PC Size ::::::::::::: '+ lstOfPC.size());
   }


    global void finish(Database.BatchableContext BC){
    }
}


/**
 * This is a scheduler class example for batch apex.
 * This class implements the Schedulable interface for a class called AffectedScheculeClassExample.
 * 
 * Link: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
 **/
global class ScheduledBatchClassExample implements Schedulable {
  //This is implemented method and it must be declared as global or public.
  global void execute(SchedulableContext sc) {
    BatchClassExample batchInst = new BatchClassExample();
    batchInst.query = 'SELECT Id FROM Product_Complaint__c';
    database.executebatch(batchInst, 1);
  }
}


/**
 * This class is responsible for calling ScheduledBatchClassExample class to run the scheduler.
 * Call this class constractor from anonymous window.
 *
 **/
public class CallScheduledBatchClassExample
{
  //Constructor need to be call from annonymous window to run the schedule job.
  public CallScheduledBatchClassExample(){
    ScheduledBatchClassExample schBatchClsExmpl = new ScheduledBatchClassExample();
    /**
      *  Schedule structure:
      *  <Seconds> <Minutes> <Hours> <Day_of_month> <Month> <Day_of_week> <Optional_year>
      *
      * For below example it will schedulw the job @ 08:23 AM every day.
    */
    String sch = '0 23 8 * * ?';
    String schJobID = system.schedule('Scheduler Batch Example', sch, schBatchClsExmpl);
  }
}

To test this example instantiate CallScheduledBatchClassExample() 
from anonymous window as,
CallScheduledBatchClassExample inst = new CallScheduledBatchClassExample
();

Apex Scheduler Example.

Check the class descriptions .....


/**
 * This is a simple class called from scheduler class.
 *
 **/
public class AffectedScheculeClassExample {
  public AffectedScheculeClassExample() {
    System.debug('Calling from scheduler class for testing ...... ');
  }
}


/**
 * This is a scheduler class example.
 * This class implements the Schedulable interface for a class called AffectedScheculeClassExample.
 * 
 * Link: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
 **/
global class ScheduledClassExample implements Schedulable {
  //This is implemented method and it must be declared as global or public.
  global void execute(SchedulableContext sc) {
    AffectedScheculeClassExample affectedSchClassExmpl = new AffectedScheculeClassExample();
  }
}


/**
 * This class is responsible for calling scheduler class named as ScheduledClassExample to run at schedule time.
 * Call this class constractor from anonymous window.
 **/
public class CallScheduledClassExample {
  //Constructor need to be call from annonymous window to run the schedule job.
  public CallScheduledClassExample(){
    ScheduledClassExample schClsExmpl = new ScheduledClassExample();
    /**
      *  Schedule structure:
      *  <Seconds> <Minutes> <Hours> <Day_of_month> <Month> <Day_of_week> <Optional_year>
      *
      * For below example it will schedulw the job @ 02:28 AM every day.
    */
    String sch = '0 28 2 * * ?';
    String schJobID = system.schedule('Scheduler Example', sch, schClsExmpl);
  }
}

To test this example instantiate CallScheduledClassExample() 
from anonymous window as,
CallScheduledClassExample inst = new CallScheduledClassExample();

Friday, June 16, 2017

apex:actionSupport with apex:param example.

1. Create apex class as below example.


/**
 * Controller class for actionSupport with passing parameter example.
 *
 **/
public with sharing class ActionSupportExamCtrl
{
 //Variable responsible for holding param value assigned in from VF.
 public String param1{get;set;}

 //action method called from actionFunction.
 public void method1(){
  System.debug('Parameter passed is '+ param1);
 }
}

2.Create VF as below example.

<apex:page  controller="ActionSupportExamCtrl">
<apex:form id="formId">
<!-- apex:actionSupport with apex:param -->
<apex:inputCheckbox>
   <apex:actionSupport action="{!method1}" event="onclick" rerender="formId">
<apex:param assignTo="{!param1}" name="prmNm" value="[ Your param value ]"/>
</apex:actionsupport>
</apex:inputCheckbox>
Click on checkbox to pass parameter from action support.
    </apex:form>
</apex:page>

apex:actionFunction with apex:param example.

1. Create an apex class as below example.
/**
 * Controller class for actionFunction with passing parameter example.
 *
 **/
public with sharing class ActionFunctionExmpCtrl
{
 //Variable for input text used in VF.
 public String firstName{get;set;}
 //Variable responsible for holding param value assigned in from VF.
 public String param1{get;set;}

 //action method called from actionFunction.
 public void method1(){
  System.debug('Parameter passed is '+ param1);
 }
}

2. Create a page like below example.

<apex:page controller="ActionFunctionExmpCtrl">
<script>
/**
 * JS method call from onClick operation.
 */
function doJSMethod(fst_name) {
//Varibale to fetch the valuse passed as a parameter on onClick event.
var fstNm = document.getElementById(fst_name).value;
//actionFunction name with parameter.
callMethod1(fstNm);
}
</script>

<apex:form id="formId">
First Name: <apex:inputText id = "f_Nm" value="{!firstName}"/><br/>
<apex:inputCheckbox onclick="doJSMethod('{!$Component.f_Nm}')"/>Click on checkbox to pass First Nmae as parameter

<!-- apex:actionFunction with apex:param -->
<apex:actionFunction action="{!method1}" name="callMethod1" rerender="formId" >
       <apex:param assignTo="{!param1}" name="prmNm" value=""/>
   </apex:actionFunction>

    </apex:form>

</apex:page>

LWC to LWC Communication using Lightning Messaging Service (Part - 2)

In my previous post ( previous post link ) we have learn how to create a Lightning Messaging Service with 6 steps and today we will use the ...