2

In Marketing Cloud, I have a CloudPages form that syncs data into my connected Health Cloud Org on form submission. I have a certain logic that determines if that user already exists in Health Cloud. If they already exist, then I only update 1 field (@pcplist). If they don't exist, a new Lead is created. Here is that code for updating that certain field:

/* Contact Check */

    set @rsContact = RetrieveSalesforceObjects('Contact', 'Id', 'FIRST_NAME_UPPER__c', '=', @fname, 'LAST_NAME_UPPER__c', '=', @lname, 'EMAIL_UPPER__c', '=', @email)

    if RowCount(@rsContact) > 0 then

      set @row = row(@rsContact, RowCount(@rsContact))

      set @contactId = Field(@row, 'Id')

      set @isContact = 'true'

    endif

    /* Lead Check */

    set @rsLead = RetrieveSalesforceObjects('Lead', 'Id', 'FIRST_NAME_UPPER__c', '=', @fname, 'LAST_NAME_UPPER__c', '=', @lname, 'EMAIL_UPPER__c', '=', @email)

    if RowCount(@rsLead) > 0 then

      set @row = row(@rsLead, RowCount(@rsLead))

      set @leadId = Field(@row, 'Id')

      set @isLead = 'true'

    endif 

    /* SF functions */

      if @isContact == 'true' then

        if @pcplist == 'true' then

        /* set @row = row(@rsContact, 1) */

        /* set @contactId = Field(@row, 'Id') */

        set @result = UpdateSingleSalesforceObject('Contact', @contactId, 'Primary_Care_Physician__c', 'true', 'Web_Form_Submission_Date__c', @today)

        endif

      elseif @isLead == 'true' then

        if @pcplist == 'true' then

        /* set @row = row(@rsLead, 1) */

        /* set @leadId = Field(@row, 'Id') */

        set @result = UpdateSingleSalesforceObject('Lead', @leadId, 'Primary_Care_Physician__c', 'true', 'Web_Form_Submission_Date__c', @today, 'LeadSource', 'Web Form')
        endif

      else

        set @id = CreateSalesforceObject('lead', 11, 'FirstName', @leadfirstname, 'LastName', @leadlastname, 'Email', @email,'PostalCode', @zip, 'Primary_Care_Physician__c',@pcplist,

                                   'Web_Form_Submission_Date__c', @today, 'Status', 'New', 'Web_Form_Age__c', @age, 'LeadSource', 'Web Form', 'Phone', @phone, 'Health_Wellness_Tips__c', @opt)                                 

        /*set @rsLead2 = RetrieveSalesforceObjects('Lead', 'Id, Email', 'FIRST_NAME_UPPER__c', '=', @fname, 'LAST_NAME_UPPER__c', '=', @lname, 'EMAIL_UPPER__c', '=', @email)

          if RowCount(@rsLead2) == 1 then

            set @isLead = 'true'

            set @row = row(@rsLead2, 1)

            set @leadId = Field(@row, 'Id')

            set @email = Field(@row, 'Email')

          endif*/           

          set @leadId = @Id

      endif

I tried updating the code to also update the @opt field but when I use this code, it updates BOTH the @opt and @pcplist for the existing Contact or Lead but then a duplicate Lead is created.

/* SF functions */
      if @isContact == 'true' then
        /* set @row = row(@rsContact, 1) */
        /* set @contactId = Field(@row, 'Id') */
        set @result = UpdateSingleSalesforceObject('Contact', @contactId, 'Primary_Care_Physician__c', @pcplist, 'Web_Form_Submission_Date__c', @today, 'Health_Wellness_Tips__c', @opt)
        endif
      elseif @isLead == 'true' then
        /* set @row = row(@rsLead, 1) */
        /* set @leadId = Field(@row, 'Id') */
        set @result = UpdateSingleSalesforceObject('Lead', @leadId, 'Primary_Care_Physician__c', @pcplist, 'Web_Form_Submission_Date__c', @today, 'LeadSource', 'Web Form', 'Health_Wellness_Tips__c', @opt)
        endif
      else

I don't want that duplicated Lead to be created. I want the logic to be:

  • If this record is already a Contact --> then update the @pcplist (Primary_Care_Physician__c) and @opt (Health_Wellness_Tips__c) fields based on the values in the form submission
  • If the record is already a Lead, do the same. I can't figure out what I'm missing.
2
  • So the issue is that when your UpdateSingleSalesforceObject function is executed, you do not get the Health_Wellness_Tips__c field updated with the @opt value but it works with Primary_Care_Physician__c + @pcplist, is that correct? And then the second issue is that you have a duplicated Lead created. Please, upload your full code with Lead creation logic as well, since now it seems incomplete and hard to understand.
    – Niko
    Commented Aug 21, 2021 at 20:08
  • @Niko I updated my original post with the full code. I had inherited this code from someone else when the "@opt" field did not exist on the form". Based on the existing code, if the form submission is either a Contact or a Lead, AND "@pcplist = true", then it updates the record to mark "@pcplist" as true in Salesforce. What I want to do is UPDATE both "@pcplist" AND "@opt" as true or false in Salesforce if it's an existing Contact or Lead. However, when I use the code in the 2nd box, the logic works for the field updates BUT a duplicate Lead is created. Just updates, no extra lead in the goal
    – JD15
    Commented Aug 21, 2021 at 21:08

1 Answer 1

2

In your first code snippet, you have a fully correct and working code, where you only needed to add the additional field Health_Wellness_Tips__c and value from @opt variable to both UpdateSingleSalesforceObject functions.

However, the problem hides in your second code snippet that has a different "IF" statement, where you have extra "ENDIF" (right after each of UpdateSingleSalesforceObject functions) - this leads to the interpretation of the code that each "IF" or "ELSEIF" is a separate statement and thus resolved separately as well, that how you can have all 3 paths to be executed at the same time if @isContact == 'true' and @isLead == 'true'.

What you need to do is simply remove extra "ENDIFs" and leave only one on the very end after the "ELSE" path.

You can review this documentation, where it says that you are required to have within one "IF" statement only one "ENDIF" and then optional "ELSEIF" and "ELSE" statements.

1
  • You're awesome! That worked!
    – JD15
    Commented Aug 22, 2021 at 18:28

You must log in to answer this question.

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