0

I created a ampscript that goes inside a landing page in salesforce exacttarget:

    SET @dtenvio = IsNullDefault(QUERYPARAMETER("dtenvio"), Now(1))

    var @data
    set @data = v(@dtenvio)
    set @data = FormatDate(v(@data), "YYYY-MM-DD")
    set @year = DatePart(v(@data),"y")
    set @year2 = multiply(v(@year),10000)

    set @month = DatePart(v(@data),"m")
    set @month2 = multiply(v(@month),100)

    set @day = DatePart(v(@data),"d")

    set @data_envio = add(v(@Year2),v(@month2))
    set @data_envio = add(v(@data_envio ),v(@day))

The thing is a get an error only after que user submits a form: "Invalid value specified for function parameter. Function Name: DatePart Ordinal 1"

I tried outputing the results of all the dateparts functions and they were all okey

%%=v(year)=%% results in 2017 for example. All the resulting code works fine but when the user submits the form the error occurs even though the code was already executed with no problems. Does anyone know what might be happening?

1 Answer 1

1

Your IsNullDefault() function is the problem. QueryParameter() does not return null if the parameter doesn't exist -- it returns an empty string.

I'd write it with an inline-if like this:

%%[
var @dtenvio, @data, @year, @year2, @month, @month2, @day, @data_envio

SET @dtenvio = iif(empty(QueryParameter("dtenvio")),Now(),QueryParameter("dtenvio"))

set @data = FormatDate(@dtenvio, "YYYY-MM-DD")

set @year = DatePart(@data,"y")
set @year2 = multiply(@year,10000)

set @month = DatePart(@data,"M")
set @month2 = multiply(@month,100)

set @day = DatePart(@data,"d")

set @data_envio = add(@Year2, @month2)
set @data_envio = add(@data_envio, @day)

]%%
Now(1): %%=Now(1)=%%
<br>@dtenvio: %%=v(@dtenvio)=%%
<br>@data: %%=v(@data)=%%
<br>@year: %%=v(@year)=%%
<br>@year2: %%=v(@year2)=%%
<br>@month: %%=v(@month)=%%
<br>@month2: %%=v(@month2)=%%
<br>@day: %%=v(@day)=%%
<br>@data_envio: %%=v(@data_envio)=%%

Output

Now(1): 3/14/2017 9:30:28 AM 
@dtenvio: 3/14/2017 9:30:28 AM 
@data: 2017-03-14 
@year: 2017 
@year2: 20170000 
@month: 03 
@month2: 300 
@day: 14 
@data_envio: 20170314 

Also, there's no need to wrap variables inside AMPScript blocks with the v() function. It's only necessary outside of the block.

1
  • Thanks Adam, even though "data_envio" was outputing the correct info the landing page was presenting an error, it made me look for the answer in the wrong place. Did as you suggested and it worked. Mar 14, 2017 at 17:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .