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
salesforce sfdxsalesforce sfdx 

Hi ohana ,Scheduled an apex class , but getting an error while execution please give your valuable comments

Created an apex class and added schedulable.
1.Need to run it every one hour 
2.Need to run it in office time like 6am to 6pm.
3.Need to run it to work only for 1st 10 days of starting of every month.
so, i have added the below Code, 
scheduledMerge m = new scheduledMerge();
String sch = '0 6-18 1-10 * *';
String jobID = system.schedule('Merge Job', sch, m);

Getting the error of below:

System.StringException: Unexpected end of expression.

Can you please suggest your  valuable suggestions.

Thanks 
Thanks in advance.
Best Answer chosen by salesforce sfdx
salesforce sfdxsalesforce sfdx
Hi Ankaiah,
Change in requirement that :
every day 6am to schedule it form  11th  to 30th every month.
 
global  class ReportControllerForRemaingEndDates implements schedulable{
   
    global   void execute(System.SchedulableContext Scontext){
        
        try
        {
            List<Messaging.EmailFileAttachment> attachments=new List<Messaging.EmailFileAttachment>();
            List<Report>reportList = [SELECT Id,DeveloperName,Name FROM Report where DeveloperName ='Test_Report'];
            if(reportList.size()>0){
                for(Report report:reportList){
                    String reportId = (String)report.Id;     
                    string reportName=(String)report.Name;     
                    ApexPages.PageReference objPage = new ApexPages.PageReference('/'+reportId+'?csv=1&isdtp=p1');                    
                    Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment();
                    objMsgEmailAttach.setFileName(reportName+' '+system.today().format()+'.csv');
                    if(!Test.isRunningTest())
                        objMsgEmailAttach.setBody(objPage.getContent());
                    objMsgEmailAttach.setContentType('text/csv');
                    attachments.add(objMsgEmailAttach);
                }
            }  
            OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'test.test@test.com'];
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            if ( owea.size() > 0 ) {
            mail.setOrgWideEmailAddressId(owea.get(0).Id);
            }
            mail.setUseSignature(false);
            mail.setToAddresses(new List<String>{'test.test@gmail.com'});
            mail.setSubject('Daily Report from Salesforce');
            mail.setHtmlBody('<br/><br/>Please review daily reports attached.<br/><br/><br/><br/>');
            mail.setFileAttachments(attachments);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
        catch(Exception ex)
        {
            system.debug(ex.getMessage());
        }
    }
    
    
    
    
}



 
//for only one time per day for remaining 11 to 30 days.
ReportControllerForRemaingEndDates m = new ReportControllerForRemaingEndDates();
String sch = '0 0 6 11-30 * ?';
String jobID = system.schedule('Scheduled Report from 11 to 30 days', sch, m);



its run successfully and is added in the schedulejobs as well, but at morning 6am it was not executed, can you give your comments if i had done any thing wrong

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi,

try with below code.
scheduledMerge m = new scheduledMerge();
String sch = '0 0 6,7,8,9,10,11,12,13,14,15,16,17,18 1-10 * *';
String jobID = system.schedule('Merge Job', sch, m);
Refer the below link
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
salesforce sfdxsalesforce sfdx
Hi Ankaiah,
 as per your code i tried but getting this Error..

Line: 3, Column: 1
System.StringException: Support for specifying both a day-of-week AND a day-of-month parameter is not implemented.

Thanks
salesforce sfdxsalesforce sfdx
Hi Ankaiah, 
Can you please suggest some solutions for the above error.
Thanks,
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

try with below code is working.
scheduledMerge m = new scheduledMerge();
String sch = '0 0 6,7,8,9,10,11,12,13,14,15,16,17,18 1-10 * ?';
String jobID = system.schedule('Merge Job', sch, m);
if this helps, Please mark it as best answer.

Thanks!!
 
salesforce sfdxsalesforce sfdx
Hi Ankaiah,
Change in requirement that :
every day 6am to schedule it form  11th  to 30th every month.
 
global  class ReportControllerForRemaingEndDates implements schedulable{
   
    global   void execute(System.SchedulableContext Scontext){
        
        try
        {
            List<Messaging.EmailFileAttachment> attachments=new List<Messaging.EmailFileAttachment>();
            List<Report>reportList = [SELECT Id,DeveloperName,Name FROM Report where DeveloperName ='Test_Report'];
            if(reportList.size()>0){
                for(Report report:reportList){
                    String reportId = (String)report.Id;     
                    string reportName=(String)report.Name;     
                    ApexPages.PageReference objPage = new ApexPages.PageReference('/'+reportId+'?csv=1&isdtp=p1');                    
                    Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment();
                    objMsgEmailAttach.setFileName(reportName+' '+system.today().format()+'.csv');
                    if(!Test.isRunningTest())
                        objMsgEmailAttach.setBody(objPage.getContent());
                    objMsgEmailAttach.setContentType('text/csv');
                    attachments.add(objMsgEmailAttach);
                }
            }  
            OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'test.test@test.com'];
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            if ( owea.size() > 0 ) {
            mail.setOrgWideEmailAddressId(owea.get(0).Id);
            }
            mail.setUseSignature(false);
            mail.setToAddresses(new List<String>{'test.test@gmail.com'});
            mail.setSubject('Daily Report from Salesforce');
            mail.setHtmlBody('<br/><br/>Please review daily reports attached.<br/><br/><br/><br/>');
            mail.setFileAttachments(attachments);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
        catch(Exception ex)
        {
            system.debug(ex.getMessage());
        }
    }
    
    
    
    
}



 
//for only one time per day for remaining 11 to 30 days.
ReportControllerForRemaingEndDates m = new ReportControllerForRemaingEndDates();
String sch = '0 0 6 11-30 * ?';
String jobID = system.schedule('Scheduled Report from 11 to 30 days', sch, m);



its run successfully and is added in the schedulejobs as well, but at morning 6am it was not executed, can you give your comments if i had done any thing wrong
This was selected as the best answer
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

the scheduler runs always in the time zone of the user who has started the scheduler. 

Can you check your user time zone and it will based on your time zone.

Can you go to the scheduled jobs and check next schedule date & time now?


THanks!!
 
salesforce sfdxsalesforce sfdx
Hi anaiah, 
The company timezone is (GMT-04:00) Eastern Daylight Time (America/Toronto),it should fire as per this timezone
ReportControllerForRemaingEndDates m = new ReportControllerForRemaingEndDates();
String sch = '0 0 6 11-30 * ?';
String jobID = system.schedule('Scheduled Report from 11 to 30 days', sch, m);

 i had user changed the timezone with (GMT-04:00) Eastern Daylight Time (America/Toronto).

i deleted the scheduled job and sheduled a new one with the same code.please let me know if i need to add 2022 as an year or some thing need to add in the code 

or 
Need to make the apex class as future callout

or something else
salesforce sfdxsalesforce sfdx
Thanks  
Ankaiah