Salesforce

[Hands-on Challenge] Account 계정에 이름 삽입하고 오류 있으면 잡아내기 Create a method for inserting accounts - Salesforce

송테이토 2023. 2. 10. 10:28

Create a method for inserting accounts.

Account 계정에 이름 삽입하고 오류 있으면 잡아내기

https://trailhead.salesforce.com/ko/content/learn/modules/apex_database/apex_database_dml 

To pass this challenge, 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.
  • 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

 

//Create a method for inserting accounts.

//To pass this challenge, 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.
//  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

public with sharing class AccountHandler {
    public static Account insertNewAccount(String name) {
        Account act = new Account();
        act.name=name;

        try{
            insert act;
            System.debug(act+'정상 등록');
        }catch (Exception e){
            System.debug('잘못된 형식입니다. 다시 입력해주세요.');
            return null;
        }

        return act;
    }
}