You need to sign in to do that
Don't have an account?
Geetha Chandran 1
Marketing Cloud AMPScript Question
I am using the following AMPScript in my email template to insert a new record in to a custom object each time an email is sent. The custom object has both contact and lead lookup fields. So using the subscriber key, I first determine whether it is a lead or a contact id and then insert the new record accordingly. My problem is that duplicate records are inserted in to the custom object each time. The email is sent out using a salesforce send definition with campaign members as recipients and a SF report to exclude contacts/leads who have already received the same email. Could someone please help me understand why the following script is inserting the record twice each time an email is sent?
%%[
var @emailName, @subKey, @rsContact, @rsLead, @ContactRow, @LeadRow, @contactName, @leadName, @SFNewRec, @rowCount
Set @emailName = emailName_
set @subKey = AttributeValue("_subscriberkey")
Set @rsContact = RetrieveSalesforceObjects("Contact","Name","Id", "=", @subKey)
set @rowCount = rowcount(@rsContact)
if @rowCount > 0 then
Set @ContactRow = ROW(@rsContact,1)
Set @contactName = FIELD(@ContactRow,"Name")
IF _messagecontext == "SEND" Then
set @SFNewRec = CreateSalesforceObject("Campaign_Email_Sent__c", 2, "Contact__c", @subKey, "Email_Name__c", @emailName)
ENDIF
endif
if empty(@contactName) then
Set @rsLead = RetrieveSalesforceObjects("Lead","Name","Id", "=", @subKey)
Set @LeadRow = ROW(@rsLead,1)
Set @leadName = FIELD(@LeadRow,"Name")
endif
if not empty(@leadName) then
IF _messagecontext == "SEND" Then
set @SFNewRec = CreateSalesforceObject("Campaign_Email_Sent__c", 2, "Lead__c", @subKey, "Email_Name__c", @emailName)
ENDIF
endif
]%%
Greatly appreciate your support!
Best Regards,
Geetha
%%[
var @emailName, @subKey, @rsContact, @rsLead, @ContactRow, @LeadRow, @contactName, @leadName, @SFNewRec, @rowCount
Set @emailName = emailName_
set @subKey = AttributeValue("_subscriberkey")
Set @rsContact = RetrieveSalesforceObjects("Contact","Name","Id", "=", @subKey)
set @rowCount = rowcount(@rsContact)
if @rowCount > 0 then
Set @ContactRow = ROW(@rsContact,1)
Set @contactName = FIELD(@ContactRow,"Name")
IF _messagecontext == "SEND" Then
set @SFNewRec = CreateSalesforceObject("Campaign_Email_Sent__c", 2, "Contact__c", @subKey, "Email_Name__c", @emailName)
ENDIF
endif
if empty(@contactName) then
Set @rsLead = RetrieveSalesforceObjects("Lead","Name","Id", "=", @subKey)
Set @LeadRow = ROW(@rsLead,1)
Set @leadName = FIELD(@LeadRow,"Name")
endif
if not empty(@leadName) then
IF _messagecontext == "SEND" Then
set @SFNewRec = CreateSalesforceObject("Campaign_Email_Sent__c", 2, "Lead__c", @subKey, "Email_Name__c", @emailName)
ENDIF
endif
]%%
Greatly appreciate your support!
Best Regards,
Geetha
if @rowCount > 0
and
if not empty(@leadName)
Are probably both bringing back true.