Monday 21 September 2015

Roll up Summery Trigger

Sales force provided standard roll up field facility in case of master detail relationship.However if you have lookup relationship and you want to built that functionality you need to built through trigger .
Below one example which has some  generic API names needs to  replace with specific .



1.Child_Object__c  needs to replace with child object API .
2.Parent_Object__c needs to replace with parent  object API
3.LookUpFld__c needs to replace with the lookup field API .
4.Parent_Fld_To__c  needs to replace with the parent filed to which roll up amount will update .
5.Child_Fld_From__c  Needs to replace with child field from the value will roll up .

trigger RollUpFromChildToParent on Child_Object__c ( after insert, after update,after delete,after undelete) {
Set<Id> prIdSet=new Set<Id>();
List<Parent_Object__c> pListToUpdate=new List<Parent_Object__c>();
if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
for(Child_Object__c cobj : Trigger.new){
   if(cobj.LookUpFld__c != null)
prIdSet.add(cobj.LookUpFld__c);     
        }
}If(Trigger.isDelete){
  for(Child_Object__c cobj : Trigger.old){
if(cobj.LookUpFld__c != null)
prIdSet.add(cobj.LookUpFld__c);     
        }
}
   for(AggregateResult res : [SELECT LookUpFld__c,sum(Child_Fld_From__c)can FROM Child_Object__c WHERE LookUpFld__c IN :prIdSet GROUP BY LookUpFld__c]) {
     pListToUpdate.add(new Parent_Object__c(Id=(Id)res.get('LookUpFld__c'),Parent_Fld_To__c=(Double)res.get('can')));
}
try{
 update pListToUpdate;
}catch(DmlException de){
 System.debug(de);
}
}


Tuesday 15 September 2015

Schedule Apex in Depth

In general English Schedule means  a ​list of the ​times when ​events are ​planned to ​happen . 
What I believe that all naming convention are always with justification .So according to that in simple way if you want to planned any thing in future know as schedule .
Sales force has given that facility in apex ,report,data loader etc .

The class which implements the Schedulable interface is know as Schedule class in apex.
 We can execute that class in future automatically as per our need ,still have some limitations will discuss later .

We can schedule a class with help of standard sales force UI or by executing schedule method in system class .

Lets discuss in detail :

Basically Schedulable  Interface has only one method execute .

Below is the signature of the method which is in  System Names Space.

public void execute(SchedulableContext context) 

To know more detail about this method and class you can click  link  Interface Detail.


Once you will implement this interface then you need to implement execute method .

In execute method signature we need to pass instance of SchedulableContext what exactly it is and
why we have to pass this in method .
Again it is one more interface in Apex which has one method which will return the Id of corn trigger Object which stores the detail about the schedule Job.
You can look into the signature of the method which returns an id which is the CornTrigger record related to Schedule Job.

There are two objects related to Schedule apex .
1,Corn Detail

  •    Contains details about the associated scheduled job, such as the job’s name and type. 
  •    This object is available in API version 29.0 and later.
  •    Name contains the name of the job we are giving while executing the schedule class .
  •    Type is picklist which contains "7" for schedule apex ,As same object create record when we         are scheduling from any source i,e apex,report,data export .

2.Corn Trigger

  •      Contains schedule information for a scheduled job.
  •      Object is available in API version 17.0 and later.
  •       Major fields in this object are    below 


      1. CronExpression (while executing the class we are passing 3 parameter this is one )
      2. CronJobDetailId(Id od the corn detail)
      3.  StartTime(when the job started )
      4. EndTime(Till what time this job is schedule)
      5. PreviousFireTime(recently when the job fired)
      6. NextFireTime(Next when it will fire )
      7. TimesTriggered(How many times the job fired)
      8. State(Current job in which state),below are the picklist values

                                     WAITING—Job is waiting for execution.
                                    ACQUIRED—Job has been picked up by the system and is about
                                                             to execute.
                                    EXECUTING—The job is executing.
                                    COMPLETE—Trigger has fired and is not scheduled to fire again.
                                    ERROR—The trigger definition has an error.
                                    DELETED—The job has been deleted.
                                   PAUSED—This state during patch and major releases.After the  release, the                                                              job state is automatically set to WAITING or another state.


public Id getTriggerId()

To know more detail about this method and class you can click  link 

How to write Schedule class .

global class scheduledDemo implements Schedulable {
   global void execute(SchedulableContext scon) {
      //Here we need to write Business logic or we can call other class .
   }
}

Now this class you can either schedule with standard UI as discussed or you can schedule  by executing some code .

How to Schedule using standard UI .

To Schedule any class we need to follow below steps
Setup >> ApexClasses >> Click on Button (Schedule Apex)

Below image we need to give one name and the class which we need to schedule .Below image is for weekly view ,we can also select monthly view .






In case our requirement will not fulfill in standard UI ,we should build our own expression to achieve.

Lets discuss :