Salesforce Pass The Context Record Id To Flexcard

7 min read Oct 13, 2024
Salesforce Pass The Context Record Id To Flexcard

Passing the Context Record ID to Flexcards in Salesforce: A Comprehensive Guide

Flexcards are a powerful feature in Salesforce that allows you to display relevant information and actions directly on a record page. This is a great way to streamline workflows and enhance user experience. However, sometimes you need to access the context record ID within the Flexcard component to dynamically load data or trigger actions. This guide will walk you through the process of passing the context record ID to Flexcards.

Why do you need the context record ID?

The context record ID is the unique identifier of the record the user is viewing. When you embed a Flexcard on a record page, you often need to access the context record ID to perform actions specific to that record. Here are some examples:

  • Displaying related data: You might need to display related data, such as contacts, opportunities, or tasks, associated with the context record.
  • Triggering actions: You may want to trigger actions like updating a field on the context record or creating a new record based on the context record information.
  • Dynamically loading data: You might need to fetch data from a different source or API using the context record ID as a parameter.

How to Pass the Context Record ID to Flexcards

There are two primary methods to pass the context record ID to Flexcards:

1. Using the recordId property:

This method utilizes a built-in property provided by the Flexcard component itself. Here's how you can implement it:

  • Within your Flexcard component's markup:

  • Accessing the record ID in your Flexcard component's JavaScript controller:
({
    // ... your controller methods
    doSomething: function(component, event, helper) {
        var recordId = component.get("v.recordId");
        // use the recordId for your specific actions
    }
})

This approach is the simplest and most common way to access the context record ID.

2. Using the Lightning Data Service (LDS):

The Lightning Data Service offers a more robust way to access record data, including the record ID. This method is particularly useful when you need to access other fields from the context record or perform updates.

  • Define a wire function in your Flexcard component's JavaScript controller:
({
    // ... your controller methods
    @wire(getRecord, {recordId: '$recordId'})
    wiredRecord(result) {
        // Access the recordId: result.data.fields.Id.value
        // Access other fields from the record: result.data.fields.Name.value
    }
})

Example: Displaying Related Contacts

Let's illustrate how to use the recordId property to display related contacts on an account record page.

  • Create a Flexcard component:

    
    

    

    
        
            {!contact.Name}
  • Implement the JavaScript controller:
({
    fetchContacts: function(component, event, helper) {
        var recordId = component.get("v.recordId");
        var action = component.get("c.getContacts");
        action.setParams({recordId: recordId});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.contacts", response.getReturnValue());
            } else {
                // Handle errors
            }
        });
        $A.enqueueAction(action);
    },
    getContacts: function(component, event, helper) {
        var recordId = event.getParam('recordId');
        var action = component.get("c.getContactsHelper");
        action.setParams({recordId: recordId});
        return action;
    },
    getContactsHelper: function(component, event, helper) {
        // Use the recordId to retrieve related contacts
        // For this example, assume you have an Apex method
        // called 'getContacts' that fetches related contacts
        // based on the passed recordId
        var action = new ApexMethod(
            'YourClass.getContacts',
            {recordId: event.getParam('recordId')}
        );
        return action;
    }
})

This example fetches contacts associated with the current account and displays their names. You can adapt this to display other types of related data as needed.

Remember:

  • For security reasons, make sure your Apex methods or other server-side logic properly handles the record ID passed to the Flexcard to ensure data integrity and user permissions.
  • When accessing the record ID from within the Flexcard component, ensure the component has the necessary permissions to access the record data.

Conclusion:

Passing the context record ID to Flexcards is essential for creating dynamic and tailored user experiences within Salesforce. You can use either the recordId property or the Lightning Data Service to access the necessary information. By understanding the principles of Flexcards and the techniques for passing the record ID, you can create powerful and informative components that enhance your Salesforce platform.