Expand my Community achievements bar.

SOLVED

Dynamic Product Listing in Adobe Campaign Classic Transactional Emails

Avatar

Level 4

Hi Team,

 

We have a requirement to list all product details in the email body shared in the ctx payload,

 

the payload is as below

 

<ctx>
                        <orderDetails>
                            <firstName>Shine</firstName>
                            <lastName>vv</lastName>
                            <ctaLink>https://www.test.com </ctaLink>
                            <pickUpDate>28 Aug 2023 </pickUpDate>
                            <pickUpStore>XXYY </pickUpStore>
                            <orderId>TESTORDERID001 </orderId>
                            <subTotal>22001.00 </subTotal>
                            <discount>5000.00 </discount>
                            <tax>1200.00 </tax>
                            <total>20000.00 </total>
                            <advanceId>ADVANCEID001 </advanceId>
                            <advanceAmountPaid>5000.00 </advanceAmountPaid>
                            
                        </orderDetails>
                        <productDetails>
                            <product1>
                                <productName>product1 </productName>
                                <productImageUrl>https://product1.jpg</productImageUrl>
                                <productPrice>18000.00 </productPrice>
                            </product1>
                            <product2>
                                <productName>product2 </productName>
                                <productImageUrl>https://product2.jpg</productImageUrl>
                                <productPrice>18000.00 </productPrice>
                            </product2>
                        </productDetails>
                    </ctx>
 
The required output in the email body is as below :

 

Product 1 
productName : 
productImage :
productPrice :
 
Product 2
productName : 
productImage :
productPrice :
 
Product 3
productName : 
productImage :
productPrice :
 
.
.
.
etc
 
There can be n number of products and all of the products must be displayed in the email. Trying to loop through the xml code like below :
 
<% var ctx = XML(rtEvent.ctx); %>

<% for(var i=1;i <= ctx.productDetails.childNodes.length;i++){%>
<%=escapeXmlStr(ctx["product"+i+".productName"])%> <%=escapeXmlStr(ctx["product"+i+".productImageUrl"])%>
 
<% } %>
 
but i am not getting any out put,
 
Please help me on the same, Thanks in advance.
 
Thanks,
Shine v.v
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 3

Hello Shine, 

 

On your XML, you can't have tags as <product1>... <product2>. It needs to be always just <product>. The correct XML would be:

<ctx>
    <orderDetails>
        <firstName>Shine</firstName>
        <lastName>vv</lastName>
        <ctaLink>https://www.test.com </ctaLink>
        <pickUpDate>28 Aug 2023 </pickUpDate>
        <pickUpStore>XXYY </pickUpStore>
        <orderId>TESTORDERID001 </orderId>
        <subTotal>22001.00 </subTotal>
        <discount>5000.00 </discount>
        <tax>1200.00 </tax>
        <total>20000.00 </total>
        <advanceId>ADVANCEID001 </advanceId>
        <advanceAmountPaid>5000.00 </advanceAmountPaid>
        
    </orderDetails>
    <productDetails>
        <product>
            <productName>product1 </productName>
            <productImageUrl>https://product1.jpg</productImageUrl>
            <productPrice>18000.00 </productPrice>
        </product>
        <product>
            <productName>product2 </productName>
            <productImageUrl>https://product2.jpg</productImageUrl>
            <productPrice>18000.00 </productPrice>
        </product>
    </productDetails>
</ctx>

 

With that in mind, you just need to do a for each loop in the productDetails vector, like such:

 

<% var xml = new XML(rtEvent.ctx); %>

</br>
<% for each (var item in xml.productDetails.product) { %>

productName: <%= item.productName.toString() %>
</br>
productImageUrl: <%= item.productImageUrl.toString() %>
</br>
productPrice: <%= item.productPrice.toString() %>

</br>
</br>
<% } %> 

 

If you need a count or prefer a for loop, the syntax would be:

<% for (var i = 0; i < xml.productDetails.product.length; i++) {%>

productName: <%= xml.productDetails.product[i].productName.toString() %>
</br>
productImageUrl: <%= xml.productDetails.product[i].productImageUrl.toString() %>
</br>
productPrice: <%= xml.productDetails.product[i].productPrice.toString() %>

</br>
</br>

<% } %> 

 

Let me know if that works for you.

Jorge 

View solution in original post

2 Replies

Avatar

Correct answer by
Level 3

Hello Shine, 

 

On your XML, you can't have tags as <product1>... <product2>. It needs to be always just <product>. The correct XML would be:

<ctx>
    <orderDetails>
        <firstName>Shine</firstName>
        <lastName>vv</lastName>
        <ctaLink>https://www.test.com </ctaLink>
        <pickUpDate>28 Aug 2023 </pickUpDate>
        <pickUpStore>XXYY </pickUpStore>
        <orderId>TESTORDERID001 </orderId>
        <subTotal>22001.00 </subTotal>
        <discount>5000.00 </discount>
        <tax>1200.00 </tax>
        <total>20000.00 </total>
        <advanceId>ADVANCEID001 </advanceId>
        <advanceAmountPaid>5000.00 </advanceAmountPaid>
        
    </orderDetails>
    <productDetails>
        <product>
            <productName>product1 </productName>
            <productImageUrl>https://product1.jpg</productImageUrl>
            <productPrice>18000.00 </productPrice>
        </product>
        <product>
            <productName>product2 </productName>
            <productImageUrl>https://product2.jpg</productImageUrl>
            <productPrice>18000.00 </productPrice>
        </product>
    </productDetails>
</ctx>

 

With that in mind, you just need to do a for each loop in the productDetails vector, like such:

 

<% var xml = new XML(rtEvent.ctx); %>

</br>
<% for each (var item in xml.productDetails.product) { %>

productName: <%= item.productName.toString() %>
</br>
productImageUrl: <%= item.productImageUrl.toString() %>
</br>
productPrice: <%= item.productPrice.toString() %>

</br>
</br>
<% } %> 

 

If you need a count or prefer a for loop, the syntax would be:

<% for (var i = 0; i < xml.productDetails.product.length; i++) {%>

productName: <%= xml.productDetails.product[i].productName.toString() %>
</br>
productImageUrl: <%= xml.productDetails.product[i].productImageUrl.toString() %>
</br>
productPrice: <%= xml.productDetails.product[i].productPrice.toString() %>

</br>
</br>

<% } %> 

 

Let me know if that works for you.

Jorge 

Avatar

Level 4

Hi @jorgeferreira ,

 

Thank you so much for your help, it is working fine. 

 

Thanks,

Shine v.v