2

I have noticed that by default when we PUBLISH the cloud page, all the AMPscript will be executed!

By this behaviour the below code creates a BLANK record in my Data Extension.

UpsertDE("DE_NAME",1,"Email", @email , "FN", @FN, "LN", @LN)

enter image description here

Can anyone suggest how to avoid this/ best practice to follow in such scenario!

  • 1
    Just surround that function with an if clause that checks if a person is existing? It really depends on your use case, your code definitely needs to change if you just blindly use upsert without checking the current context. – Johannes Schapdick Jan 22 at 12:24
  • 1
    Like Johannes said, it depends on your use case, but you should definetly check the context. If your cloudpage contains a form, you may take a look at Lukas's answer here: salesforce.stackexchange.com/questions/250796/… – Rachid Mamai Jan 22 at 12:26
  • 1
    Indeed, this would be an option - as using _messagecontext will always return LANDINGPAGE regardless of whether it is preview or actual viewing of the Cloud Page – Lukas Lunow Jan 22 at 12:34
7

So, I actually was inspired by this question to do some more digging. I found out that the URL fetched by RequestParameter('PageURL') differes, depending on whether the page is presented in the "Publish" context, or accessed through the proper URL, e.g. http://cloud.example.com/cloudpage

The URL in "publish" is internal and contains e.g. Preview part, e.g.: http://intsvc-s50.xt.local/internalservice/v1/CloudPages/Preview/3

Hence you can use following AmpScript to check for whether or not the Ampscript is executed in Publish or View context:

%%[
SET @isPreview = indexOf(lowercase(RequestParameter('PageURL')), "preview")
]%% 
URL is %%=RequestParameter('PageURL')=%% 
and context is: %%=IIF(@isPreview > 0,"Publish","View")=%%

This will help you prevent undesirable execution of the code, leading to strange records in data extensions. However in some cases this might not be sufficient, hence doing an additional check for e.g. form submission (like here: SFMC Cloudpage Execution Context is always load ) or presence of particular parameters with EMPTY() function, would be beneficial.

  • 1
    @ Lukas, thank you so much! for not only providing the best practice, for also performing R & D to understand the concept of EXECUTION CONTEXT.... I was able to overcome the issue by placing a condition as suggested above .... %%[ SET @isPreview = indexOf(lowercase(RequestParameter('PageURL')), "preview") IF @isPreview < 0 THEN UpsertDE("DE_NAME",1,"Email", @email , "FN", @FN, "LN", @LN) ENDIF ]%% – Vishal Kumar C V Jan 22 at 13:34

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.