You need to sign in to do that
Don't have an account?
Scheduled Email not working...
Hi folks,
I'm trying to create a scheduled outbound email. The class queries for all of the custom objects whose date fields are today and emails people whose email addresses are recorded as custom fields within the object. Only... it doesn't work. The code, and its schedule class, below do absolutely nothing.
public class EmailMessage { public static void SendEmail() { List<Milestone1_Project__c> activeproc = new List<Milestone1_Project__c>([Select id from Milestone1_Project__c]); for( integer i=0;i<activeproc.size();i++){ if(System.Today()==date.newinstance(activeproc[i].Date__c.year(),activeproc[i].Bid_Review__c.month(),activeproc[i].Date__c.day())){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] address = new String[]{activeproc[i].Owner.Email,activeproc[i].Legal_Contributor__c,activeproc[i].Technical_Specialist__c}; mail.setToAddresses(address); for(OrgWideEmailAddress owa : [select id, Address from OrgWideEmailAddress]){ mail.setOrgWideEmailAddressId(owa.id); } mail.setSubject('Record of hours spent on Procurement: ' +activeproc[i].Name); mail.setBccSender(false); mail.setUseSignature(false); mail.setPlainTextBody('This is a test.'); List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.Email[] { mail }); System.debug('Email Sent: '+results.get(0).isSuccess() ); } } } }
And the Scheduler class:
global class ScheduleEnable implements Schedulable{ global void execute (SchedulableContext SC){ EmailMessage e = new EmailMessage(); } }
Which I've then selected to run every week day at 08:00.
Does anyone see why this might not be working? I can't figure it out at all. It looks fairly solid to me, but I'm not certain about the String array. Am I barking up the right tree?
Any help you lovely folk of the Developer Force community could give would be much appreciated.
Thanks,
Lee
I think I may have figured this out... I'm not querying for any of the email addresses/date in my initial SOQL query at the beginning of the class. Rookie mistake. I'll be able to confirm whether this is the case when the scheduled job runs in five minutes' time. Hold on to your hats...
All Answers
You have only created object of EmailMessage class in schedule class.
Try below code.
Regards,
Kamlesh Chauhan, (Founder & CTO, LogicRain Technologies)
kamlesh@logicrain.com || http://www.logicrain.com || LinkedIn
Answers/Suggestions are my own.
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Hmmm... no difference that I can immediately see.
It would appear that I'm stuck in the "Schedulable class has jobs pending or in progress" stage, according to the Developer Console. Any ideas as to why this might be? Surely it hasn't taken four hours to search through a list of 10 objects (which is how many there are total), pick out the ones whose date fields match today (which is two of them) and email three people per object found (i.e. send six emails)!?
To apply the changes which I have mentioned, you need to remove the old scheduling then only it will allow you to make the changes to the scheduled class.
After applying the same, reschedule the class one more time.
Regards,
Kamlesh Chauhan, (Founder & CTO, LogicRain Technologies)
kamlesh@logicrain.com || http://www.logicrain.com || LinkedIn
Answers/Suggestions are my own.
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Hi,
As Kamlesh mentioned, you can only change your EmailMessage class after you remove all the scheduled jobs where it is currently used.
Anyway, I could not overlook the areas of improvement in your code, keeping in mind the governor limits.
The above code is a more cleaned up version of the logic you had written.
To call this method, in the developer console, execute the code.
To have this process scheduled, you can use the scheduled apex class given by Kamlesh.
Thanks guys.
I made everyone's suggested changes and still nothing, unfortunately. The Developer Console seems to think the class has been initiated, i.e. it's still giving me the "Schedulable class..." "problem", but none of the emails have landed. Could it be to do with my OWA being the email address that Salesforce generated for inbound handling (I used that because I want people to reply to this message and to extract some of the data therefrom).
Thanks,
Lee
Try to look at your current scheduled job and abort if anything is in progress.
Make one minor change to Jerun's code.
If this doesn't helps, please attach snapshot of your developer console where you are seeting this error.
Regards,
Kamlesh Chauhan, (Founder & CTO, LogicRain Technologies)
kamlesh@logicrain.com || http://www.logicrain.com || LinkedIn
Answers/Suggestions are my own.
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Neither the line of code left in by Jerun or the correction you submitted work. Both give an error "Initial expression is of incorrect type, expected: Messaging.Email".
Also, if I look at my currently scheduled job in "Monitoring -> Scheduled Jobs" it just tells me when the job was submitted, by whom and when the next one's due. It doesn't tell me anything about the status of the job. Also, the job doesn't appear in any of the other "Monitoring" sub-sections. As an additional test, I requested an email log, and it's empty but for a couple of emails sent as part of something else I'm working on (which shares none of the same Apex, I should add).
Quite a conundrum. Thanks for your help so far though, folks. Any further ideas?
Chaning this.
to
should get you through the compile issues.
If the current class is not involved in any scheduled job, then you should be able to modify the class. Can you let me know what error you are facing now?
I'm still in the same boat...
Unfortunately, your code amendment suggestion didn't fix the compiling issue. When I revert to the old code which did compile, I'm still getting the error message on the developer console as described above, and the job itself only appears in the "Scheduled Jobs" portion of the "Monitoring" section - not in Apex Jobs or any other part of the "Monitoring" system. I still, for the life of me can't figure it out...
Hi,
Does anyone got this issue resolved?
Even I am facing the same problem.
I think I may have figured this out... I'm not querying for any of the email addresses/date in my initial SOQL query at the beginning of the class. Rookie mistake. I'll be able to confirm whether this is the case when the scheduled job runs in five minutes' time. Hold on to your hats...
Yep, that's what it was. Schoolboy error. It works fine now!
Thanks Lee.
But I think there is some other problem occurring in my code below:
I am getting the same error - 'Error: Compile Error: Initial expression is of incorrect type, expected: Messaging.SingleEmailMessage at line 109 column 62'
I am getting the rror on my last lne of code i.e:
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mailList });
Any clues on this type of error??
I reverted to the code I originally posted, as I couldn't figure that {mailList} error out. However, to avoid the governor limits problem, I'm able to trim my list by a custom date field.