Friday, March 28, 2025

Discover Integration Connectors in Salesforce

Integration connectors play a crucial role in helping different systems and applications communicate with each other. Let’s take a look at a few key integration connectors, focusing on MuleSoft and Salesforce connectors, which enable seamless data transfer and efficient connections between platforms.

MuleSoft Salesforce Connector: The MuleSoft Salesforce Connector allows Mule applications to integrate with Salesforce and other systems, such as Workday. This connector simplifies data transfer by acting as a bridge between different systems. For example, when Salesforce sends data to the Anypoint connector, the Mule application processes the data and sends it to the destination API (like Workday). This setup eliminates the need for direct, point-to-point integrations, ensuring that systems remain decoupled and flexible. While pre-built integrations for specific combinations (like Salesforce to Workday) may not always be available, finding connectors is easy through platforms like AppExchange or marketplace applications like Workday.

Heroku Connect: Heroku Connect is another useful connector that links Salesforce instances with Heroku Postgres databases. Using various APIs (SOAP, Bulk, and Streaming), it facilitates seamless data synchronization between Salesforce and Heroku, enhancing the ability to work with both platforms simultaneously.

Salesforce Connect: Salesforce Connect enables real-time data access from external systems without storing the data in Salesforce. This is done by creating external objects, which are temporary connections that pull in data from other platforms. Salesforce Connect supports various adapters, including:

  • OData Adapters (2.0 and 4.0): These connect to data from OData producers, a protocol for sharing data over REST.
  • Cross-Org Adapter: Allows connections to other Salesforce organizations directly using the standard REST API.
  • Custom Adapter: If the other options don't fit, you can build your own adapter using Apex (Salesforce’s programming language) for more specialized needs.

In summary, these connectors offer flexible and efficient ways to link Salesforce with other platforms, ensuring smooth data exchange and improving overall system functionality. Whether it’s through MuleSoft’s powerful integration platform or Salesforce’s native tools, integration connectors streamline business processes and data flow.

Friday, March 21, 2025

Interview Questions on Salesforce Apex Part-2

 1) Let's say a user does not have permission on a child object but has permission on the parent object to read, create, edit, and delete the parent record. If I create a trigger on the parent object to insert a child record after the parent record is created from an Apex class (which does not have any keywords declared, like "with sharing" or "without sharing," nor does it have WITH SECURITY_ENFORCED), will a child record be created after the user inserts the parent record manually?

Yes, a child record will be created. By default, triggers and Apex classes run in system mode, which means they bypass the user's permissions and can insert the child record even if the user does not have access to the child object.

2) If, in the above scenario, from the trigger, I am calling an Apex class that is in "with sharing" mode and inserting a child record after the parent is manually inserted by the user, will a child record be created?

Yes, a child record will be created. The with sharing keyword only enforces the sharing rules for the user, but it does not affect object permissions. Since the trigger runs in system mode and bypasses the user's permissions, it will still create the child record. The with sharing keyword primarily governs the visibility of records based on sharing rules, not user permissions for specific objects.

3) Let’s say an Apex class, which is not declared with any keywords such as "with sharing" or "without sharing," is fetching Opportunity records. The user running under whose context this apex class is invoked does not own any Opportunity records, nor are any Opportunity records shared with them. In this case, will the class be able to fetch Opportunity records?

Yes, the class will be able to fetch Opportunity records. Since the Apex class runs in system mode by default (when neither the with sharing nor without sharing keywords are specified), it bypasses the user's record-level access and CRUD permissions. As a result, the class can retrieve all Opportunity records, regardless of the user's permissions or whether the records are shared with them.

4) Let’s say an Apex class, which is declared with the "with sharing" keyword, is fetching Opportunity records. The user, under whose context this Apex class is invoked, does not own any Opportunity records, nor are any Opportunity records shared with them. In this case, will the class be able to fetch Opportunity records?

No, the class will not be able to fetch Opportunity records. The with sharing keyword enforces sharing rules, meaning that only records the user has access to (based on sharing rules, ownership, or explicit sharing) will be retrieved. Since the user does not own any Opportunity records, nor are any records shared with them, the class will not be able to access those records.

5) What is bulkifying code in Salesforce Apex, and why is it important?

Bulkifying code means writing code that can handle multiple records at once instead of processing just one record. It is essential because Salesforce has governor limits on the number of records that can be processed at a time. Bulkifying your code ensures it can efficiently handle large data sets while staying within those limits.

6) Why should you avoid SOQL queries and DML operations inside FOR loops?

Placing SOQL queries or DML operations inside FOR loops can quickly exhaust governor limits, leading to runtime exceptions. To avoid hitting these limits, you should perform queries and DML operations outside of loops, processing records in bulk instead.

7) Why should you avoid SOQL queries and DML operations inside FOR loops?

Placing SOQL queries or DML operations inside FOR loops can quickly exhaust governor limits, leading to runtime exceptions. To avoid hitting these limits, you should perform queries and DML operations outside of loops, processing records in bulk instead.

// Bad practice: SOQL inside FOR loop

for (Contact con : Trigger.new) {

    Contact result = [SELECT Id, Name FROM Contact WHERE Id = :con.Id LIMIT 1];

}

// Good practice: Perform SOQL outside the loop

Set<Id> contactIds = new Set<Id>();

for (Contact con : Trigger.new) {

    contactIds.add(con.Id);

}

List<Contact> contacts = [SELECT Id, Name FROM Contact WHERE Id IN :contactIds];

8) What is the purpose of using @future methods in Apex, and what should be considered when using them?

@future methods are used for asynchronous processing in Apex, allowing operations to be performed in the background. They help offload long-running tasks and improve the user experience. However, @future methods cannot accept sObjects or objects as parameters and have a limit of 50 invocations per transaction.

trigger accountAsyncTrigger on Account (after insert, after update) {

    someClass.processAccount(Trigger.newMap.keySet());

}

global class someClass {

    @future

    public static void processAccount(Set<Id> accountIds) {

        // Code to process accounts asynchronously

    }

}

9) Why should you avoid hardcoding IDs in Apex code, and how can you handle record types or other dynamic values instead?

Hardcoding IDs in Apex code makes it less flexible and prone to errors when moving between environments (like from sandbox to production). Instead, use dynamic methods such as querying for IDs based on the label or record type, ensuring the code works across different environments.

List<RecordType> rtypes = [SELECT Name, Id FROM RecordType WHERE sObjectType='Contact' AND IsActive=true];

Map<String, String> contactRecordTypes = new Map<String, String>();

for (RecordType rt : rtypes) {

    contactRecordTypes.put(rt.Name, rt.Id);

}

for (Contact a : Trigger.new) {

    if (a.RecordTypeId == contactRecordTypes.get('SomeRecordTypeName')) {

        // Your logic here

    }

}

Friday, March 14, 2025

Interview Questions on Salesforce Apex Part-1

 1) How do SOQL List and SOQL For Loop differ in terms of memory handling?

SOQL List loads all queried records into memory at once, which can quickly exceed the heap size limit if too many records are queried.

SOQL For Loop fetches records one at a time in smaller chunks, making it more efficient and reducing the risk of hitting the heap size limit.

2) What are the potential issues when querying large datasets using SOQL List?

Querying large datasets with SOQL List may result in heap size limit errors if the total size of the data exceeds 6 MB for synchronous operations. To avoid this, you should use SOQL For Loop, which processes records in smaller chunks.

3) How would querying parent-child data affect the use of SOQL For Loop?

When querying parent-child data in Salesforce, if there are more than 200 child records in a relationship, an error like "Aggregate query has too many rows for direct assignment" might occur. This can be avoided by using a nested loop structure.

Incorrect approach (will throw error if there are more than 200 child records):

for(Account obj : [SELECT id, (SELECT id, name FROM contacts) FROM account LIMIT 1]) {

    list<Contact> conList = obj.contacts;  // Error if more than 200 contacts

}

Correct approach (avoids error by iterating over child records inside the loop):

for(Account obj : [SELECT id, (SELECT id, name FROM contacts) FROM account LIMIT 1]) {

    for(Contact con : obj.contacts) {

        // Process each contact record

    }

}

4) Can you explain the heap size limit error with an example?

Let's say you're querying 50,000 records from the Contact object, and each record takes up 2 KB. The total memory required would be 50,000 * 2 KB = 100 MB. Since the maximum heap size limit for synchronous operations is 6 MB, this would exceed the limit, causing a heap size error. This is why SOQL For Loop should be used, as it processes each record individually.

5) Why are Governor Limits enforced in Apex?

Governor Limits are enforced in Apex to ensure that code runs efficiently in Salesforce's multitenant environment. They prevent one tenant from consuming too many resources, which could negatively impact others sharing the same instance.

7) What is the maximum number of records that can be returned by a SOQL query in a synchronous transaction?

A synchronous SOQL query can return a maximum of 50,000 records. If this limit is exceeded, a runtime exception will occur.

8) How many DML statements can be executed in a single transaction?

You can execute up to 150 DML statements in a single transaction, whether it's a synchronous or asynchronous operation.

9) What is the maximum heap size for synchronous operations in Apex?

The maximum heap size for synchronous operations in Apex is 6 MB. If the heap size exceeds this limit, an exception will occur.

10) How many future methods can be invoked in a single Apex transaction?

A maximum of 50 future methods can be invoked per Apex transaction, but for batch/future operations, only 50 queueable jobs can be invoked.

Friday, March 7, 2025

Master Interview questions on apex triggers in salesforce Part-3

 13) What are the the context variable available with before insert event?

Only Trigger.new is available.

Tags: #Interview questions on triggers in salesforce with answers, #Scenario based Interview Questions on triggers in Salesforce

14) What are the the context variable available with after insert event?

Trigger.new and Trigger.newMap.

15) What are the the context variable available with before update event?

Trigger.new, Trigger.old, Trigger.newmap and Trigger.oldmap

16) What are the the context variable available with after update event?

Trigger.new, Trigger.old, Trigger.newmap and Trigger.oldmap

17) What are the the context variable available with before delete event?

Trigger.old and Trigger.oldMap.

18) What are the the context variable available with after delete event?

Trigger.old and Trigger.oldMap.

Tags: #Interview questions on triggers in salesforce with answers, #Scenario based Interview Questions on triggers in Salesforce

19) What are the the context variable available with after undelete event?

Trigger.new and Trigger.newMap.

Friday, February 28, 2025

Master Interview questions on apex triggers in salesforce Part-2

 7) How many trigger we should write on one object?

 We should always follow the best practice and consider writing one trigger per object. If we have more than one trigger per object on same event say before insert than we cannot guarantee the order of execution.  

8) What is the difference between database.insert and insert?

 Using insert if one record fails entire operation is stopped and none of the record is inserted into database, whereas with databse.insert partial insertion is supported.

9) Is it possible to call batch class from trigger.

Yes.

10) What is the condition to call a method from trigger which is making callout?

The callout should be asynchronous.

11) What will you do if a method inside apex class need to be executed only when it is getting called from trigger?

We will use trigger.isexecuting in apex class to check if the method inside apex class is getting called from trigger and will execute the method if getting called from trigger.

12) What is "Trigger Handler Framework Salesforce"?

Refer article: Trigger Handler Framework Salesforce