+ Start a Discussion
KMK91KMK91 

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

Manj_SFDCManj_SFDC
Hi Madhukar,
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;
    
           }
    }
}
KMK91KMK91
Compile Error: Variable does not exist: name at line 7 column 7
Manj_SFDCManj_SFDC
can you please paste your code here
KMK91KMK91
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;
    
           }
    }
}
Manj_SFDCManj_SFDC
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.
Mandodari RawatMandodari Rawat

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;
        }
    }
   
}
AnkehAnkeh
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
Suraj Tripathi 47Suraj Tripathi 47
Hi KMK91,

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