create an Apex class that inserts a new account named after an incoming parameter. If the account is successfully inserted, the method should return the account record. If a DML exception occurs, the method should return null
This works finfe for me, at line 7 newAccount.name = accountName; "name" is the field name for Account Name in the Account Object, you can verify in the Account Object Fields.
I have copy-pasted the code. Now should I execute the code in the anonymous window? if yes, what would be the code in that window? Please help me out with this. Thanks
you can try this.
public class AccountHandler{
public static Account insertNewAccount(String accountName){
try{
Account newAccount = new Account();
newAccount.name = accountName;
insert newAccount;
return newAccount;
}
Catch(DMLException de){
return null;
}
}
}
public static Account insertNewAccount(String accountName){
try{
Account newAccount = new Account();
newAccount.name = accountName;
insert newAccount;
return newAccount;
}
Catch(DMLException de){
return null;
}
}
}
public class AccountHandler{
public static Account insertNewAccount(String accountName){
try{
Account acct = new Account(Name=accountName);
insert acct;
return acct;
} catch(DmlException e) {
System.debug('A DML exception has occurred:'+
e.getMessage());
return null;
}
}
}
Try this code.
public class AccountTriggerHandlerClass{
public static Account insertNewAccountRecord(String s1){
try{
Account accountObj = new Account();
accountObj.name = s1;
insert accountObj;
return accountObj;
}
Catch(DMLException de){
return null;
}
}
}
If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi