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
vanessa veronvanessa veron 

Query Verify

hello
I would like to send an email containing the error message if the wrong type a User request (example: select name from cont or selecttttttttttttttttttttttt).

I tried the above code but not get the email when the request is wrong.
Help me!

string queryResultatString = '';
    list<sObject> queryResultat;
    System.QueryException q;
    try {
        queryResultat = (List<sObject>)database.query(requete);
        if (queryResultat.isEmpty()){
              String errMsg = 'EMPTY';
              Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
              String[] adressMail = new list<string> {mail};
              String subject = Label.StreamReport+' - '+Date.today().format();
              email.setSubject(subject);
              email.setToAddresses(adressMail);
              email.setPlainTextBody(Label.Hi+'\n\n'+errMsg );   
              Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        }
        else
        if (!queryResultat.isEmpty()){
            for(sObject a: queryResultat)
            {
                queryResultatString = queryResultatString + string.valueof(a);
            }
       }
    }
    catch (System.QueryException e){
        if (e.getMessage() != null){
        String error = e.getMessage();
        Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
        String[] adressMail = new list<string> {mail};
        String subject = Label.StreamReport+' - '+Date.today().format();
        email.setSubject(subject);
        email.setToAddresses(adressMail);
        email.setPlainTextBody(Label.Hi+'\n\n'+error );   
        Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        }
    }

Anup JadhavAnup Jadhav
Where is this code used? In a trigger? Controller? Batch job?
vanessa veronvanessa veron
Is a method, class Schedulable
vanessa veronvanessa veron
Method:   newPublier();

global void execute(SchedulableContext sc) {
  newPublier();
}