Considering you need this for the form that creates an opportunity mentioned in your previous question, you can use the following:
<script runat="server">
Platform.Load("Core","1.1.1");
try{
</script>
%%[
IF RequestParameter("submitted") == true THEN
set @email = QueryParameter("email")
set @firstName = RequestParameter("firstName")
set @lastName = RequestParameter("lastName")
set @email = RequestParameter("email")
set @amount = FormatNumber(RequestParameter("Amount"),"N2")
/* check if contact exists*/
set @retrievecontact = RetrieveSalesforceObjects("Contact","Id,Email","Email", "=", RequestParameter("email"))
IF RowCount(@retrievecontact) > 0 THEN
/* get Id if exists*/
set @ContactId = Field(Row(@retrievecontact, 1), "Id")
ELSE
/* create a new one if doesn't exists*/
set @ContactId = CreateSalesforceObject("Contact", 3, "FirstName", @firstName, "LastName", @lastName, "Email", @email)
ENDIF
/* create opportunity for either*/
set @opportunity = CreateSalesforceObject("Opportunity", 3, "Name", "Test_donation", "Amount", @amount, "npsp__Primary_Contact__c", @ContactId)
]%%
<h2>Thank you for submitting the form.</h2><br>
<h3>The amount enetered was $ %%=v(@amount)=%%</h3>
%%[ ELSE ]%%
<table style="padding: 20px;"><tr><td>
<h2>Please fill in the form:</h2>
<form action="%%=RequestParameter('PAGEURL')=%%" method="post">
<label>First name: </label><input type="text" name="firstname" required=""><br>
<label>Last name: </label><input type="text" name="lastname" required=""><br>
<label>Company: </label><input type="text" name="company" required=""><br>
<label>Email: </label><input type="text" name="email" required=""><br><br>
<input type="radio" name="Amount" value="1000"> $ 1000<br>
<input type="radio" name="Amount" value="500"> $ 500<br>
<input type="radio" name="Amount"> $ <input type="number" name="Amount"><br>
<input name="submitted" type="hidden" value="true"><br>
<input type="submit" value="Submit">
</form>
</td></tr></table>
%%[ ENDIF ]%%
<script runat="server">
}catch(e){
Write(Stringify(e));
}
</script>
The RequestParameter('PAGEURL')
function reloads the page when the form is submitted, posting the form parameters to back the page, which are then retrieved by the RequestParameter()
functions.
If above is failing, check the following things:
- how many required fields are on a Contact record and if you are providing values for all of them
- how many required fields are on an Opportunity record and if you are providing values for all of them
- do the field types and lengths match for fields on a Contact record
- do the field types and lengths match for fields on an Opportunity record
- especially check the Amount fields and the settings you have in Sales/Service Cloud, maybe the field needs to be passed as something else than a number, maybe the format needs to be changed
- check if you have any rules in your Sales/Service Cloud that would prevent duplication and are now preventing you from creating a test Contact/Opportunity using the same test data
- try hardcoding some values to see if the Opportunity will be created and only then replace them with AMPscript variables, for example:
set @opportunity = CreateSalesforceObject("Opportunity", 3, "Name", "Test_donation", "Amount", "1000", "npsp__Primary_Contact__c", @ContactId)