Friday, 30 December 2022

Apex Cheat Sheet

Here is a basic Apex cheat sheet that you can use as a reference while creating your Apex class or in your Interview preparation:

// Declare a class

public class MyClass {


}


// Declare a class with a constructor

public class MyClass {

  public MyClass() {


  }

}


// Declare a class with a method

public class MyClass {

  public void myMethod() {


  }

}


// Declare a class with a property

public class MyClass {

  public Integer myProperty { get; set; }

}


// Declare a class with a static property

public class MyClass {

  public static Integer myProperty { get; set; }

}


// Declare an interface

public interface MyInterface {


}


// Declare an interface with a method

public interface MyInterface {

  void myMethod();

}


// Declare an interface with a property

public interface MyInterface {

  Integer myProperty { get; set; }

}


// Implement an interface

public class MyClass implements MyInterface {

  public void myMethod() {


  }


  public Integer myProperty { get; set; }

}


// Declare an enum

public enum MyEnum {

  VALUE1,

  VALUE2,

  VALUE3

}


// Declare a trigger

trigger MyTrigger on MyObject__c (before insert, before update) {

  // trigger logic here

}


// Declare a batch Apex class

global class MyBatchClass implements Database.Batchable<SObject> {

  global Database.QueryLocator start(Database.BatchableContext bc) {

    // query logic here

  }


  global void execute(Database.BatchableContext bc, List<SObject> scope) {

    // processing logic here

  }


  global void finish(Database.BatchableContext bc) {

    // cleanup logic here

  }

}


// Declare a scheduled Apex class

global class MyScheduledClass implements Schedulable {

  global void execute(SchedulableContext sc) {

    // scheduled logic here

  }

}


// Declare a class with a @testmethod

@isTest

private class MyTestClass {

  @isTest

  static void myTestMethod() {

    // test logic here

  }

}

I hope this helps!

No comments:

Post a Comment