I have a CloudPage that contains a simple form. I ask for an email address, and upon submit, the page first checks in Salesforce if a (Person)Account exists. If not, it creates one, if so, then it updates the account.
What follows is the code below, which is supposed to check if the person is already a CampaignMember for a specific campaign. If not, it should create a CampaignMember record with Status as Responded. If so, it should update the existing record with Status as Responded. But because I need the Contact ID for that and I only have the Account ID, I start by looking up the Contact ID for the Account ID.
However, I'm getting a HTTP 500 error when I submit, and no CampaignMember record is being created. My Account gets updated, I can see the timestamp update, so my guess is that something in the CampaignMember part is wrong. Seeing as my Ampscript skills are still limited, I'm afraid it could be something really simple. Can anyone see something that could be wrong?
SET @contactId = RetrieveSalesforceObjects('Contact', 'Id', 'AccountId', '=', @accountId)
SET @sfCampaignMemberLookup = RetrieveSalesforceObjects('CampaignMember', 'Id', 'CampaignId', '=', @campaignId, 'ContactId', '=', @contactId)
SET @sfCampaignMemberCount = RowCount(@sfCampaignMemberLookup)
IF @sfCampaignMemberCount == 0 THEN
SET @sfCampaignMemberResult = CreateSalesforceObject('CampaignMember', 3
, 'CampaignId', @campaignId
, 'ContactId', @contactId
, 'Status', 'Responded'
)
ENDIF
IF @sfCampaignMemberCount == 1 THEN
SET @sfCampaignMemberId = Field(@sfCampaignMemberLookup, 'Id')
SET @sfCampaignMemberResult = UpdateSingleSalesforceObject('CampaignMember', @sfCampaignMemberId
, 'Status', 'Responded'
)
Even like this, it gives the error:
SET @contactId = RetrieveSalesforceObjects('Contact', 'Id', 'AccountId', '=', @accountId)
SET @sfCampaignMemberLookup = RetrieveSalesforceObjects('CampaignMember', 'Id', 'CampaignId', '=', @campaignId, 'ContactId', '=', @contactId)
SET @sfCampaignMemberCount = RowCount(@sfCampaignMemberLookup)
IF @sfCampaignMemberCount == 0 THEN
SET @sfCampaignMemberResult = CreateSalesforceObject('CampaignMember', 2
, 'CampaignId', @campaignId
, 'ContactId', @contactId
)
ENDIF
Is it the Contact lookup? The count? The creation of the CampaignMember object? I've checked, and only CampaignId and ContactId (or LeadId) are required, correct?