Skip to content

Instantly share code, notes, and snippets.

@olgaloza
Last active October 28, 2024 15:44
Show Gist options
  • Save olgaloza/8c4c069f18ae4b23ce58fc6b4c4f66f0 to your computer and use it in GitHub Desktop.
Save olgaloza/8c4c069f18ae4b23ce58fc6b4c4f66f0 to your computer and use it in GitHub Desktop.
Manipulating Records with DML
public class AccountHandler {
/* From "Manipulate Records with DML" Trailhead:
* https://trailhead.salesforce.com/modules/apex_database/units/apex_database_dml
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
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 static Account insertNewAccount(String s) {
Account acct = new Account(Name = s);
try {
insert acct;
ID acctID = acct.ID;
System.debug('new record ID is: ' + acctID);
return acct;
} catch (DmlException e) {
System.debug('A DML exception has occurred: ' + e.getMessage());
return null;
}
}
}
Copy link

ghost commented Mar 23, 2022

Did it work for you? id didnt work for me.

@wcastro1
Copy link

This did work for me.

@SpinMari
Copy link

SpinMari commented Oct 28, 2024

You need to insert it into the AccountHandler class, maybe you got confused because of the comments inside?

`
public class AccountHandler {

public static Account insertNewAccount(String s) {
    Account acct = new Account(Name = s);
    try {
        insert acct;
        ID acctID = acct.ID;
        System.debug('new record ID is: ' + acctID);
        return acct;
    } catch (DmlException e) {
        System.debug('A DML exception has occurred: ' + e.getMessage());
        return null;
    } 
        
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment