Skip to main content
Build the future with Agentforce at TDX in San Francisco or on Salesforce+ on March 5–6. Register now.
Durée estimée

Instantiate an Object and Invoke a Method from Execute Anonymous

Follow Along with Trail Together

Want to follow along with an expert as you work through this step? Take a look at this video, part of the Trail Together series.

(This clip starts at the 11:30 minute mark, in case you want to rewind and watch the beginning of the step again.)

Create a Object

Here you create a checking account object based on the BankAcct class. Technically, this is called instantiating an object: creating a specific instance of an object defined by its class. Then you invoke the makeDeposit method to run the code that adds money to the account balance.

  1. In the Developer Console, open the Execute Anonymous window. (Click Debug | Open Execute Anonymous Window.)
  2. Replace the existing code with this code:
    //From the BankAcct class, instantiate an object named chkAcct
    BankAcct chkAcct = new BankAcct();
    //Set the accttype attribute of the chkAcct object to Checking
    chkAcct.accttype = 'Checking';
    //Set the acctName attribute of the chkAcct object to D.Castillo-Chk
    chkAcct.acctName = 'D.Castillo-Chk';
    //Invoke the makeDeposit method with 150 as the argument
    chkAcct.makeDeposit(150);
    System.debug(chkAcct.getBalance());
  3. Verify that Open Log is selected, and then click Execute. The execution log opens.
  4. Select Debug Only.
  5. Review the debug messages and then close the log.

We won't check any of your work. Click Verify Step to proceed to the next step.

Partagez vos commentaires sur Trailhead dans l'aide Salesforce.

Nous aimerions connaître votre expérience avec Trailhead. Vous pouvez désormais accéder au nouveau formulaire de commentaires à tout moment depuis le site d'aide Salesforce.

En savoir plus Continuer à partager vos commentaires