[SalesForce] Multiple rows using Loop in Ampscript

I want to print some rows in a table of the HTML email based on the count of rows (rowcount) that match the criteria.

According with the number of rows, it have to show the number of informations. For example: 1 row = 1 info, 2 rows = 2 infos.

I'm using 'FOR' to do a loop and populate the table rows with the records, but it's not working as expected. It shows only one row even when it must show more than one.

Is there a way to resolve this?

I am using the code below:

%%[

set @localjob = AttributeValue('Questionario__c:Local_de_Avaliacao__c')
set @nomejob = AttributeValue('Questionario__c:Nome_Job_simplificado__c')
set @id = AttributeValue('Questionario__c:Pesquisador__c')

set @Questionario = retrievesalesforceobjects('Questionario__c','Pesquisador__c,Status_anterior_do_questionario__c,Nome_Job_simplificado__c,Local_de_Avaliacao__c,Status_do_Questionario__c','Status_anterior_do_questionario__c','=','Em Andamento','Status_do_Questionario__c','=','Expirado','Pesquisador__c','=',@id)

set @rowcount = rowcount(@Questionario)


set @styletd = "-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; border: 1px solid #606060; border-collapse: collapse !important; color: #606060; font-family: 'Ubuntu', 'Candara', 'Arial', sans-serif; font-size: 10px; line-height: 120%; padding: 8px 0;"


for @contador = 1 to @rowcount do

set @rowcelula = row(@Questionario,@contador)

set @fieldcelula1 = field(@rowcelula,'Nome_Job_simplificado__c')

set @fieldcelula2 = field(@rowcelula,'Local_de_Avaliacao__c')


set @linha = CONCAT('<tr><td align="center" style="',@styletd,'">',@fieldcelula1,'</td>','<td align="center" style="',@styletd,'">',@fieldcelula2,'</td></tr>')


next @contador

]%%

Best Answer

You need to output the concatenated variable using the V() AMPScript function, you can set this inside your for loop so on each iteration, this can be achieved with the refined code below, I have not tested this code but it should work.

I added an opening and closing <table> tag in case it is required otherwise you can remove that.

%%[

set @localjob = AttributeValue('Questionario__c:Local_de_Avaliacao__c') set @nomejob = AttributeValue('Questionario__c:Nome_Job_simplificado__c') set @id = AttributeValue('Questionario__c:Pesquisador__c')

set @Questionario = retrievesalesforceobjects('Questionario__c','Pesquisador__c,Status_anterior_do_questionario__c,Nome_Job_simplificado__c,Local_de_Avaliacao__c,Status_do_Questionario__c','Status_anterior_do_questionario__c','=','Em Andamento','Status_do_Questionario__c','=','Expirado','Pesquisador__c','=',@id)

set @rowcount = rowcount(@Questionario)


set @styletd = "-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; border: 1px solid #606060; border-collapse: collapse !important; color: #606060; font-family: 'Ubuntu', 'Candara', 'Arial', sans-serif; font-size: 10px; line-height: 120%; padding: 8px 0;"

]%%

<table>

%%[ for @contador = 1 to @rowcount do

set @rowcelula = row(@Questionario,@contador)

set @fieldcelula1 = field(@rowcelula,'Nome_Job_simplificado__c')

set @fieldcelula2 = field(@rowcelula,'Local_de_Avaliacao__c')

set @linha = CONCAT('<tr><td align="center" style="',@styletd,'">',@fieldcelula1,'</td>','<td align="center" style="',@styletd,'">',@fieldcelula2,'</td></tr>')

v(@linha)

next @contador ]%%

</table>

Reference

V AMPScript Function

Related Topic