You need to sign in to do that
Don't have an account?
vanessa veron
Class Schedulable via code Apex
Hello!!!
How do I:
1 - Report on a Visualforce page a request to generate a CSV.
2 - Report on a Visualforce page the day of the week and time that I want to execute the request and send an email with the result.
3 - The class Schedulable Apex will contain the code for sending email.
How do I handle this?
I have the Schedulable class but do not know how to continue ..
How do I:
1 - Report on a Visualforce page a request to generate a CSV.
2 - Report on a Visualforce page the day of the week and time that I want to execute the request and send an email with the result.
3 - The class Schedulable Apex will contain the code for sending email.
How do I handle this?
I have the Schedulable class but do not know how to continue ..
global class CSVTest implements System.Schedulable { public String mailSouhaite {get; set;} public string inputText {get; set;} global void execute(SchedulableContext sc) { planifier(); } public void planifier(){ String query=inputText; String premier=query.substringAfter('select '); premier= premier.substringBefore('from'); string titre= premier+'\n'; string contenuCSV = titre; string queryResultatString = ''; list<sObject> queryResultat = (List<sObject>)database.query(inputText); for(sObject a: queryResultat) { queryResultatString = queryResultatString + string.valueof(a); } list<string> queryLignes = queryResultatString.split('}'); for(string s:queryLignes){ list<string> queryColonnes = s.split(','); for(string st:queryColonnes){ contenuCSV = contenuCSV + st.substringAfter('=') + ','; } contenuCSV = contenuCSV.substringBeforeLast(',').substringBeforeLast(',') + '\n'; } //Send Mail Messaging.EmailFileAttachment csvPJ = new Messaging.EmailFileAttachment(); blob csvBlob = Blob.valueOf(contenuCSV ); string csvNom = 'nom_'+Date.today().format()+'.csv'; csvPJ.setFileName(csvNom); csvPJ.setBody(csvBlob); Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage(); String[] adressMail = new list<string> {mailSouhaite}; String subject; subject = 'CSV - '+Date.today().format(); email.setSubject(subject); email.setToAddresses(adressMail); email.setPlainTextBody('Mail....'); email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ}); Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); } } } ------------------------------------------------------------------------------------------------------------------------- PAGE <apex:page controller="CSVTest"> <apex:form > <apex:PageBlock > :::::::::: TEST TEST TEST :::::::::: <br /><br /> Mail..........: <apex:inputText styleClass="classeMail" value="{!mailSouhaite}"/><br /><br /> Request : <apex:inputText styleClass="classeRequete" value="{!inputText}"/><br /><br /> <apex:commandButton value="SEND" action="{!panifier}"/> <br/><br/> </apex:PageBlock> </apex:form> </apex:page>Thank you,
Thank you
All Answers
I would generate your CSV file from a report. Generating a CSV file from a query seems like a lot of work --
I wrote a blog post a few years back on how to do most of what you need - the link is here:
http://kevindotcar.wix.com/home/apps/blog/how-to-schedule-a-report-to-send
Hope this helps.
But how can I use the code I've ever made for him to be schedulable when the User inform the day and time he wants?
so, unless I'm getting something wrong, It looks like you need to have a page that schedules data to be sent to an email addresses that may or may not be a salesforce user in your SFDC instance?
Hmmm -
Well, it would be ideal if it was possible to create a workflow rule from an APEX class - but I don't think that's posible.
What I think you need to do is from your VF page, create a record in a custom object that has the date and email address - something like this;
...and then, have a scheduled apex class that runs at, say noon or something like that and that class reads all records that are "not sent" (ie; date equals "today()") and send your report to those email addresses saved on the record.
This is the complete solution (If I got your requirements right);
0. Create an object called ScheduledReport__c that has the following fields;
Email__c (type: email)
Processed__c (type: checkbox)
ScheduledDate__c (type; Date)
1. Create the following class (a VF component controller);
2. Create the following class;
3. Create your VisualForce component;
4 Create your VisualForce email template;
...Make sure the "xstrRptname" variable is set to your report data name!!!
5. Create the class for reading your scheduled report records and processing them;
6. Create the following VF page for creating report requests;
7. From the console window, set up the scheduled job;
...And your report will be sent to the address specified on the ScheduledReport__c records at (about) 2:30.
not through the console window ...
How do I do this?
Thank you
I made changes to the code, but still have errors.
The JOB schedulable is created, but the email is not sent.
Help me please!!!
Thank you
If you put debug in your newPublier() method, you'll see that the email address (mail) is null.
But if you put debug statements in schedulejob() it's set correctly.
Basically the scheduled class is called in the future, and the email address is null because it's another instance of the class.
If you really want to keep this code structured as it is, I'd put the email address in an object that the job could get to.
Thank you