Appearance
Exercise 5 (Optional): Ground a Prompt with Data Cloud
In this exercise, you'll query the Reservation object in Data Cloud to get the check-in date for a guest's next stay. You'll then use that check-in date in the Welcome Email for Upcoming Trip template you created in Exercise 3.
Step 1: Create an Apex class that queries Data Cloud data
In Code Builder, open the command palette (
CMD + SHIFT + P
on Mac orCTRL + SHIFT + P
on PC).Search for Apex and click Create Apex Class.
Enter GetCheckinDate as the class name and accept the default directory.
Replace the default code with the following code:
apexpublic with sharing class GetCheckinDate { // Make this method available for grounding // the Generate_Personalized_Schedule prompt template @InvocableMethod public static List<Response> getDate(List<Request> requests) { Request input = requests[0]; Contact contact = input.Recipient; String nextCheckInDate = null; // Create input for query operation ConnectApi.CdpQueryInput queryInput = new ConnectApi.CdpQueryInput(); queryInput.sql = 'SELECT MIN(Check_in_Date__c) FROM Reservation__dlm JOIN UnifiedLinkssotIndividualCcid__dlm ON Contact_ID__c = SourceRecordId__c WHERE Check_in_Date__c >= NOW() AND UnifiedRecordId__c = (SELECT UnifiedRecordId__c FROM UnifiedLinkssotIndividualCcid__dlm WHERE SourceRecordId__c=\''+contact.Id+'\')'; // Execute SQL ConnectApi.CdpQueryOutputV2 response = ConnectApi.CdpQuery.queryAnsiSqlV2(queryInput); if (response != null && response.data != null && !response.data.isEmpty()) { ConnectApi.CdpQueryV2Row resultRow = response.data[0]; Object rowDataValue = resultRow.rowData[0]; if (rowDataValue != null && rowDataValue instanceof String) { nextCheckInDate = (String) rowDataValue; } } // Create response List<Response> responses = new List<Response>(); Response res = new Response(); res.Prompt = nextCheckInDate; responses.add(res); return responses; } // The variables in this class need to match the prompt template inputs, // that may be different on each prompt template public class Request { @InvocableVariable(required=true) public Contact Recipient; @InvocableVariable(required=true) public User Sender; } public class Response { @InvocableVariable public String Prompt; } }
Save the file (
CMD + S
on Mac orCTRL + S
on PC).In the file explorer (left sidebar), click the GetCheckinDate.cls-meta.xml file.
Update the class API version to 60.0. The file should now look like this:
xml<?xml version="1.0" encoding="UTF-8"?> <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>60.0</apiVersion> <status>Active</status> </ApexClass>
Save the file.
Right-click anywhere in the code and select SFDX: Deploy This Source to Org.
Step 2: Ground the prompt template with Data Cloud data
In Setup, open Prompt Builder.
Open the Welcome Email for Upcoming Trip template.
Replace the
Input:Recipient.Next_Check_in_Date__c
merge field with the check-in date retrieved by the Data Cloud query in your Apex class: use the Resource Search box and select Apex > GetCheckinDate.Click Save As > Save as New Version.
In the Preview panel, select Aarika Jaray as the Contact.
Click Preview.
In the Resolution panel, examine the prompt that was generated. Notice that the Apex merge field in the template has been replaced with the check-in date for the guest's next stay obtained by the Invocable Apex method.
In the Response panel, examine the message generated by the LLM including the actual check-in date.