[SalesForce] Ampscript: combining IF with Lookup or LookupRows

We send emails to our dealers, and for each brand a dealer sells we have a record in a 'Brands' synchronized data extension. So if one dealer sells multiple brands, there will be multiple records for his id in that data extension. I'm trying to come up with the simplest way to only show a piece of content/HTML to a dealer selling a certain brand.

Because I'm only getting started with Ampscript, first I put a simple lookup as content in the email. This is what I started with:

%%=Lookup("ent.Brands","Brand","DealerId", AccountId)=%%

This returns a brand when I go through the various dealers in my email preview. But, only one. So I should probably be using LookupRows instead of Lookup if I want all of the values returned, is that correct?

Then, I hoped I could use the Lookup as condition in an IF statement. So I made an HTML block which is basically like this:

%%[IF Lookup("ent.Brands","Brand","DealerId", AccountId) = "Brandname" THEN]%%
--HTML--
%%[ENDIF]%%

But the preview screen now returns the following error:

There is an error in your email. Please contact your customer service
representative. Error 1: Script IF Statement Invalid The script
condition contains an invalid comparison operator.

I've seen examples here where a variable is declared, filled using a lookup, and then used in a way like IF @variable = "value",

I also came across this page on ampscript.com, but I don't fully understand it and I think that's maybe more than what I'm looking for.

Basically, what I'm looking for is Ampscript that compares all returned rows with a certain value (brand name) and if one of the rows contains that value, show some HTML. Can anyone help find the easiest way to do this?

Thanks in advance!

Best Answer

What you are going to want to do in order to compare all of the possible brands against that single value is utilize LookupRows instead of Lookup. (as you suspected). From there you will need to do a for loop over the final IF statement to iterate through each value.

See below:

%%[

SET @Rows = LookupRows("ent.Brands","DealerId", AccountId) /*Gets the rowset */

IF Rowcount(@Rows) > 0 THEN /* Verifies there is data in Rowset */

  FOR @i=1 TO Rowcount(@Rows) DO /* Iterates through Rowset */

    SET @Row = Row(@Rows,@i) /* Assigns specific row */

    SET @Brand = Field(@Row,"Brand") /* Assigns Brand Value */

    IF @Brand == "Brandname" THEN /* Compares assigned value to your static value */
]%%
--HTML-- 
%%[ ENDIF /* End comparison */

  Next @i /* End iteration */

ENDIF /* end verification of data */
]%%
Related Topic