0
@RemoteAction
public static  List<Account> GetAccountOwnerEmail(String AccountId) {
    List<Account> lstAccount = new List<Account>();
    String [] conFields = new string[]{'Email'};
    if ( !se.isFieldAccessible('Contact',conFields,FLS_Security.FLSType.ISACCESS) ) {
        throw new MyCustomeException ('You do not have sufficient privilege to perform this operation for Contact, Please contact to system admin');
    }
    try {
        String queryString ='Select Id ,(Select Id,email from contacts) from Account where Id= :AccountId'; 
        //return(database.Query(queryString)); 
        lstAccount = database.Query(queryString);
        return lstAccount;  
    } catch ( Exception e ) {
        ErrorLog.createErrorRecord(e.getStackTraceString(),e.getMessage(),e.getTypeName());
        throw e;
    }       
}

closed as off-topic by Himanshu, Mohith Shrivastava, Ratan Paul, Vigneshwaran G, Doug B May 6 '16 at 14:14

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Himanshu, Mohith Shrivastava, Ratan Paul, Vigneshwaran G, Doug B
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Your question appears to be asking for other people to write code for you. SFSE is a Q&A forum where we look to help each other by asking and answering questions about real problems or errors with our code. Posting requests to write code for you will get closed. If you post what you've written so far and where you're stuck, I'm confident you'll find plenty of people willing to assist you – Himanshu May 6 '16 at 12:56
  • As told by @ninja.hk try by passing some junk value to parameter accountId like 123 it will give you error "ERROR at Row:1:Column:30 invalid ID field: 123" and execute catch block. – San May 6 '16 at 20:09
1

The try block contains only a string assignment statement and a query statement. The string created is straightforward, so no chance of getting an exception there.

Another statement is Database.query() which may throw an exception if the query is wrong but as the query string is hardcoded there, you cannot get a QueryException as well.

So to force an exception you will have to change the code a little bit. Instead of directly using the variable AccountId in query, first create a local variable of type Id in try block, assign the value of AccountId to it and use this variable in query.

Here if the value of AccountId is not actually an Id, a StringException will be thrown saying Invalid Id while assigning it's value to variable with Id type.

Not the answer you're looking for? Browse other questions tagged or ask your own question.