Skip to content

Instantly share code, notes, and snippets.

@wvpv
Created December 15, 2015 16:54
Show Gist options
  • Save wvpv/4935bf919ce38a77609e to your computer and use it in GitHub Desktop.
Save wvpv/4935bf919ce38a77609e to your computer and use it in GitHub Desktop.
SFMC AMPScript Upsert/UpdateAdd Data Extension Row
%%[
VAR @deObj, @prop, @de_statusCode, @de_statusMsg, @errorCode
var @FirstName, @LastName, @Email, @SubscriberKey
set @FirstName = RequestParameter("FirstName")
set @LastName = RequestParameter("LastName")
set @Email = RequestParameter("Email")
set @SubscriberKey = RequestParameter("SubscriberKey")
/* for debugging */
outputline(concat("<br>@FirstName: ",@FirstName,""))
outputline(concat("<br>@LastName: ",@LastName,""))
outputline(concat("<br>@Email: ",@Email,""))
outputline(concat("<br>@SubscriberKey: ",@SubscriberKey,""))
SET @deObj = CreateObject("DataExtensionObject")
SetObjectProperty(@deObj, "CustomerKey", "TestDataExtension")
IF NOT empty(@FirstName) THEN
SET @prop = CreateObject("APIProperty")
SetObjectProperty(@prop, "Name", "FirstName")
SetObjectProperty(@prop, "Value", @FirstName)
AddObjectArrayItem(@deObj, "Properties", @prop)
ENDIF
IF NOT empty(@LastName) THEN
SET @prop = CreateObject("APIProperty")
SetObjectProperty(@prop, "Name", "LastName")
SetObjectProperty(@prop, "Value", @LastName)
AddObjectArrayItem(@deObj, "Properties", @prop)
ENDIF
IF NOT empty(@email) THEN
SET @prop = CreateObject("APIProperty")
SetObjectProperty(@prop, "Name", "Email")
SetObjectProperty(@prop, "Value", @email)
AddObjectArrayItem(@deObj, "Properties", @prop)
ENDIF
IF NOT empty(@SubscriberKey) THEN
SET @prop = CreateObject("APIProperty")
SetObjectProperty(@prop, "Name", "SubscriberKey")
SetObjectProperty(@prop, "Value", @SubscriberKey)
AddObjectArrayItem(@deObj, "Properties", @prop)
ENDIF
SET @updateOptions = CreateObject("UpdateOptions")
SET @saveOptions = CreateObject("SaveOption")
SetObjectProperty(@saveOptions, "SaveAction", "UpdateAdd")
SetObjectProperty(@saveOptions, "PropertyName", "*")
AddObjectArrayItem(@updateOptions, "SaveOptions", @saveOptions)
var @options
SET @de_statusCode = InvokeUpdate(@deObj, @de_statusMsg, @errorCode, @updateOptions)
outputline(concat("<br>@de_statusCode: ",@de_statusCode,""))
outputline(concat("<br>@de_statusMsg: ",@de_statusMsg,""))
outputline(concat("<br>@errorCode: ",@errorCode,""))
IF @de_statusCode != "OK" THEN
RaiseError(@de_statusMsg, 0, @de_statusCode, @errorCode)
ENDIF
]%%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment