+ Start a Discussion
Thangamani Nachimuthu 6Thangamani Nachimuthu 6 

Executing the 'insertNewAccount' method failed. Either the method does not exist, is not static, or does not insert the proper account.

Hi Experts,
  Pls help me to solve the below issue.I am trying to take a trailhead challenge and i have created the below class and it is working fine able to create the account ,but when i am checking the challenge completion i am getting the error.

public class AccountHandler {
      public static Object insertNewAccount(String strNames)
      {
          try
          {
          Account acct = new Account();
            acct.Name = strNames;
            acct.Phone = '(415)555-1212';
            acct.NumberOfEmployees = 100;
        insert acct;
        //ID acctID = acct.Id;
// Display this ID in the debug log
//System.debug('ID = ' + acctID);
              return acct;
          }
          catch(DmlException  ex)
          {
              System.debug('Got the Error:' +ex.getMessage());
              return null;
          }
      }

}

Challenge not yet complete in My Trailhead Playground 1
Executing the 'insertNewAccount' method failed. Either the method does not exist, is not static, or does not insert the proper account.
Best Answer chosen by Thangamani Nachimuthu 6
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Yes exactly .The return type of the method should be the type which you are returning from your method.

All Answers

Akshay Agarwal 36Akshay Agarwal 36
Use this :
public class AccountHandler {
      public static void insertNewAccount(String strNames)
      {
          try
          {
            Account acct = new Account();
            acct.Name = strNames;
            acct.Phone = '(415)555-1212';
            acct.NumberOfEmployees = 100;
              
             insert acct;
        //ID acctID = acct.Id;
// Display this ID in the debug log
//System.debug('ID = ' + acctID);
              system.debug(acct.Id);
          }
          catch(DmlException  ex)
          {
              System.debug('Got the Error:' +ex.getMessage());
              
          }
      }

}
Thangamani Nachimuthu 6Thangamani Nachimuthu 6
Hi Akshay,
Below is my problem statement.
The Apex class must be called AccountHandler and be in the public scope
The Apex class must have a public static method called insertNewAccount
The method must accept an incoming string as a parameter, which will be used to create the Account name
The method must insert the account into the system and then return the record
The method must also accept an empty string, catch the failed DML and then return null

For the above statement only i have written the code which i have posted.i am getting the below error.
Challenge not yet complete in My Trailhead Playground 1
Executing the 'insertNewAccount' method failed. Either the method does not exist, is not static, or does not insert the proper account.


Regards,
Thangamani
Akshay Agarwal 36Akshay Agarwal 36
//Try it.If it worked marked my answer as the best answer
public class AccountHandler {

    public static Account insertNewAccount(String name) {

        Account account = new Account(Name = name);

        try {

            insert account;

        } catch (System.DmlException e) {

            return null;

        }

        return account;

    }

}
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Thangamani,
The return type of AccountHandler should be account because you have to return the account after it is inserted.
try this code
public class AccountHandler {
    public static Account insertNewAccount(String accname) {
        Account acc = new Account();
        acc.Name = accname;
        try
        {
            insert acc;
        } catch (Exception e) {
            return null;
        }
        return acc;
    }
}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
Thangamani Nachimuthu 6Thangamani Nachimuthu 6
Hi Devi Chandrika ,
Thanks for your reply and it solved my issue and i have a query like suppose if i want to return contact object then i should add like below
public static  Contact insertNewContac()
{
}
Regards,
Thangamani
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Yes exactly .The return type of the method should be the type which you are returning from your method.
This was selected as the best answer
Thangamani Nachimuthu 6Thangamani Nachimuthu 6
Hi Devi Chandrika,
   Now i am trying for another trailhead challenge and below is my problem statement and my code and error as well.Kindly suggest me.
The Apex class must be called ContactSearch and be in the public scope
The Apex class must have a public static method called searchForContacts
The method must accept two incoming strings as parameters
The method should then find any contact that has a last name matching the first string, and mailing postal code (API name: MailingPostalCode) matching the second string
The method should finally return a list of Contact records of type List that includes the ID and Name fields

My Code:
public class ContactSearch {
    public static contact[] searchForContacts(string lstName,string mpostalcode)
    {
        contact[] cts = [SELECT FirstName,LastName,ID
                          FROM Contact WHERE LastName=:lstName AND mailingpostalcode=:mpostalcode];
            
            return cts;
        
    }

}

I am getting the below error in trailhead challenge.
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Loan_Amount__c]: [Loan_Amount__c]

Kindly suggest me.

Regards,
Thangamani
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
try this code.In your query you are returning firstname,lastname but in the challenge it is mentioned to return id and name fields.
public class ContactSearch {
    public static contact[] searchForContacts(string lstName,string mpostalcode)
    {
        contact[] cts = [SELECT ID,name
                          FROM Contact WHERE LastName=:lstName AND mailingpostalcode=:mpostalcode];
            
            return cts;
        
    }

}

If still you are unable to complete challenge,try using a new playground.

Hope this helps you
Akshay Agarwal 36Akshay Agarwal 36
Hi Thangamani Nachimuthu 6,
You are getting an error because you had created a field Loan_Amount__c which is required in your org. Try to delete that field or create a new playground and then try this :

public class ContactSearch {
    public static List<Contact> searchForContacts(String lastName,String mailingPostalCode)
        
    {
        List<Contact> contacts=[SELECT Id, Name, LastName, MailingPostalCode
                                 FROM Contact
                                 WHERE LastName = :lastName AND MailingPostalCode = :mailingPostalCode];
        return contacts;
    }         
        
    }
Hope this helps you. If worked mark it as the best answer.
Thangamani Nachimuthu 6Thangamani Nachimuthu 6
Hi Akash,
I have got the issue that Loan_Amount__C custom field is marked as Required field in the page layout so it is looking for th evalue to input.I have unchecked the requirement field option and it started working.

Thanks for your suggestion.