Showing posts with label #SFDC #Apex #VineetYadav. Show all posts
Showing posts with label #SFDC #Apex #VineetYadav. Show all posts

Wednesday, 4 January 2023

How do you use Lightning Aura components to interact with Apex classes?

To use Lightning Aura components to interact with Apex classes, you can use the @AuraEnabled annotation in your Apex class to expose the class and its methods to Lightning Aura components. You can then use the Lightning Aura component's JavaScript controller to call the Apex class method using the Lightning Data Service (LDS) or the Lightning Component Library (LCL).

Here is an example of an Apex class with an @AuraEnabled method:

public with sharing class MyApexClass {

    @AuraEnabled

    public static String getMessage() {

        return 'Hello from Apex';

    }

}

And here is an example of a Lightning Aura component that calls the Apex class method using LDS:

<!-- myComponent.cmp -->

<aura:component>

    <aura:attribute name="message" type="String" />

    <lightning:button label="Get Message" onclick="{! c.getMessage }" />

    <br />

    <p>{! v.message }</p>

</aura:component>


// myComponentController.js

({

    getMessage: function(component, event, helper) {

        // Call the Apex class method

        var action = component.get('c.getMessage');


        // Set the callback function

        action.setCallback(this, function(response) {

            var state = response.getState();

            if (state === 'SUCCESS') {

                // Set the component attribute with the returned value

                component.set('v.message', response.getReturnValue());

            } else {

                console.log('Error: ' + state);

            }

        });


        // Send the request to the server

        $A.enqueueAction(action);

    }

})

I hope this helps! 

Order of execution In Salesforce

In Salesforce, the order of execution is the order in which the system processes various events and actions. It's important to understand the order of execution in Salesforce because it determines the order in which certain events occur, and can affect how your code behaves. Here is a summary of the order of execution in Salesforce:

  • Loads the original record from the database or initializes the record for an insert operation.
  • Executes all before triggers.
  • Validates the record to ensure that it meets all validation rules.
  • Executes all after triggers.
  • Saves the record to the database, but doesn't commit yet.
  • Executes assignment rules.
  • Executes auto-response rules.
  • Executes workflow rules.
  • If there are workflow field updates, updates the record again.
  • If the record was updated with workflow field updates, fires before and after update triggers one more time (and only one more time), in addition to standard validations.
  • Commits the record to the database.
  • Executes post-commit logic, such as sending email.

    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!