function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pranav ChitransPranav Chitrans 

Cron expression : every 30 minutes

Hi,

I need one help for Cron Expression for sechduling my batch at certain interlval which would be a lighitning web component, and whatever period sleected in the drop down i will take that sync period as my parameter to calculate cron expression. My interval could be every 30 minute, hourly, daily. And the moment user click on custom sechdule button from LWC page from that time the calculation should start, not from the near by hours. For example user click on button at 18th July 2019 10:34 A.M and he seleted the interval from drop down as '30 Minutes' then the next sechdueld run should be 18th July 2019 11:04 A.M, not the 10:30 A.M because salesforce takes the near by hours by default which i did not wanted in my solution.

I have written the logic for hourly and daily which seems workin fine but i am ot sure too for that as well.
if(syncPeriod == 'Every 30 Minutes') {
	cronExpressionfromUI = '0 '+system.now().addMinutes(30).minute()+' * * * ?';
	
	cronExpressionfromUI = '0 10'+' *'+' *'+' *'+' ?';
	}
	else if(syncPeriod == 'Hourly') {
		cronExpressionfromUI = '0 '+system.now().minute()+' * * * ?';
	}
	else if(syncPeriod == 'Daily') {
		cronExpressionfromUI = '0 0 '+system.now().addHours(1).hour()+' *'+' *'+' ?';
	}
}
For the first condition( every 30 minute) i am facing an issue which are listed below :
  • If i use expression('0 '+system.now().addMinutes(30).minute()+' * * * ?') the it runs for the 1 st time but for the next run after completion of one it gets sechduled for 1 hour automatically.
  • If i use expression for 10 minute(for testing purpose) something like(cronExpressionfromUI = '0 10'+' *'+' *'+' *'+' ?';) then it always calculate from the near by hours not the moment from when button was clicked.
  • I do not want to run the system.sechdule multiple time as per my sync interval timing because that might hit governer limit.
I tried few other expression as well, but i wont help.
Please assist at earliest.

Thanks
Pranav
 
Deepali KulshresthaDeepali Kulshrestha
Hi Pranav,

Try the following code, it maybe helpful for you:
if(syncPeriod == 'Every 30 Minutes') {
    cronExpressionfromUI = '0 0/30 * 1/1 * ? *';
    }
if(syncPeriod == 'Hourly') {
        cronExpressionfromUI = '0 0 0/1 1/1 * ? *';//Start time    12 0 0 0/1 1/1 * ? *
    }
if(syncPeriod == 'Daily') {
        cronExpressionfromUI = '0 0 12 1/1 * ? *';//Start time    12
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 
Pranav ChitransPranav Chitrans
Hi Deepali,

The expression which you used for 30 minutes will not be going to work because that expression is not correct.

Thanks
Pranav
Deepali KulshresthaDeepali Kulshrestha
Hi Pranav,

I don't understand why this '0 0/30 * 1/1 * ? *' cron expression not work.But if you want a cron expression to run every half an hour:
The cron expression '0 0,30 * * * *' is run every half an hour:
if(syncPeriod == 'Every 30 Minutes') {
    cronExpressionfromUI = '0 0,30 * * * *';
    }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha