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!

if I submit one request that returns empty value, it sends email
if I submit one request that returns values, it sends email
if I report a wrong request, it does not send email ... WHY????????????
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});
        }
    }

Best Answer chosen by vanessa veron
Denis VakulishinDenis Vakulishin
I suggest you to ask SF support team about that, if you have appropriate license. I cannot undertand the reason, because I cannot reproduce it.
The only thing I can suggest now is to check your emails(sender and reciever), maybe there is some mistake. But basing your description it shouldn't send emails in the other cases.

All Answers

Denis VakulishinDenis Vakulishin
Hello Vanessa,
Could you provide example of the "Wrong" request ?
vanessa veronvanessa veron
select name from cont

or

selecttttttttttttttttttttttt

or

fsdfsdfsdfdsfsdfsdfsdfsdf
Denis VakulishinDenis Vakulishin
I've tried wrong request and it sends email for me.
Try to request "Email Logs" and look there.
vanessa veronvanessa veron
Thank you!!!

how i do it?
Denis VakulishinDenis Vakulishin
1)In setup type in search "Email Log Files"
2) Press Request an Email Log
3) Set start/end date of neede interval. and set email to which you sent emails in your code.

It can take some time before you recieve log files. They are in CSV format(as I remember), There you can look at delivery status and error type if it available.
vanessa veronvanessa veron
Thank you!
vanessa veronvanessa veron
I tried, but its ok:

Date Time, Internal Message ID, Mail Event, Recipient, Sender, Remote Host, Bytes Transferred, Salesforce.com User, Message ID Header, Retry Count, Seconds In Queue, Delivery Stage, Relay Address, ,
"07/24/2014 07:20:17","84/19-29354-1B3B0D35",R,"v.vvvvvv@vvvvvvvv.com","vvv******=gmail.com__q4ruhdb3qrcmut2o@l4tl7za50e4.2-mybjeam.eu0.bnc.salesforce.com","10.237.71.4",3559,00520000003HHTG,"<-4Jvo000000000000000000000000000000000000000000000N97GDD00-GCi-aVnTFWOoiCN2p4HoA@sfdc.net>",,,,,,
vanessa veronvanessa veron
There isn't ERRORS...
vanessa veronvanessa veron
is very strange.
Why did I not receive emails?

The line: System.debug('ALGO ACONTECEU AQUI: ' + e.getMessage()); works!

What am I doing wrong?

catch (Exception e){
        System.debug('ALGO ACONTECEU AQUI: ' + e.getMessage());
        Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
        email.setSubject(Label.StreamReport+' - '+Date.today().format());
        String[] adressMail = new list<string> {'v.pvvvvvo@vvvvvvvv.com'};
        email.setToAddresses(adressMail);
        email.setPlainTextBody(Label.Hi+'\n\n'+e.getMessage() ); 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }

Denis VakulishinDenis Vakulishin
I suggest you to ask SF support team about that, if you have appropriate license. I cannot undertand the reason, because I cannot reproduce it.
The only thing I can suggest now is to check your emails(sender and reciever), maybe there is some mistake. But basing your description it shouldn't send emails in the other cases.
This was selected as the best answer
vanessa veronvanessa veron
thank you!