function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Geetha Chandran 1Geetha 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
Omoyemi JohnsonOmoyemi Johnson
looking at it it would seem that your two if statements to add a record are not mutually exclusive.
if @rowCount > 0
and
if not empty(@leadName)

Are probably both bringing back true.