Read Values from Form and insert/update into Salesforce using Ampscript

 Read Values from Form and insert/update into Salesforce using Ampscript

In this article, we will try to understand the basics of AmpScript and insert records into Sales cloud. In our previous article, we learned how to personalize using Ampscript. Ampscript code starts with %% and ends with %%. For example, if we wanted to get the dynamic value of Email from Data extension we can use %%Email%% on the Email template or on the cloud page. In case of Cloud page if we want to write an ampScript block then it starts with %%[ and ends with ]%%.

 %%[  

/** Start of Variable Declarations **/
var @newFirstName,@newLastName,@newEmail,@newPhone ,@website,@Submit
/** End of Variable Declarations **/

/** Start of Setting Dynamic/static values to desired variables **/
set @newFirstName = RequestParameter(“Firstname”)
set @newLastName = RequestParameter(“Lastname”)
set @newEmail = RequestParameter(“email”)
set @newPhone = RequestParameter(“phone”)
set @website = ‘Home Page’
set @Submit = RequestParameter(‘submit’)
/** End of Setting Dynamic/static values to desired variables **/

/** Validate if the variable has got the desired value before inserting into Data extension **/
If @submit == ‘Submit’ Then
CreateSalesforceObject(“Contact”,3,”Email”, @emailAddress, “FirstName”, @newFirstName,”LastName”, @newLastName,”Phone”,@newPhone)

Endif

]%%

<label>Name:</label> <input name=”Firstname” type=”text” value=”%%FirstName%%” />
<label>Name:</label> <input name=”Lastname” type=”text” value=”%%LastName%%” />
<label>Email:</label> <input name=”email” type=”text” value=”%%email%%” />
<label>Phone:</label> <input name=”phone” type=”number” value=”%%phone%%” />
<input name=”submit” type=”submit” value=”Submit” />

In the above code snippet, we have started our ampscript code with %%[ and then we declared variables using var keyword. Every variable must be declared/starts with @. We have declared newName, newEmail, newPhone for demo purposes.

var @newFirstName,@newLastName,@newEmail,@newPhone,@website,@Submit

After declaring the variables the next step is to populate the variables with some values. We can set any value to the variable as it can hold text or numbers. In our scenario, we want to collect all the dynamic values such as name, email, phone and submit from the form which the customer has entered.  RequestParameter function in Ampscript helps in getting dynamic values from the form.

set @newFirstName = RequestParameter(“Firstname”)
set @newLastName = RequestParameter(“Lastname”)
set @newEmail = RequestParameter(“email”)
set @newPhone = RequestParameter(“phone”)
set @website = ‘Home Page’
set @Submit = RequestParameter(‘submit’)

After the Declaration of variables and setting/getting the values into variables, our next step is to use the variables and send them to Sales cloud/Service Cloud.

We always have to do conditional based logic to avoid unnecessary calls/errors to data extensions. To implement conditional logic we can use If condition/If else condition similar to other programming/Scripting languages. The syntax is a little different when compared to other programming/Scripting languages.

If (condition is true) Then
do this
Endif

In our scenario, we are checking if @submit variable is populated with value as submit then insert into Sales cloud/Service cloud.

If @submit == ‘Submit’ Then
CreateSalesforceObject(“Contact”,3,”Email”, @emailAddress, “FirstName”, @newFirstName,”LastName”, @newLastName,”Phone”,@newPhone)
Endif

sfdclightning

Related post