Top Salesforce Interview Questions: Governor Limits, Roles, Triggers, Sandboxes, and Security

 


  1. What are Governor Limits in Salesforce?

    • Explain the concept and why it's important.

Ans- Governor Limits in Salesforce are the runtime limits enforced by Salesforce to ensure efficient use of shared resources on its multi-tenant architecture. These limits prevent any single organization (tenant) from monopolizing system resources, ensuring that all customers have reliable and consistent performance.
Why Governor Limits Exist:

Salesforce operates on a multi-tenant platform, meaning multiple organizations share the same infrastructure. To maintain system performance and stability, Salesforce enforces Governor Limits to control how resources like CPU, memory, and database queries are consumed.

Types of Governor Limits:
  1. Apex Code Execution Limits: These limits govern the execution of Apex code.

    • SOQL Query Limit:
      1. Maximum 100 SOQL queries per transaction (synchronous).

      2. Maximum 200 SOQL queries per transaction (asynchronous).

    • DML Statements:

      1. Maximum 150 DML statements per transaction.

    • CPU Time:

      1. Maximum CPU time for a transaction: 10 seconds.

    • Heap Size:

      1. Maximum heap size: 6 MB (synchronous), 12 MB (asynchronous).

    • Recursive Triggers:

      1. To prevent infinite loops, recursive triggers have limits on how many times they can call themselves.

  2. SOQL and SOSL Limits:

    • Total Records Returned by SOQL:

      1. Maximum 50,000 records returned per query.

    • SOQL Query Rows:

      1. Maximum 50,000 rows retrieved per transaction.

    • SOSL Query Rows:

      1. Maximum 2,000 records returned for SOSL queries.

  3. DML Operation Limits:

    • Total Number of DML Statements:

      1. 150 DML operations per transaction.

    • Total Number of Records Processed by DML:

      1. Maximum 10,000 records in a single DML operation.

  4. Callouts (HTTP, Web Services) Limits:

    • Total Number of Callouts:

      1. Maximum 100 callouts per transaction (external web service or HTTP requests).

    • Maximum Callout Timeout:

      1. 120 seconds per callout.

  5. Email Limits:

    • Single Email Messages:

      1. Maximum 5,000 recipients per day for single emails.

    • Mass Email:

      1. Maximum of 500 external email addresses per day for mass email (per org).

  6. Future Methods:

    • Total Number of Future Calls:

      1. Maximum 50 future method invocations per transaction.

  7. Batch Apex Limits:

    • Batch Size:

      1. Maximum of 200 records processed per batch.

    • Execution Time:

      1. Each batch can execute for a maximum of 60 seconds.

  8. Governor Limits on Other Salesforce Resources:

    • Total Number of Fields on an Object:

      1. Maximum 800 fields per custom object.

    • Total Active Workflow Rules:

      1. Maximum 2,000 active workflow rules per object.

    • Total Daily API Calls:

      1. Varies based on Salesforce edition but typically ranges from 15,000 to unlimited calls per day.

Key Considerations:
  • Per-Transaction Basis: Most governor limits are enforced per transaction, meaning each time a trigger, batch job, or Visualforce page runs, the limits are applied to that specific execution.

  • Handling Limits: Developers need to carefully write efficient code, optimize queries, and use bulk operations to stay within these limits.

  • Error Handling: If a limit is exceeded, Salesforce throws a runtime exception (such as System.LimitException), and the transaction is rolled back.

Best Practices to Avoid Hitting Governor Limits:
  1. Bulkify Code: Write Apex code that handles multiple records at a time (bulk operations), especially in triggers.

  2. Efficient SOQL Queries: Avoid putting queries inside loops and retrieve only the required fields.

  3. Use Collections: Leverage collections like List, Set, and Map to minimize SOQL queries and DML statements.

  4. Use Batch Apex: For processing large data sets, use Batch Apex to handle records in manageable chunks.

  5. Use Future Methods and Queued Apex: Offload heavy operations asynchronously to avoid synchronous limits.


Conclusion:
Governor Limits are Salesforce’s way of ensuring that no single customer can monopolize shared resources in the cloud. Developers must design applications that respect these limits by writing efficient, scalable, and optimized code, ensuring a seamless and performant user experience across all tenants.

Q2. What is the difference between a role and a profile in Salesforce?
Define roles and profiles and their usage in user management.

Ans- In Salesforce, roles and profiles are both essential components of the security model, but they serve different purposes. Here’s a breakdown of their differences:
1. Role
  • Purpose: Controls data visibility within an organization, determining what records users can see based on their hierarchy within the company.

  • Scope: Affects access to records (rows of data) within Salesforce, often through sharing rules and the role hierarchy.

  • Hierarchy: Roles are organized in a role hierarchy, allowing users higher in the hierarchy to access the records owned by users below them.

  • Record Access: Roles determine which records a user can view or edit, often based on ownership and sharing settings.

  • Use Case: If a sales manager is in a higher role than a sales representative, the manager can access the sales rep’s data, but not vice versa.

2. Profile
  1. Purpose: Controls what users can do within Salesforce, determining what objects, fields, and features they have access to.

  2. Scope: Affects access to objects, fields, tabs, apps, and permissions (CRUD operations like Create, Read, Update, Delete).

  3. No Hierarchy: Profiles are not hierarchical; every user must have exactly one profile, which defines their baseline permissions.

  4. Permissions: A profile controls:

    • Object Permissions: What standard or custom objects a user can access (e.g., Accounts, Opportunities).

    • Field-Level Security: What specific fields on an object the user can view or edit.

    • App Permissions: What apps, tabs, and page layouts the user can access.

    • System Permissions: Controls actions like running reports, exporting data, or mass emailing.

  5. Use Case: A sales representative might have a profile that grants access to view and edit Opportunities, while a marketing user’s profile might not grant access to the same objects or have different field-level security.


Q3. What is a trigger in Salesforce?
Discuss what triggers are and when to use them.

Ans- A trigger in Salesforce is a piece of Apex code that automatically executes in response to specific database events on Salesforce objects. Triggers are typically used to automate complex business processes by performing custom actions before or after data manipulation occurs, such as inserts, updates, deletes, or undeletes.
Types of Triggers:
  1. Before Trigger:

    • Executed before a record is saved to the database.

    • Commonly used to validate or modify data before it is committed to the database.

    • Example: Prevent a record from being inserted if a certain field is blank or invalid.

  2. After Trigger:

    • Executed after a record has been saved to the database.

    • Commonly used to perform actions dependent on the data being committed (e.g., creating related records, sending emails).

    • Example: Automatically create a task after an Opportunity is closed or update related records.

Events that Can Fire a Trigger:

A trigger can be fired based on the following events on Salesforce objects:

  • Before Insert

  • Before Update

  • Before Delete

  • After Insert

  • After Update

  • After Delete

  • After Undelete (used when a record is recovered from the Recycle Bin)

Syntax of a Basic Trigger:

apex


trigger TriggerName on ObjectName (before insert, before update) {

    for (ObjectName obj : Trigger.new) {

        // Logic to execute before the insert or update

    }

}


Context Variables in Triggers:
  • Trigger.new: Contains new versions of the records that are being inserted or updated.

  • Trigger.old: Contains old versions of the records before they were updated or deleted.

  • Trigger.newMap: Map of IDs to new records for update and insert operations.

  • Trigger.oldMap: Map of IDs to old records for update and delete operations.

  • Trigger.isInsert, Trigger.isUpdate, Trigger.isDelete: Boolean values that indicate which DML event fired the trigger.

  • Trigger.isBefore, Trigger.isAfter: Boolean values indicating whether the trigger is running before or after the DML operation.

Example: Before Insert Trigger

This example checks that the “Amount” field is not negative before an Opportunity record is inserted:

apex


trigger OpportunityBeforeInsert on Opportunity (before insert) {

    for (Opportunity opp : Trigger.new) {

        if (opp.Amount < 0) {

            opp.addError('Amount cannot be negative.');

        }

    }

}


Example: After Insert Trigger

This example creates a new Task after an Opportunity is inserted:

apex

trigger CreateTaskAfterOpportunity on Opportunity (after insert) {

    List<Task> tasks = new List<Task>();

    for (Opportunity opp : Trigger.new) {

        Task t = new Task(

            Subject = 'Follow up on Opportunity',

            WhatId = opp.Id,

            OwnerId = opp.OwnerId

        );

        tasks.add(t);

    }

    insert tasks;

}


When to Use Triggers:
  • Business Logic Automation: When you need to perform actions automatically in response to changes in your data, such as updating related records, sending notifications, or enforcing complex validation rules.

  • Cross-Object Field Updates: When the logic involves updating fields across different objects.

  • Record Validation: When standard validation rules are not sufficient to enforce specific business rules.

  • Complex Processes: When you need to handle complex processes that require multiple steps or involve different objects.

Best Practices for Using Triggers:
  1. Bulkify Your Code: Ensure that your trigger can handle bulk operations, such as mass updates, by using collections (e.g., List, Set) and avoiding SOQL queries or DML operations inside loops.

  2. Use Trigger Handler Classes: Move complex logic out of the trigger itself into Apex classes to keep your trigger code clean and reusable.

  3. Avoid Recursion: Prevent triggers from recursively calling themselves, which can cause governor limit issues.

  4. Limit SOQL Queries and DML Statements: Be mindful of governor limits when writing triggers, ensuring that you do not exceed Salesforce limits on SOQL queries or DML operations.

Trigger Frameworks:

In more complex scenarios, you can use or implement a trigger framework to structure and organize your triggers better, allowing for more maintainable and scalable code. Frameworks help ensure that triggers are modular, reusable, and easier to manage in large projects.


In summary, triggers in Salesforce allow developers to write custom business logic that executes automatically in response to changes in Salesforce data, offering a powerful tool to automate and enforce business processes.


Q4. What is a sandbox in Salesforce?

Explain the different types of sandboxes and their use cases.
Ans-
A sandbox in Salesforce is a copy of your production environment that you can use for development, testing, and training purposes without affecting your live production data or configuration. Sandboxes are isolated from your main production environment, allowing users to build and test changes safely before deploying them to the live environment.
Key Purposes of Sandboxes:
  1. Development: Developers use sandboxes to build and test new features, customizations, and code without risking changes to the production environment.

  2. Testing: Sandboxes are used for various types of testing, such as:

    • Unit testing: Testing individual components (like Apex classes, triggers).

    • Integration testing: Testing how different components work together.
    • User acceptance testing (UAT): Business users can validate new features before they go live.
  3. Training: Sandboxes provide a safe space for training new users without affecting live data or processes.
  4. Data Management: You can test changes to your data structure or run large data migrations in a sandbox before implementing them in production.
Types of Sandboxes in Salesforce:
Salesforce offers different types of sandboxes, each suited for different use cases:
  1. Developer Sandbox:
    • Storage: Small (200 MB of data, 200 MB of files).
    • Purpose: Used for individual development and testing tasks. It is refreshed with metadata but not production data (sample data may be inserted).
    • Refresh Interval: Once per day.
    • Use Case: Ideal for writing and testing code, building configurations, or testing small changes.
  2. Developer Pro Sandbox:
    • Storage: Larger (1 GB of data, 1 GB of files) compared to Developer Sandbox.
    • Purpose: Similar to Developer Sandbox but with more storage, making it suitable for larger development and testing projects.
    • Refresh Interval: Once per day.
    • Use Case: More complex development and integration tasks that require a larger data set.
  3. Partial Copy Sandbox:
    • Storage: Medium (5 GB of data, 5 GB of files).
    • Purpose: Includes a copy of your production organization’s metadata and a sample of data defined by a sandbox template (up to 10,000 records per object).
    • Refresh Interval: Every 5 days.
    • Use Case: Used for more comprehensive testing, including user acceptance testing (UAT), QA, and integration testing with a subset of production data.
  4. Full Sandbox:
    • Storage: Same storage as production.
    • Purpose: Contains a full copy of production metadata and data.
    • Refresh Interval: Every 29 days.
    • Use Case: Used for performance testing, load testing, staging, and final user acceptance testing. It is ideal for testing with a complete production data set to simulate real-world usage.

Differences Between Sandboxes:






Popular posts from this blog

Top Salesforce Interview Questions to Ace Your Next Job Interview

Demystifying Apex: Salesforce's Powerful Programming Language