Salesforce Opportunity Attachment Insights using AI Agent > Introduction to Salesforce Opportunity Attachment Insights using AI Agent recipe > Prerequisites
  

Prerequisites

To create a Salesforce Opportunity Attachment Insights using AI Agent, the following prerequisites must be met:

Configuring Salesforce Change Data Capture

Configure Salesforce Change Data Capture (CDC) to send a message when the Opportunity object changes.
  1. 1Log in to the Salesforce account.
  2. 2Go to Setup > Build > Develop > Change Data Capture.
  3. 3From the Available Entities list, select the Opportunity (Opportunity) object and add it to the Selected Entities list.
  4. The following image shows the Change Data Capture page:
  5. 4Click Save.

Creating a custom field in the Salesforce Opportunity entity

To create a custom field in the Salesforce Opportunity entity, perform the following steps:
  1. 1Log in to the Salesforce organization.
  2. 2Go to Setup > Build > Customize > Opportunities > Fields, and then click New.
  3. The following image shows the New button on the Opportunities Fields page:
  4. 3In the Field Label field, enter File ID, and in the Field Name field, enter File_ID. Click Next and save the custom field.
  5. You must create a custom field with the Text data type.
    The following image shows the New Custom Field page:

Configuring Salesforce Apex Triggers

Configure Salesforce Apex Triggers to update the Opportunity when an attachment is uploaded.
  1. 1Log in to the Salesforce organization.
  2. 2Go to Setup > Build > Develop > Apex Triggers > Developer Console.
  3. The following image shows the Apex Triggers page:
  4. 3In the Developer Console, go to File > New > Apex Trigger as shown in the following image:
  5. 4Enter the trigger name, select Opportunity from the sObject list, and then click Submit.
  6. 5Enter the following code snippet and save the trigger.
  7. trigger {trigger name} on Attachment (after insert) {
    Map<Id, String> oppUpdates = new Map<Id, String>();

    for (Attachment att : Trigger.new) {
    if (String.valueOf(att.ParentId).startsWith('006')) {
    oppUpdates.put(att.ParentId, att.Id);
    }
    }

    if (!oppUpdates.isEmpty()) {
    List<Opportunity> oppToUpdate = [SELECT Id, Name FROM Opportunity WHERE Id IN :oppUpdates.keySet()];
    for (Opportunity c : oppToUpdate) {
    c.File_ID__c= oppUpdates.get(c.Id);
    }
    update oppToUpdate;
    }
    }