1

I want help in AMP script for below scenario

From the web studio cloudpage the new contact record should be created in sales cloud. Contact duplication should be avoided based on unique email id. If the Contact already exists, only new opportunity should be created else new contact and corresponding new opportunity should be created.

I am able to create new contact using createSalesforceObject () function, can someone help on remaining part? its really troubling me now.

Below is the code

%%[
var @email, @firstName, @lastName, @createSfRecord, @createSfRecord1, @createSfRecord2, @createSfRecord3
set @email = QueryParameter("email")

set @firstName = RequestParameter('firstName')
set @lastName = RequestParameter('lastName')
set @email = RequestParameter('email')
set @createSfRecord = RetrieveSalesforceObjects("contact","firstName", "email","=", @email)

if RowCount(@createSfRecord) > 0 THEN

set @createSfRecord1 = CreateSalesforceObject(
    "Opportunity", 3,
    "Name", "Test_donation",
    "Amount", "1000",
    "npsp__Primary_Contact__c", "Test Contact")

ELSE

 set @firstName = RequestParameter('firstName')
 set @lastName = RequestParameter('lastName')
 set @email = RequestParameter('email')

set @createSfRecord2 = CreateSalesforceObject(
  'Contact', 3,
  'FirstName', @firstName,
  'LastName', @lastName,
  'Email', @email)

set @createSfRecord3 = CreateSalesforceObject(
    "Opportunity", 3,
    "Name", "Test_donation",
    "Amount", "1000",
    "npsp__Primary_Contact__c", "Test Contact")  
endif

]%%
3
  • Hi, you are asking for someone to do the work for you. Please provide what you have tried and errors that you are getting. You can check the help for some examples: developer.salesforce.com/docs/… Oct 4, 2019 at 9:32
  • As Rachid said, please give it a try first and if you encounter any errors, post your code here. Regarding the concept you are asking about, it’s described here: sfmarketing.cloud/2019/09/22/…
    – zuzannamj
    Oct 4, 2019 at 9:44
  • Thank you both. I have pasted my code in the original post now Oct 4, 2019 at 9:48

1 Answer 1

3

You need to first check if the contact exists, and then:

  • if it does just create an opportunity,
  • if it doesn't, create both contact and opportunity.

This should do it:

%%[ var @email, @firstName, @lastName, @retrievecontact, @ContactId, @opportunity

set @email = QueryParameter("email")
set @firstName = RequestParameter("firstName") 
set @lastName = RequestParameter("lastName") 
set @email = RequestParameter("email") 

/* 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", "1000", "npsp__Primary_Contact__c", @ContactId)

]%%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.