0

We are using the following AMPscript to:

  1. Retrieve the PersonContactId value from a Salesforce Account object (based on an Id value in the Sendable Data Extension)
  2. Create a new record in the Salesforce CampaignMember object

Here's the code:

%%[
var @contactId, @campaignId, @id, @contactRows, @limitRow
set @id = Id
set @campaignId = '70190000000sgS9'
set @contactRows = RetrieveSalesforceObjects('Account', 'PersonContactId','Id', '=', @id)
set @limitRow = Row(@contactRows, 1)
set @contactId= Field(@limitRow, 'PersonContactId')

CreateSalesforceObject(
   'CampaignMember', 3,
   'CampaignId',@campaignId,
   'ContactId',@contactId,
   'Status','Sent'
)
]%%

This works, but the CreateSalesforceObject() function is executed when the email is previewed in Marketing Cloud (therefore creating a record in Salesforce).

Is there a way to only execute the CreateSalesforceObject() function when the email is sent and not previewed?

1 Answer 1

5

You have to use _messagecontext personalization string SEND view to check whether email is sent or not.

%%[
var @contactId, @campaignId, @id, @contactRows, @limitRow
set @id = Id
set @campaignId = '70190000000sgS9'
set @contactRows = RetrieveSalesforceObjects('Account', 'PersonContactId','Id', '=', @id)
set @limitRow = Row(@contactRows, 1)
set @contactId= Field(@limitRow, 'PersonContactId')

IF _messagecontext == "SEND" Then
    CreateSalesforceObject(
       'CampaignMember', 3,
       'CampaignId',@campaignId,
       'ContactId',@contactId,
       'Status','Sent'
    )
EndIF   
]%%
1
  • 1
    be careful though, this would work in a test send as well, you should likely put in something like _IsTestSend == false as well to prevent adding in records during your testing phase. Commented Aug 30, 2016 at 12:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .