Wednesday, 30 October 2013

Schedule the batch class for every 1 hour.



At first we should write a Batch class without a schedule code in it.
create the below schedule class.

Schedule Class


global class ScheduleClass implements Schedulable {
   global void execute(SchedulableContext sc) {
      BatchClass  b = new BatchClass(); 
      database.executebatch(b);
   }
}

After saving that schedule class append the code which I have highlighted in red color in finish method of batch Apex.

Batch Class



global class BatchClass implements Database.Batchable<sobject>
{
        global Database.QueryLocator start(Database.BatchableContext BC){
             String Query='Select LastName,Id from Lead';
              return Database.getQueryLocator(Query);
        }
        global void Execute(Database.BatchableContext BC,List<sobject> Scope){
                List<Lead> ld = Scope;
                for(Lead l: ld){
                        l.LastName = l.LastName+'Batch testing';
               }
       }
       global void finish(Database.BatchableContext BC){
              Datetime sysTime = System.now();
              sysTime = sysTime.addMinutes(60);
              String chron_exp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + 
              sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();
              system.debug(chron_exp);
              ScheduleClass ssr = new ScheduleClass();  // Schedule class name
              String sch = 'Schedulejob' + chron_exp;
              List<AsyncApexJob> aaj = [select ApexClassID,CompletedDate,ExtendedStatus,
                                                                       JobItemsProcessed,JobType,MethodName, 
                                                                       NumberOfErrors,Status,TotalJobItems 
                                                                      from AsyncApexJob
                                                                      where status='queued'];
              if(aaj.size()== 0){
                   system.schedule(sch,chron_exp,ssr); 
              }
       }
}



The chron_exp will used to schedule the class for every 1 hour based on the System Time.


we need to execute the above code as a anonymous using developer console.
Once it has been started to execute, it will automatically schedule the apex class for every 1 hour.

Tuesday, 29 October 2013

Simple Batch Apex with Schedule Class


Batch Class


global class BatchClass implements Database.Batchable<sobject>
{
        global Database.QueryLocator start(Database.BatchableContext BC){
             String Query='Select LastName,Id from Lead';
              return Database.getQueryLocator(Query);
        }

        global void Execute(Database.BatchableContext BC,List<sobject> Scope){

                List<Lead> ld = Scope;
                for(Lead l: ld){
                        l.LastName = l.LastName+'Batch testing';
               }
        }
        global void finish(Database.BatchableContext BC){     
     }
}


Schedule Class



global class ScheduleClass implements Schedulable {
   global void execute(SchedulableContext sc) {
      BatchClass  b = new BatchClass(); 
      database.executebatch(b);
   }
}


Saturday, 26 October 2013

Format the Date field in visualforce and VF Email Template

Below is the code which is used to format the date in Visualforce page and VF Email Template

<apex:outputText value="{0,date,MM/dd/YYYY}">
    <apex:param value="{!relatedTo.closeDate}" />
</apex:outputText> </td>

Output:

08/11/2010 


Friday, 25 October 2013

Freeze Button on User detail Page

This button is available from Winter 14 releases.

In a Very big organization, if you want to deactivate an user Account, it is not possible because there will be lot of dependency in Assignment rules, Public groups etc. So in winter 14 release salesforce has introduced Freeze button on User detail page, when you click that button, It will prevent the user from login to their salesforce organization.

Really very nice to have this button on User Detail Page!!!

Note: 
  1.  Freezing User account doesn't make their user licenses available, to make their User license available we have to deactivate the user Account.
  2. To freeze the User, the User should have Manage User Permission.




Thursday, 24 October 2013

Apex sharing for Activity Object

There is no Eventshare and TaskShare object in salesforce. The sharing will be controlled by Who and What field of the Object. so we cant create sharing rules or write Apex sharing...