Saving attachments with a Lightning web component#
If your organization uses a custom Lightning web component (LWC) to save emails or events from Context Panel, you can also enable attachment saving as part of the same flow.
With this setup, your component can:
receive attachment metadata from Context Panel before saving, so users can review which files will be saved
instruct Context Panel to upload and link selected attachments after the component finishes creating Salesforce records
This article explains how the flow works and how to configure your component.
Before you begin#
Ensure that the following requirements are met before configuring attachment saving:
The Lightning web component (LWC) is integrated with Context Panel and configured as the save experience instead of the standard Context Panel dialog.
Lightning component field auto-population is configured. This is required for Context Panel to pass attachment data to the component.
How attachment saving works#
The attachment flow consists of two stages.
Stage 1. Context Panel passes attachment metadata to the component#
When Context Panel opens your LWC for an email or event that contains attachments, it passes attachment metadata to the component through the attachments property.
Your component can use this data to show users which files are available before they complete the save operation. The property contains metadata only, such as attachment ID, file name, and file size. The component does not receive file content.
Stage 2. The component tells Context Panel which attachments to save#
After the user completes the save action in your component, the component dispatches the rg:finish event.
To trigger attachment uploading, include the linkedAttachments parameter in the event payload. This parameter tells Context Panel:
which Salesforce records should receive the files
which mailbox attachments should be uploaded and linked
After Context Panel receives the event, it uploads the specified attachments and links them to the specified Salesforce records.
The upload starts after the component finishes its own save logic. This keeps the component responsible for creating Salesforce records, while Context Panel remains responsible for retrieving and uploading attachment files from the mailbox context.
Configure attachment saving#
To enable attachment saving in your LWC:
Add the
attachmentsproperty to the component.Display attachment details in the UI if needed.
Include
linkedAttachmentsin therg:finishevent payload after the save completes.
Add the attachments property#
To let Context Panel pass attachment details into your component, declare attachments as a public property.
JavaScript example#
// itemProperties.js
import { LightningElement, api } from 'lwc';
export default class ItemProperties extends LightningElement {
@api attachments;
}
attachments property#
The attachments property is an array. Each item contains metadata for one attachment.
Example:
{
attachments: [
{
Id: "AAMkA...QAas=",
Name: "Proposal.pdf",
Size: 2048
},
{
Id: "AAMkA...QAbb=",
Name: "Pricing.xlsx",
Size: 8192
}
]
}
Nested properties:
Property |
Type |
Description |
|---|---|---|
|
String |
The unique identifier of the attachment. Typically corresponds to the Outlook attachment ID. |
|
String |
The file name displayed in the UI. |
|
Number |
The size of the attachment in bytes. |
For the complete list of properties that Context Panel can pass to an LWC, see the article Lightning web component integration properties.
Display attachment details in the component#
Displaying attachments is optional, but it is recommended when users need to confirm which files will be saved.
HTML example#
<template>
<div>
<p><b>Attachments:</b></p>
<template if:true={attachments}>
<ul>
<template for:each={attachments} for:item="attachment">
<li key={attachment.Id}>{attachment.Name} ({attachment.Size} bytes)</li>
</template>
</ul>
</template>
</div>
</template>
This example uses the attachment metadata for display only. The actual upload happens later, after your component dispatches rg:finish.
Dispatch rg:finish event with linkedAttachments#
When your component finishes creating the target Salesforce record, dispatch the rg:finish event and include the linkedAttachments parameter.
linkedAttachments parameter#
linkedAttachments is an object that defines the attachments to upload and the Salesforce records to link them to.
Property |
Type |
Description |
|---|---|---|
|
String[] |
Salesforce record IDs to link attachments to |
|
String[] |
Attachment IDs from the |
Each attachment listed in AttachmentIds is linked to each record listed in ParentIds.
For example, if you provide two attachment IDs and one parent record ID, both files are uploaded and linked to that record.
Step 1. Define event parameters#
Specify the Salesforce record IDs and attachment IDs.
var finishEventParams= {
linkedAttachments: {
ParentIds: ["02sKa00000W10nIIAR"],
AttachmentIds: ["AAMkA...QAas="]
}
};
ParentIds– Salesforce record IDs that should receive the attachmentsAttachmentIds– Attachment IDs from theattachmentsproperty
Step 2. Create the event#
Create a CustomEvent with the name rg:finish and pass the parameters in the detail object.
var finishEvent=newCustomEvent("rg:finish", {
detail:finishEventParams
});
Important
Do not change the rg:finish event name. Context Panel listens for this exact event, and modifying it will prevent the feature from working.
Step 3. Dispatch the event#
Dispatch the event after the save operation is completed.
this.dispatchEvent(finishEvent);
Once dispatched, Context Panel uploads the specified attachments and links them to the provided Salesforce records.
Important implementation notes#
Do not rename the event
Use the exact event name
rg:finish. Context Panel listens for this exact name. If you change it, Context Panel will not process the event.Dispatch
rg:finishonly after the save succeedsIf record creation fails, do not dispatch
rg:finish, because Context Panel would receive incomplete or incorrect linking instructions.Use valid Salesforce record IDs in
ParentIdslinkedAttachments.ParentIdsmust contain valid Salesforce record IDs for records that already exist by the time the event is dispatched.
(optional) Combine attachment saving with other Context Panel actions#
The rg:finish event supports additional parameters besides linkedAttachments. You can combine them in the same event payload to control the Context Panel behavior after the save completes.
Common examples include:
notificationMessageto show a custom success messagecreatedActivityIdsorcreatedObjectIdsto enable the View it now actionreloadActivitiesto refresh the activity state in the Context PanelreloadResolvedObjectsto refresh resolved objectssetCategoryto assign the configured Outlook category to the saved email or event
Example
var finishEventParams = {
notificationMessage: 'Email and attachments were saved to Salesforce.',
createdActivityIds: ['02sXXXXXXXXXXXXXXX'],
reloadActivities: true,
linkedAttachments: {
ParentIds: ['02sXXXXXXXXXXXXXXX'],
AttachmentIds: ['AAMkA...QAas=']
}
};
For the complete list of rg:finish parameters, see the article rg:finish event parameters.
Deployment#
After updating the component, deploy it to your Salesforce organization.