0

I have a sendable_DE that looks like the below. I am sending to the ContactID and will dedupe at send.

enter image description here

I want the AMPscript to read all the rows based on ContactID as lookupvalue and drop the script based on asset_name_new. so there are 3 scenarios here.

  1. asset_name_new contains aaa only THEN Script_a
  2. asset_name_new contains bbb only THEN Script_b
  3. asset_name_new contains aaa AND bbb THEN Script_c

Below is my code but it seems to be reading the first row only and not all rows. So where there is aaa and bbb then Script_c isn't showing.


var @rows, @row, @rowCount, @numRowsToReturn, @lookupValue, @i,

set @lookupValue = AttributeValue("ContactID")
set @numRowsToReturn = 0
set @rows = LookupOrderedRows("DE_name",@numRowsToReturn,"asset_name_new asc", "ContactID", @lookupValue)
set @rowCount = rowcount(@rows)

if @rowCount > 0 then

  for @i = 1 to @rowCount do

    var @asset
    
    set @row = row(@rows,@i)
    set @asset = field(@row,"asset_name_new")
            
]%%

%%[
 If @asset == "aaa" AND @asset != "bbb"
    Then 
    set @asset_return = "Script_a" endif
 
 If @asset == "bbb" AND @asset != "aaa"
    Then 
    set @asset_return = "Script_b" endif

 If @asset == "aaa" AND @asset = "bbb"
    Then 
    set @asset_return = "Script_c" endif
     
]%%
   
%%[ next @i ]%%

%%[ else ]%%

%%[ endif ]%%

<span>%%=v(@asset_return)=%%</span>
0

1 Answer 1

1

If there can only be two possible values for asset_name_new you can count how many instances of each asset_name_new exist for each subscriber in order to determine the return value.

%%[
var @rows, @row, @rowCount, @numRowsToReturn, @lookupValue, @i,

set @lookupValue = AttributeValue("ContactID")
set @numRowsToReturn = 0
set @rows = LookupOrderedRows("DE_name",@numRowsToReturn,"asset_name_new asc", "ContactID", @lookupValue)
set @rowCount = rowcount(@rows)

if @rowCount > 0 then
  
  set @asset_name_new_count1 = 0
  set @asset_name_new_count2 = 0

  for @i = 1 to @rowCount do

      set @row = row(@rows,@i)
      set @asset = field(@row,"asset_name_new")

      If @asset == "aaa" Then 
         set @asset_name_new_count1 = add(@asset_name_count1,1)
      endif

      If @asset == "bbb" Then 
         set @asset_name_new_count2 = add(@asset_name_count2,1)
      endif

   next @i 

   if @asset_name_new_count1 > 0 and @asset_name_new_count2 == 0 then

     set @asset_return = "Script_a"

   elseif @asset_name_new_count1 == 0 and @asset_name_new_count2 > 0 then 

     set @asset_return = "Script_b"

   elseif @asset_name_new_count1 > 0 and @asset_name_new_count2 > 0 then 

      set @asset_return = "Script_c"

   else

      raiseError("no content found", 1)
      
   endif

]%%

%%[ else ]%%


%%[ endif ]%%
1
  • That is beautifully constructed Adam & works as expected. I thank you.
    – Rahul
    Feb 14, 2022 at 1:28

You must log in to answer this question.

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