0

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?

4
  • 1
    In the first line, make sure you are getting required contactID? NULL check might help. It seems you need to employ debug tricks to fix the issues. [sfmarketing.cloud/2019/08/09/debugging-ampscript/] Commented Jul 12, 2020 at 3:17
  • 1
    where is the value for @accountid coming from?
    – EazyE
    Commented Jul 13, 2020 at 2:29
  • @VijayaKapadam You were right. The try/catch script did not give any insight (maybe I did it wrong), but when I removed the CampaignMember part, I got a success page, I then printed the contactId and it turned out to be empty. I then looked back at how I was getting accountId, and figured that maybe I was not getting an id but a whole object. So I replaced the first line with: SET ATcontactId = Field(Row(RetrieveSalesforceObjects('Contact', 'Id', 'AccountId', '=', ATaccountId),1), 'Id') And now it works! (I have to write AT or my comment cannot be posted) Commented Jul 13, 2020 at 10:16
  • @EazyE Your question helped me solve the problem, thanks! I found the contactId was empty, so I looked at how I was getting accountId. When you create an object, you get the id as result. But when the account exists, the code contains a Row() and a Field() function to fill accountId with the correct id. So I put those around my contact object lookup and voila! Commented Jul 13, 2020 at 10:19

0

You must log in to answer this question.

Browse other questions tagged .