ampscript

Inserting Data into Salesforce Data Extensions Using Cloud Pages and If-Else Conditional Statement in AMPscript

This blog brings you an interesting topic: How to save data into Salesforce data extensions via cloud pages and using If-Else statements.

As you must have an idea of what is a conditional statement and what it does. If not, what it does is if a condition gets satisfied code gets executed inside the If statement and if not then else code gets executed.

The requirement is:

I want to save data into Salesforce Data extensions but there is a condition that data must get inserted into two different extensions on the basis of any criteria.

So I decided to use the If-Else conditional statement in AMPscript and Insert function to insert data into different data extensions

dont miss out iconDon't forget to check out: Salesforce Marketing Cloud: AmpScript Basics

Below is the whole process of how I achieve this:

1. First, I created a cloud page having a form with several fields to save data of the user.

Under the Web Studio > Cloud Pages > Create a Collection > Create a Landing page.

AMPscript which I use for this is: 

%%[
IF RequestParameter("submitted")==true THEN
       Var @insert
       SET @firstname = RequestParameter("firstname")
       SET @lastname = RequestParameter("lastname")
       SET @phone = RequestParameter("phone")
       SET @email = RequestParameter("email")
       SET @rating = RequestParameter("rating")
       SET @description = RequestParameter("description")
       SET @status = RequestParameter("status")            
]%%
 %%[ ELSE ]%%
<table style="padding: 20px;"><tr><td>
     <h2>Please Fill the form:</h2>
      <form action="%%=RequestParameter('PAGEURL')=%%" method="post">   
       <label>First Name: </label><input type="text" name="firstname" required="false"><br>
         <label>Last Name: </label><input type="text" name="lastname" required="false"><br>
         <label>Email: </label><input type="email" name="email" required="true"><br>
         <label>Phone: </label><input type="phone" name="phone" required="false"><br>
        <label>Description: </label><input type="text" name="description" required="false"><br>
              <label>Status: </label><input type="text" name="status" required="false"><br>
        <label>Rating: </label><input type="number" name="rating" required="false"><br>
         <input name="submitted" type="hidden" value="true"><br>
         <input type="submit" value="Submit">
      </form>  
</td></tr></table>
%%[ ENDIF ]%%

2. This code gives us an idea of how our form will look like but as you will publish the cloud page and click on the submit button nothing will happen as we are not using any function to insert the data.

Clicking on submit is only setting the values as of now.

3. Now I use the Insert Data function to insert data into the Salesforce data extension. Check out the code given below.

%%[
IF RequestParameter("submitted")==true THEN
       Var @insert       
       SET @firstname = RequestParameter("firstname")
       SET @lastname = RequestParameter("lastname")
       SET @phone = RequestParameter("phone")
       SET @email = RequestParameter("email")
       SET @rating = RequestParameter("rating")
       SET @description = RequestParameter("description")
       SET @status = RequestParameter("status")
       SET @insert = InsertData("UC_Target_DE",
       "First Name",@firstname,
       "Last Name",@lastname,
       "Phone",@phone,
       "Email", @email,
       "Rating",@rating,
       "Description",@description,
       "Status",@status)              
]%%
 %%[ ELSE ]%%
<table style="padding: 20px;"><tr><td>
     <h2>Please Fill the form:</h2>
      <form action="%%=RequestParameter('PAGEURL')=%%" method="post">   
       <label>First Name: </label><input type="text" name="firstname" required="false"><br>
         <label>Last Name: </label><input type="text" name="lastname" required="false"><br>
         <label>Email: </label><input type="email" name="email" required="true"><br>
         <label>Phone: </label><input type="phone" name="phone" required="false"><br>
        <label>Description: </label><input type="text" name="description" required="false"><br>
              <label>Status: </label><input type="text" name="status" required="false"><br>
        <label>Rating: </label><input type="number" name="rating" required="false"><br>
         <input name="submitted" type="hidden" value="true"><br>
         <input type="submit" value="Submit">
      </form>  
</td></tr></table>
%%[ ENDIF ]%%

In the above code, data is getting saved into the Salesforce data extension “UC_Target_DE” whenever a person hits the Submit after populating the fields.

4. Now my requirement is to save data into two data extensions based on any criteria, So I choose the rating field and criteria which I defined is like if the value in Rating field is between 1-5 then data is going to save into the “UC_Target_DE” and if the value is between 6-10 then data is going to save into “MT_Target_DE”. 

dont miss out iconCheck out another amazing blog by Udit here: Commonly Made Mistakes in Salesforce Integration and How One Can Avoid Them

%%[
IF RequestParameter("submitted")==true THEN
       Var @insert      
       SET @firstname = RequestParameter("firstname")
       SET @lastname = RequestParameter("lastname")
       SET @phone = RequestParameter("Phone")
       SET @email = RequestParameter("Email")
       SET @rating = RequestParameter("Rating")
       SET @description = RequestParameter("Description")
       SET @status = RequestParameter("Status")      
      IF (@rating >=0 AND @rating <= 5) THEN
       SET @insert = InsertData("UC_Target_DE",
       "First Name",@firstname,
       "Last Name",@lastname,
       "Phone",@phone,
       "Email", @email,
       "Rating",@rating,
       "Description",@description,
       "Status",@status)       
       ELSE
       SET @insert = InsertData("MT_Target_DE",
       "First Name",@firstname,
       "Last Name",@lastname,
       "Phone",@phone,
       "Email", @email,
       "Rating",@rating,
       "Description",@description,
       "Status",@status)       
       ENDIF         
]%%
%%[ ELSE ]%%
<table style="padding: 20px;"><tr><td>
     <h2>Please Fill the form:</h2>
      <form action="%%=RequestParameter('PAGEURL')=%%" method="post">   
       <label>First Name: </label><input type="text" name="firstname" required="false"><br>
         <label>Last Name: </label><input type="text" name="lastname" required="false"><br>
         <label>Email: </label><input type="email" name="email" required="true"><br>
         <label>Phone: </label><input type="phone" name="phone" required="false"><br>
        <label>Description: </label><input type="text" name="description" required="false"><br>
              <label>Status: </label><input type="text" name="status" required="false"><br>
        <label>Rating: </label><input type="number" name="rating" required="false"><br>
         <input name="submitted" type="hidden" value="true"><br>
         <input type="submit" value="Submit">
      </form>  
</td></tr></table>
       %%[ ENDIF ]%%

This is how I fulfilled my requirement of saving data into two different data extensions by using conditions, you can have your own type of criteria to do the same. 

Go on and try it in your account.

Reference: AMPscript Guide

Popular Salesforce Blogs