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 

Verify SOQL

Hi...
how can I test a request SOQL in Apex?

string queryResultatString = '';
   // requete: user inform into VisualPage
    list<sObject> queryResultat = (List<sObject>)database.query(requete);
    for(sObject a: queryResultat)
    {
        queryResultatString = queryResultatString + string.valueof(a);
        
    }
Thank you...
bob_buzzardbob_buzzard
If you are referring to unit testing, you'd create some records that should be found by the query, execute the method and then verify the correct number were found.  If you just want to test it out to see if it is valid, take a look at the execute anonymous capability:

https://help.salesforce.com/apex/HTViewHelpDoc?id=code_dev_console_execute_anonymous.htm&language=en
vanessa veronvanessa veron
I need another thing:

I have an Apex module that the client reports a request and then an email is sent to him with the result of that request.
But if the client report a wrong REQUEST, is possible to send an email saying that the request is incorrect?
How do I test the REQUEST to send an email saying OK or NOT OK request?

Thank you
Vinit_KumarVinit_Kumar
The way I would approach is to check whether the query is returning any rows or not.If it is,that means that the request was OK and if not then it was NOT OK.

And,if it was NOT OK send am email to the requester.

Hope this helps !!
vanessa veronvanessa veron
And how do I do it?
bob_buzzardbob_buzzard
If the request can result in bad SOQL that will cause an error, you can simply use exception handling to trap that:

string queryResultatString = '';
// requete: user inform into VisualPage
try
{
    list<sObject> queryResultat = (List<sObject>)database.query(requete);
    for(sObject a: queryResultat)
    {
        queryResultatString = queryResultatString + string.valueof(a);
        
    }
}
catch (Exception e)
{
  // the query failed - reason is in e.getMessage();
}


vanessa veronvanessa veron
I will Try, thank you....
vanessa veronvanessa veron
It didn't solve my problem, why????

I used TRY and CATCH:

string queryResultatString = '';
    list<sObject> queryResultat;
   
    try {
        queryResultat = (List<sObject>)database.query(requete);
        for(sObject a: queryResultat)
        {
            queryResultatString = queryResultatString + string.valueof(a);
        }
    }
    catch (System.QueryException q)
    {
      String errMsg = q.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'+errMsg );   
      Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});      
    }

bob_buzzardbob_buzzard
I don't know - perhaps if you explain what the problem is I might be able to help.  

If you look at my reply, I started with "if the request can result in bad soql' - is that the situation you are in?  If not, you might need to count the number of returned items. Its impossible to tell from a couple of lines of code.
vanessa veronvanessa veron
how do I count the lines of a request?
bob_buzzardbob_buzzard
string queryResultatString = '';
// requete: user inform into VisualPage
try
{
    list<sObject> queryResultat = (List<sObject>)database.query(requete);
    if (queryResultat.isEmpty())
    {
       // the SOQL query was valid, but no matches were found
    }
    else
    {
        for(sObject a: queryResultat)
        {
            queryResultatString = queryResultatString + string.valueof(a);
        
        }
    }
}
catch (Exception e)
{
  // the query failed - reason is in e.getMessage();
}

vanessa veronvanessa veron
Thank you...
But I have the same problem

string queryResultatString = '';
    list<sObject> queryResultat;
    try {
        queryResultat = (List<sObject>)database.query(requete);
        if (queryResultat.isEmpty()){
                String errMsg = 'ERROR QUERY';
              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{
            for(sObject a: queryResultat)
            {
                queryResultatString = queryResultatString + string.valueof(a);
            }
       }
    }
    catch (System.QueryException q){q.getMessage();}

bob_buzzardbob_buzzard
But once again you don't tell us what the problem is - just that you have one and some code.  

What is the problem that you are seeing?
vanessa veronvanessa veron
Thank you and sorry:

PROBLEM: I do not get the email ...

The request informed NOT CORRECT: select name from Cont
vanessa veronvanessa veron

Hiii...

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});
        }
    }
I would like to send an email containing the error message if the wrong type a User request (example: select name from cont).

I tried the above code but not get the email when the request is wrong.
Help me!
Vinit_KumarVinit_Kumar
Where are you populating adressMail,I see this below :- 

String[] adressMail = new list<string> {mail};

What is mail here which you are adding to the List ??
vanessa veronvanessa veron
The email I get the Visualforce page (this is not the problem).

The problem is how to send the email saying the wrong request when the User informs 'select name from Cont' or 'dddddddddddddddddddddddddddddddd'
Vinit_KumarVinit_Kumar
If the adressMail is getting populated correctly,I don't see any reason why the email would not go out ...

The way I would approach is to check the debug logs and see what's there in Messaging.SendEmailResult [] envoyer,that should show me the if there is a success or failure ...

Hope this helps !!
vanessa veronvanessa veron
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 ...

How do I send email error stating the request??
Vinit_KumarVinit_Kumar
You are catching exception using query Exception,I would suggest to use only Exception which would cover all exception instead of only Query exception.Change your code from 

catch (System.QueryException e){

to

catch (Exception e){

and then try !!
vanessa veronvanessa veron
I will test.... thank you!
vanessa veronvanessa veron
The Apex code not send email when I inform ' select name from cont'...
WHY????????????????????????

string queryResultatString = '';
    list<sObject> queryResultat;
    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 (Exception e){
        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});
    }