0

Trying to code a form handler, and something is going awry with the RetrieveSalesforceObjects and CreateSalesforceObjects calls (I think) in my code. My parameters are posting to the page fine (I can pull out the functions and they display), but I get a 500 error when I try to load the page by itself (even with arguments added), and a 500 error when I post my form to the code resource (external HTML).

I usually just post my forms into SFMC without any issue - this is the first time I'm trying to write to Salesforce. Connector is good, scope is appropriate, user on both sides is an admin. End goal here is to evaluate from the form, based on email, if a contact exists (yes? update) - if not, if a lead exists (yes? update) - if not, create a lead.

When I test this in email (preview/test) - the lead is created. When I do this in a CP, 500 error. No inputs are being accepted when I test against on an email - are any of these functions maybe email specific?

Would welcome any of you Ampscript gurus taking a look! TIA. =Nate

    %%[

   VAR @firstName, @lastName, @phoneNumber, @emailAddress, @company, @employeeCount, @zipCode, @state, @dateModified, @industry, @CRMContactLookup, @CRMLeadLookup

   SET @firstName = RequestParameter("firstName")
   SET @lastName = RequestParameter("lastName")
   SET @phoneNumber = RequestParameter("phoneNumber")
   SET @emailAddress = RequestParameter("emailAddress")
   SET @company = RequestParameter("company")
   SET @employeeCount = RequestParameter("employeeCount")
   SET @zipCode = RequestParameter("zipCode")
   SET @state = IIF(NOT Empty(RequestParameter("state")),RequestParameter("state"),'Alaska')
   SET @dateModified = Now()
   SET @industry = RequestParameter("industry")
   SET @CRMContactLookup = RetrieveSalesforceObjects('Contact','ID','Email','=',@emailAddress)
   SET @CRMLeadLookup = RetrieveSalesforceObjects('Lead','ID','Email','=',@emailAddress)
  
        IF RowCount(@CRMContactLookup) > 0 THEN
            SET @firstCRMRow = row(@CRMContactLookup, 1)
            SET @ID = field(@firstCRMRow,'ID')
            UpdateSingleSalesforceObject('Contact',@ID,
            'FirstName',@firstName,
            'LastName',@LastName,
            'Phone',@phoneNumber,
            'Email',@emailAddress,
            'Company',@company,
            'Number_of_Employees_Range__c',@employeeCount,
            'PostalCode',@zipCode,
            'State',@state)

        ELSEIF RowCount(@CRMLeadLookup) > 0 THEN
            SET @firstCRMRow = row(@CRMLeadLookup, 1)
            SET @ID = field(@firstCRMRow,'ID')
            UpdateSingleSalesforceObject('Lead',@ID,
            'FirstName',@firstName,
            'LastName',@LastName,
            'Phone',@phoneNumber,
            'Email',@emailAddress,
            'Company',@company,
            'Number_of_Employees_Range__c',@employeeCount,
            'PostalCode',@zipCode,
            'State',@state
            'Industry',@industry)

        ELSE
            CreateSalesforceObject('Lead',9,
            'FirstName',@firstName,
            'LastName',@LastName,
            'Phone',@phoneNumber,
            'Email',@emailAddress,
            'Company',@company,
            'Number_of_Employees_Range__c',@employeeCount,
            'PostalCode',@zipCode,
            'State',@state,
            'Industry',@industry)
        ENDIF


]%%
0

As @Macca suggested, a try/catch will give you an insight as to the exact error. I tend to just use a debug flag variable and some outputs before conditionals and operations on external data. Most of the time you can narrow down the offending code or value just by stepping through.

When it comes to the troubleshooting SF Object functions, I'd start with adding/updating one attribute at a time until it breaks.

I'd also validate your incoming values vs the data types and field lengths defined on the SF Objects.

%%[

VAR @firstName, @lastName, @phoneNumber, @emailAddress, @company, @employeeCount, @zipCode, @state, @dateModified, @industry, @CRMContactLookup, @CRMLeadLookup

set @debug = 1

SET @firstName = RequestParameter("firstName")
SET @lastName = RequestParameter("lastName")
SET @phoneNumber = RequestParameter("phoneNumber")
SET @emailAddress = RequestParameter("emailAddress")
SET @company = RequestParameter("company")
SET @employeeCount = RequestParameter("employeeCount")
SET @zipCode = RequestParameter("zipCode")
SET @state = IIF(NOT Empty(RequestParameter("state")),RequestParameter("state"),'Alaska')
SET @industry = RequestParameter("industry")
SET @dateModified = Now()

if @debug == 1 then 
   output(concat("<br>@firstName: ", @firstName))
   output(concat("<br>@lastName: ", @lastName))
   output(concat("<br>@phoneNumber: ", @phoneNumber))
   output(concat("<br>@emailAddress: ", @emailAddress))
   output(concat("<br>@company: ", @company))
   output(concat("<br>@employeeCount: ", @employeeCount))
   output(concat("<br>@zipCode: ", @zipCode))
   output(concat("<br>@state: ", @state))
   output(concat("<br>@industry: ", @industry))
   output(concat("<br>@dateModified: ", @dateModified))
endif

if @debug == 0 then 

   SET @CRMContactLookup = RetrieveSalesforceObjects('Contact','ID','Email','=',@emailAddress)
   SET @CRMLeadLookup = RetrieveSalesforceObjects('Lead','ID','Email','=',@emailAddress)

   IF RowCount(@CRMContactLookup) > 0 THEN

      SET @firstCRMRow = row(@CRMContactLookup, 1)
      SET @ID = field(@firstCRMRow,'ID')

      UpdateSingleSalesforceObject('Contact',@ID,
      'FirstName',@firstName,
      'LastName',@LastName,
      'Phone',@phoneNumber,
      'Email',@emailAddress,
      'Company',@company,
      'Number_of_Employees_Range__c',@employeeCount,
      'PostalCode',@zipCode,
      'State',@state)

   ELSEIF RowCount(@CRMLeadLookup) > 0 THEN

      SET @firstCRMRow = row(@CRMLeadLookup, 1)
      SET @ID = field(@firstCRMRow,'ID')

      UpdateSingleSalesforceObject('Lead',@ID,
      'FirstName',@firstName,
      'LastName',@LastName,
      'Phone',@phoneNumber,
      'Email',@emailAddress,
      'Company',@company,
      'Number_of_Employees_Range__c',@employeeCount,
      'PostalCode',@zipCode,
      'State',@state
      'Industry',@industry)

   ELSE

      CreateSalesforceObject('Lead',9,
      'FirstName',@firstName,
      'LastName',@LastName,
      'Phone',@phoneNumber,
      'Email',@emailAddress,
      'Company',@company,
      'Number_of_Employees_Range__c',@employeeCount,
      'PostalCode',@zipCode,
      'State',@state,
      'Industry',@industry)

   ENDIF

endif

]%%
| improve this answer | |
  • Thanks @Macca and Adam - appreciate the input - I finally caught the offending missing comma near the 'State' attribute, and realized that a function coming after this block for a redirect was ultimately killing the page, debugging with your strategies. Really appreciate the assist! – Nate Brunk Sep 24 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.