I've built some AMPscript code that selects two different contentblocks to be displayed next to each other in the same block. The code retrieves all the blocks that have the content_type "themablok".
The first block is selected by randomly selecting an index between 1 and the total rowcount divided by two.
The second block is selected by randomly selecting an index between the total rowcount divided by two and the total rowcount. This makes sure that the code doesn't select the same row twice.
It works great, except when I try to view the email in my browser via the %%view_email_url%% string. The AMPscript seems to get rendered again, and I get totally different blocks in the HTML mail. How can I make sure that the blocks I received in the mail clients don't differ from the blocks I see when I press the 'View this email as HTML' button?
Code:
/* initiate variables and lookup blocks */
var @blocks, @block, @blockCount
set @blocks = LookupRows("contentBlokkenTest", "content_type", "themablok", "thema", @FavouriteTheme)
set @blockCount = rowcount(@blocks)
if @blockCount > 0 then
/* Define variables */
var @subidBlock
/* Set variables block 1*/
SET @themeIndexHalf = Divide(@blockCount, 2)
SET @themeIndexHalf = FormatNumber(@themeIndexHalf, "N0")
set @randomThemeIndex = Random(1, Subtract(@themeIndexHalf, 1))
set @block = row(@blocks,@randomThemeIndex) /* get random theme block #1 */
set @subidBlock1 = field(@block,"subid")
set @subidBlockset = "true"
/* Block 2, get second half of indexes */
set @randomThemeIndex = Random(@themeIndexHalf, @blockCount)
set @block = row(@blocks,@randomThemeIndex) /* get random theme block #2 */
set @subidBlock2 = field(@block,"subid")
if @subidBlock2 == @historyBlockSubid AND @blockCount > 1 then
set @randomThemeIndex = Random(@themeIndexHalf, @blockCount)
set @block = row(@blocks,@randomThemeIndex) /* get next random theme block #2 */
set @subidBlock2 = field(@block,"subid")
else
set @block = row(@blocks,@randomThemeIndex) /* get theme block #1 */
set @subidBlock2 = field(@block,"subid")
endif
set @subidBlockset = "true"
/* fallback content subid */
else
set @subidBlock1 = "lxtr"
set @subidBlock2 = "lmgr1"
endif
/* END: getting thema content blocks */