0

-Created a landing page to create records for newsletter subscription

  • To fill a form there is a Full name & Email Field, once the user submits the form it will check whether the user with this email exists or not in any of the contact records of the Sales Cloud
  • If the user does not exist in CRM then it will 1st create a record in Newsletter DE in SFMC, then will make records of the Account & their related Contact objects in SFDC
  • if the user already exists then it will just do an upsert operation in Newsletter DE
  • if the user does not put the last name in the Full Name field then it will assume "Unknown" as a Last Name to save records.

so the issue is when the contact record is not present in the sales cloud then shows an error when trying to update the record in Newslater Data Extension.

so how can do it when someone fills out the newsletter form if that record presents DE as well as sales cloud then it will update in both if that record is present DE and not in sales then how can create it?

%%[
var @Name, @firstName, @lastName, @email, @retrieveContact, @accRecordTypeId, @conRecordTypeId, @accSource, @unknown,  @createContact, @createAccount 

set @unknown = "Unknown"
set @Name = RequestParameter('name')
set @email = RequestParameter('email')
set @accRecordTypeId= "0128b000000xlP8AAI"
set @conRecordTypeId= "0128b000000lC9lAAE"
set @accSource= "Newsletter"
Set @attendeeSyncInput ="True"

SET @retrieveContact = RetrieveSalesforceObjects('Contact','Id, Email','Email', '=',@email) ]%%


%%[if RowCount(@retrieveContact) >= 1 AND RequestParameter("submitted") == true then 

if indexOf(@Name," ") > 0 then

  set @firstName = Substring(@Name,1, Subtract(IndexOf(@Name," "),1))
  set @lastName = Substring(@Name,Add(indexOf(@Name," "),1))

 upsertDE("Newsletter",1,"Email",@email,"First Name", @firstName, "Last Name", @lastName)


else

upsertDE("Newsletter",1,"Email",@email,"First Name", @Name, "Last Name",@unknown)

endif

 endif ]%%

%%[if RowCount(@retrieveContact) == 0 AND RequestParameter("submitted") == true then

  if indexOf(@Name," ") > 0 then
  set @firstName = Substring(@Name,1, Subtract(IndexOf(@Name," "),1))
  set @lastName = Substring(@Name,Add(indexOf(@Name," "),1))

  InsertDE(
    "Newsletter",
    "First Name",@firstName,
     "Last Name",@lastName,
     "Email",@email
     )

  set @createAccount = CreateSalesforceObject(
        "Account",3,
        "RecordTypeId",@accRecordTypeId,
        "Name", Concat(@firstName," ",@lastName),
        "AccountSource",@accSource

       )
      if not empty(@createAccount) then
      set @createContact = CreateSalesforceObject(
        "Contact",7,
        "RecordTypeId",@conRecordTypeId,
        "AccountId", @createAccount,
        "FirstName", @firstName,
        "LastName", @lastName,
        "Email", @email,
        "LeadSource",@accSource,
        "Attendee_Sync_Input__c", @attendeeSyncInput
       )
       endif

      Else

      InsertDE(
    "Newsletter",
    "First Name",@Name,
     "Last Name",@unknown,
     "Email",@email
     )

      set @createAccount = CreateSalesforceObject(
        "Account",3,
        "RecordTypeId",@accRecordTypeId,
        "Name", Concat(@Name," ",@unknown),
        "AccountSource",@accSource

       )
      if not empty(@createAccount) then
      set @createContact = CreateSalesforceObject(
        "Contact",7,
        "RecordTypeId",@conRecordTypeId,
        "AccountId", @createAccount,
        "FirstName", @Name,
        "LastName", @unknown,
        "Email", @email,
        "LeadSource",@accSource,
        "Attendee_Sync_Input__c", @attendeeSyncInput
       )
      endif

  endif 

endif]%%

%%[ if not empty(@createContact) AND not empty(@createAccount) then ]%%
 Thanks for subscribed to the Newsletter 
%%[ endif ]%% Full Name

Email

enter image description here

1 Answer 1

0

This is too long for a reply. It's not a correct answer.

  • isolate and trim down your code for us to help you. there are a lot of potential issues.

  • a lot of double code can be streamlined by better use of variables. example at the bottom. Don't copy the same function all over the place if it can be avoided, it makes debugging even harder.

  • use proper indentation

  • code defensively, You are not empty checking name /email before running functions on it, or writing it into data extension (which we cannot see, and whose field limits might be part of the problem).

  • Get into SSJS. Pure Ampscript will run into some constraints, especially around debugging. Examples:

    • You are not using try / catch around (especially) the SF functions. ( How to try catch RetrieveSalesforceObjects in AMPScript ); This is one of the bigger issues as also SF records have limits that are obscured by 500 errors, but visible in the catch messages.

    • you are creating an account and IMMEDIATELY retrieving the created record, which can fail if it takes a couple milliseconds for the created record to be available. A workaround is using an SSJS sleep function between the two create calls. example with 500 ms wait time below.

<script type="javascript" runat="server>
Platform.Load("Core",1)
// wait for x milliseconds and retrieve the created account to get created Id
var start = new Date().getTime();
for (var i=0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > 500) {
       break;
    }
}
</script>

structural example with some comments

%%[
SET @unknown = "Unknown"
SET @Name = RequestParameter('name')
SET @email = RequestParameter('email')

/* insert an empty check */
IF empty(@name) or empty(@email) THEN
  SET @error = "true"
ENDIF 

/* dont bother executing code if vars are missing */ 

IF @error != "true" THEN

  IF indexOf(@Name," ") > 0 THEN
    SET @firstName = "" /*manipulate it to whatever*/
    SET @lastName = ""/* manipulate it to whatever*/
  ELSE
    SET @firstName = "" /*manipulate it to something else */
    SET @lastName = ""/* manipulate it to something else */
  ENDIF
  /* use the manipulated @firstname and @lastname vars below, save yourself "copies" of all functions. */ 


  SET @accRecordTypeId= "0128b000000xlP8AAI"
  SET @conRecordTypeId= "0128b000000lC9lAAE"
  SET @accSource= "Newsletter"
  SET @attendeeSyncInput ="True" 
  SET @submitted = RequestParameter("submitted") /* put this to the top*/

  SET @retrieveContact = RetrieveSalesforceObjects('Contact'
    ,'Id, Email'
    ,'Email', '=',@email
  ) 

  IF RowCount(@retrieveContact) >= 1 and @submitted == true then 
    
  ELSE /* avoid the repetition of rowcount and submitted check - this is a simple ELSE statement */ 

  ENDIF

ELSE ]%%

    mandatory params missing

%%[ENDIF ]%%

You must log in to answer this question.

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