1

I am new to SFMC. Trying to populate the dropdown list from a DE through Ampscript.

I am trying this code. Any help is appreciated.

Data DE Name: testcompname

Column Name: company_name

Sending DE Name: test_seed

Column Names: email_id, org_name

%%[ 
var @compname, @compname_rows
SET @compname_rows = lookupRows("testcompname", "compname", @company_name)
SET @compname = Lookup(@testcompname, "key", "company_name")

%%[ if @compname_rows > 0 then ]%%

    <select name="">
        <option value="">Select</option>

    %%[ for @i = 1 to @compname_rows do ]%%

        <option value="%%=v(@compname)(@i)=%%">%%=v(@compname)(@i)=%%</option>

    %%[ next @i ]%%  

    </select>

%%[ else ]%%

        <br>Options not found 
%%[ endif ]%%
]%%
4
  • would you be able to explain the use case here at a high level? This will make it easier to debug the code. Cheers Aug 27, 2018 at 10:50
  • Are you wanting to output all of the company names every time or just specific ones that match a value? Aug 27, 2018 at 11:17
  • Hey Adam, thanks for the code. I need to display all the company names in the dropdown and there is no region specific.
    – Prateek
    Aug 29, 2018 at 4:02
  • You'll still need a lookup value even if you need to retrieve all of them. Here's an example of using a lookup key. Aug 31, 2018 at 11:30

1 Answer 1

5

I'd suggest looking over some the lookup examples:

If I were to output an option list of companies for a specific region, I'd approach it like this:

%%[

var @compname_rows, @compname_row, @rowCount, @company_region, @i

set @company_region = "Central"
set @compname_rows = LookupRows("Companies", "region", @company_region)
set @rowCount = rowcount(@compname_rows)

output(concat("company_region: ", @company_region))

if @rowCount > 0 then 

    for @i = 1 to @rowCount do

        if @i == 1 then
          output(concat('<select name=""><option value="">Select</option>'))
        endif

        var @compname
        set @compname_row = row(@compname_rows, @i)
        set @compname = field(@compname_row,"emailAddress")

        output(concat('<option value="', @compname, '">', @compname, '</option>'))

        if @i == @rowCount then
          output(concat('</select>'))
        endif

    next @i 

else

 output(concat("<br>companies not found"))

endif 

]%%

Setting your lookup key can vary based on how the input is getting to where the AMPscript is located.

  • for an email: set @company_region = AttributeValue("region")
  • for a landing/cloud page: set @company_region = QueryParameter("region")

Reference

0

You must log in to answer this question.

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