You need to sign in to do that
Don't have an account?
Thangamani 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.
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.
All Answers
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());
}
}
}
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
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;
}
}
The return type of AccountHandler should be account because you have to return the account after it is inserted.
try this code
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
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
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
If still you are unable to complete challenge,try using a new playground.
Hope this helps you
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.
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.
public class AccountHandler {
public static Account insertNewAccount(String s){
try{
Account acc=new Account(Name=s);
insert acc;
return acc;
}
catch (DmlException e ){
System.debug('A DML exception has occured: '+ e.getMessage());
return null;
}
}
}
public class AccountHandler {
public static Account 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;
}
}
}