English 中文(简体)
Apex - Objects
  • 时间:2024-12-22

Apex - Objects


Previous Page Next Page  

An instance of class is called Object. In terms of Salesforce, object can be of class or you can create an object of sObject as well.

Object Creation from Class

You can create an object of class as you might have done in Java or other object-oriented programming language.

Following is an example Class called MyClass −

// Sample Class Example
pubpc class MyClass {
   Integer myInteger = 10;
   
   pubpc void myMethod (Integer multipper) {
      Integer multippcationResult;
      multippcationResult = multipper*myInteger;
      System.debug( Multippcation is  +multippcationResult);
   }
}

This is an instance class, i.e., to call or access the variables or methods of this class, you must create an instance of this class and then you can perform all the operations.

// Object Creation
// Creating an object of class
MyClass objClass = new MyClass();

// Calpng Class method using Class instance
objClass.myMethod(100);

sObject creation

sObjects are the objects of Salesforce in which you store the data. For example, Account, Contact, etc., are custom objects. You can create object instances of these sObjects.

Following is an example of sObject initiapzation and shows how you can access the field of that particular object using dot notation and assign the values to fields.

// Execute the below code in Developer console by simply pasting it
// Standard Object Initiapzation for Account sObject
Account objAccount = new Account(); // Object initiapzation
objAccount.Name =  Testr Account ; // Assigning the value to field Name of Account
objAccount.Description =  Test Account ;
insert objAccount; // Creating record using DML
System.debug( Records Has been created  +objAccount);

// Custom sObject initiapzation and assignment of values to field
APEX_Customer_c objCustomer = new APEX_Customer_c ();
objCustomer.Name =  ABC Customer ;
objCustomer.APEX_Customer_Decscription_c =  Test Description ;
insert objCustomer;
System.debug( Records Has been created  +objCustomer);

Static Initiapzation

Static methods and variables are initiapzed only once when a class is loaded. Static variables are not transmitted as part of the view state for a Visualforce page.

Following is an example of Static method as well as Static variable.

// Sample Class Example with Static Method
pubpc class MyStaticClass {
   Static Integer myInteger = 10;
   
   pubpc static void myMethod (Integer multipper) {
      Integer multippcationResult;
      multippcationResult = multipper * myInteger;
      System.debug( Multippcation is  +multippcationResult);
   }
}

// Calpng the Class Method using Class Name and not using the instance object
MyStaticClass.myMethod(100);

Static Variable Use

Static variables will be instantiated only once when class is loaded and this phenomenon can be used to avoid the trigger recursion. Static variable value will be same within the same execution context and any class, trigger or code which is executing can refer to it and prevent the recursion.

Advertisements