0

I am trying to implement the following code but not having any luck. I want the form to only create a new Person Account record IF they don't already exist in Salesforce. Form works fine creating the records without the RetrieveSalesforceObjects section but the latter is important for obvious reasons.

Any help is appreciated.

%%[if RequestParameter("submitted") == true then ]%%


%%[
var @StudentAccounts

set @StudentAccounts = RetrieveSalesforceObjects(
"Account",
"PersonEmail,PersonContactId",
"PersonContactId","=",_subscriberKey)
if RowCount(@StudentAccounts)>0 then 
var @Email, @StudentAccount
set StudentAccount = Row(@StudentAccounts,1)
set @Email=Field(@StudentAccount,"PersonEmail")
]%%


%%[else]%%



%%[
  var @createAccount, @RecordTypeId
  set @RecordTypeId = "0121r000000q5UqAAI"
  set @createAccount = CreateSalesforceObject(
        "Account", 4,
        "FirstName", RequestParameter("firstname"),
        "LastName", RequestParameter("lastname"),
        "PersonEmail", RequestParameter("email"),
        "RecordTypeId", @RecordTypeId
       )
]%% 



%%[endif]%%


%%[
set @FirstName = RequestParameter("firstname") 
set @LastName = RequestParameter("lastname") 
set @Email = RequestParameter("email") 

set @result = InsertData("UoCTest", "EmailAddress", @Email, "Firstname", @FirstName, "Lastname", @LastName)

]%%

%%=v(@result)=%%


<!DOCTYPE html>

<html>
   <body>
      %%[ if not empty(@createAccount) then ]%%
         <p>%%=v(@createAccount)=%% record created in Account Object</p>
      %%[ endif ]%%
      <h2>Register 2</h2>
      <form action="%%=RequestParameter('PAGEURL')=%%" method="post">
         <label>First name</label>
         <input type="text" name="firstname">
         <label>Last name</label>
         <input type="text" name="lastname">
         <label>Email</label>
         <input type="text" name="email">
         <input name="submitted" type="hidden" value="true" />
         <input type="submit" value="Submit">
      </form>
   </body>
</html>

1 Answer 1

0

First issue I can see is this:

set @StudentAccounts = RetrieveSalesforceObjects("Account","PersonEmail,PersonContactId","PersonContactId","=",_subscriberKey)

You are missing some " between PersonEmail and PersonContactId.

To identify the PersonContactId you use the personalisation string _subscriberKey.

This personalisation string will be empty inside your form. You can try to use PersonEmail to identify the person inside your CRM.

There is another issue with this part of your code:

set StudentAccount = Row(@StudentAccounts,1)

Missing an @ before StudentAccount.

Last but not least, you are open two %%if%% statements but only close one.

Here is the code cleaned up.

%%[
var @StudentAccounts, @Email, @createAccount, @RecordTypeId

if RequestParameter("submitted") == true then

    set @FirstName = RequestParameter("firstname") 
    set @LastName = RequestParameter("lastname") 
    set @Email = RequestParameter("email") 

    /* Identifier needs to be changed as _subscriberKey is not given*/
    set @StudentAccounts = RetrieveSalesforceObjects("Account","PersonEmail","PersonContactId","PersonEmail","=",@Email)

    if RowCount(@StudentAccounts)>0 then 
        set @Email = Field(Row(@StudentAccounts,1),'PersonEmail')
    else
        set @RecordTypeId = "0121r000000q5UqAAI"
        set @createAccount = CreateSalesforceObject(
            "Account", 4,
            "FirstName", @FirstName,
            "LastName", @LastName,
            "PersonEmail", @Email,
            "RecordTypeId", @RecordTypeId
        )
    endif

    /* I assume this is your test?? */
    set @result = InsertData("UoCTest", "EmailAddress", @Email, "Firstname", @FirstName, "Lastname", @LastName)

    Output(@result)

endif
]%%

<!DOCTYPE html>

<html>
   <body>
      %%[ if not empty(@createAccount) then ]%%
         <p>%%=v(@createAccount)=%% record created in Account Object</p>
      %%[ endif ]%%
      <h2>Register 2</h2>
      <form action="%%=RequestParameter('PAGEURL')=%%" method="post">
         <label>First name</label>
         <input type="text" name="firstname">
         <label>Last name</label>
         <input type="text" name="lastname">
         <label>Email</label>
         <input type="text" name="email">
         <input name="submitted" type="hidden" value="true" />
         <input type="submit" value="Submit">
      </form>
   </body>
</html>

You must log in to answer this question.

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