Quantcast
Channel: Active questions tagged ampscript - Salesforce Stack Exchange
Viewing all articles
Browse latest Browse all 359

How to Correctly Exclude Checkout Day in Daily Rate Calculation with AMPscript?

$
0
0

I am using AMPscript in Salesforce Marketing Cloud to parse serialized JSON data for hotel reservations. Each entry contains start and end dates along with a daily rate. I need to calculate and display rates for each day of the stay, excluding the rate for the checkout day.

Data example:

[{"start":"2024-08-22","end":"2024-08-24","amount":546.4}, {"start":"2024-08-24","end":"2024-08-25","amount":581.68}]

Desired Output:

•   August 22, 2024: $546.40•   August 23, 2024: $546.40•   August 24, 2024: $581.68

However, my current implementation incorrectly includes an entry for the checkout day and duplicates the rate for the transition day:

AMPscript Snippet:

%%[Set @json = AttributeValue("serializedresdata")Set @rows = BuildRowsetFromJSON(@json, "$[*]", true)For @row = 1 TO RowCount(@rows) do  Set @rowData = Row(@rows, @row)  Set @start = Field(@rowData, "start")  Set @end = Field(@rowData, "end")  Set @amount = Field(@rowData, "amount")  Set @startDate = DateParse(@start)  Set @endDate = DateParse(@end)  Set @dateDiff = DateDiff(@startDate, @endDate, "D")  For @i = 0 TO @dateDiff - 1 do    Set @currentDate = DateAdd(@startDate, @i, "D")    Set @formattedDate = FormatDate(@currentDate, "MMMM dd, yyyy")    Output(Concat("<p>", @formattedDate, ": $", FormatCurrency(@amount, "en-US"), "</p>"))  next @inext @row]%%

Issue:The output incorrectly displays:

•   August 22, 2024: $546.40•   August 23, 2024: $546.40•   August 24, 2024: $546.40 (should not include as start of new reservation)•   August 24, 2024: $581.68•   August 25, 2024: $581.68 (checkout day, should not display)

How can I adjust the AMPscript to avoid duplicating the daily rate for the transition day and exclude the checkout day’s rate?

Any assistance would be greatly appreciated!

Regards,

Matt


Viewing all articles
Browse latest Browse all 359

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>