Monday, September 22, 2014

Getting today date in some other format !

I recently came across a requirement in which date was requited in a format of MM/DD/YY but the default format is given as '2014-09-22 00:00:00'
Code to change date format 
Date d = Date.Today();
       String MonthValue = String.valueof(Date.Today().month());
       if(MonthValue != null && MonthValue != '' && MonthValue.length() == 1){
         MonthValue = '0'+MonthValue;
       }
       String DateValue = String.valueof(Date.Today().day());
       if(DateValue != null && DateValue != '' && DateValue.length() == 1){
         DateValue = '0'+DateValue;
       }
       String Last_Date_Modified = String.valueof(Date.Today().year())+'-'+MonthValue+'-'+DateValue;
       string StringToBePassed = 'Last_Date_Modified='+Last_Date_Modified;

Monday, September 15, 2014

Running a batch class at every 5 minuites

Recently I came across a requirement in which a batch class was made to run for every 5 minuites though this cannot be fullfilled using a scheduler class to make the batch class run every 5 minuites

//Setting the scheduled time
String CRON_EXP1 = '0 5 * * * ?' ;
String CRON_EXP2 = '0 10 * * * ?' ;
String CRON_EXP3 = '0 15 * * * ?' ;
String CRON_EXP4 = '0 20 * * * ?' ;
String CRON_EXP5 = '0 25 * * * ?' ;
String CRON_EXP6 = '0 30 * * * ?' ;
String CRON_EXP7 = '0 35 * * * ?' ;
String CRON_EXP8 = '0 40 * * * ?' ;
String CRON_EXP9 = '0 45 * * * ?' ;
String CRON_EXP10 = '0 50 * * * ?' ;
String CRON_EXP11 = '0 55 * * * ?' ;
String CRON_EXP0 = '0 0 * * * ?' ;

//Batch class variable
scheduledBatchClassToInsertAllLists bt1 = new scheduledBatchClassToInsertAllLists();
scheduledBatchClassToInsertAllLists bt2 = new scheduledBatchClassToInsertAllLists();
scheduledBatchClassToInsertAllLists bt3 = new scheduledBatchClassToInsertAllLists();
scheduledBatchClassToInsertAllLists bt4 = new scheduledBatchClassToInsertAllLists();
scheduledBatchClassToInsertAllLists bt5 = new scheduledBatchClassToInsertAllLists();
scheduledBatchClassToInsertAllLists bt6 = new scheduledBatchClassToInsertAllLists();
scheduledBatchClassToInsertAllLists bt7 = new scheduledBatchClassToInsertAllLists();
scheduledBatchClassToInsertAllLists bt8 = new scheduledBatchClassToInsertAllLists();
scheduledBatchClassToInsertAllLists bt9 = new scheduledBatchClassToInsertAllLists();
scheduledBatchClassToInsertAllLists bt10 = new scheduledBatchClassToInsertAllLists();
scheduledBatchClassToInsertAllLists bt11 = new scheduledBatchClassToInsertAllLists();
scheduledBatchClassToInsertAllLists bt0 = new scheduledBatchClassToInsertAllLists();

//Scheduling the batch class for the specific time
System.schedule('Hourly Batch Schedule job1', CRON_EXP1, bt1);
System.schedule('Hourly Batch Schedule job2', CRON_EXP2, bt2);
System.schedule('Hourly Batch Schedule job3', CRON_EXP3, bt3);
System.schedule('Hourly Batch Schedule job4', CRON_EXP4, bt4);
System.schedule('Hourly Batch Schedule job5', CRON_EXP5, bt5);
System.schedule('Hourly Batch Schedule job6', CRON_EXP6, bt6);
System.schedule('Hourly Batch Schedule job7', CRON_EXP7, bt7);
System.schedule('Hourly Batch Schedule job8', CRON_EXP8, bt8);
System.schedule('Hourly Batch Schedule job9', CRON_EXP9, bt9);
System.schedule('Hourly Batch Schedule job10', CRON_EXP10, bt10);
System.schedule('Hourly Batch Schedule job11', CRON_EXP11, bt11);
System.schedule('Hourly Batch Schedule job0', CRON_EXP0, bt0);
This will run the scheduled batch class for every 5 minutes till infinity !

Getting the current profile of user or checking the current profile of the user !

Hi below I have described how can u do a simple profile check on the user without creating any id list or set
//Creating a list to get the current user via the UserInfo.getProfileId()
List pro = [SELECT id,Name FROM Profile  where id = :UserInfo.getProfileId()];

//checking the profile via the name
if(pro[0] != null &&  pro[0].name.contains('Profile name'))

//