0

I want to send email to my customers in SFMC. The email contains a list of products. The data of products comes from a HTTP request, and the HTTP response is a JSON object.

Now I've developed a content block to display a product. The content block contains product name, a thumbnail image, and a link. Then when I add several content blocks in email, I find that I have no method to update name and image and url for each content block.

PS, I tried document.getElementbyId but failed. It seems DOM related function is not fully supported in AMPScript.

The content block looks like below:

<table>
    <tr>
        <td>
            <a href=...>
                <img src=...>
            </a>
        </td>
    </tr>
    product name
</table>

The email contains several content blocks. I hope I can find a method to update each content block's image's src.

Thanks very much.

2
  • Let's see the code for your content block. Commented Dec 7, 2022 at 14:20
  • Also provide some details about how you're intentending to trigger the email. Commented Dec 7, 2022 at 14:23

1 Answer 1

1

You can do this, but to make it easiest, I would personally look to utilize just SSJS but in below I included AMPscript translation of the output vars in the content block as I am assuming that is where you feel stronger.

Basically in your content block you have it so that each of the values is an AMPscript variable and then using SSJS you get the JSON payload, using a for loop to ensure you hit each object inside of it, you parse through the payload to get the values inside of it then set AMPscript variables with the same values as the SSJS equivalents. You then call in the ContentBlock to display that version of content and then the for loop iterates to the next object and starts the whole process over until you have displayed all the objects in the returned JSON.

An example of how to do this is below:

HTML EMAIL

<script runat=server>
    Platform.Load("Core","1.1.1");

    var json = [my httprequest to get response]
    var len = json.length()

    for (i=0;i<len;i++) {
        var row = json[i]
        var cb_link = row.linkURL;
        var cb_img = row.imgURL;
        var cb_productName = row.product;

        Variable.SetValue("@cb_link",cb_link)
        Variable.SetValue("@cb_img",cb_img)
        Variable.SetValue("@cb_productName",cb_productName)
    
<script>

%%=ContentBlockByID(@myContentBlockID)=%%

<script runat=server>
    }
<script>

EXAMPLE JSON PAYLOAD

[
    {   
        linkURL: "link1",
        imgURL: "img1",
        product: "Product1"
    },
    {   
        linkURL: "link2",
        imgURL: "img2",
        product: "Product2"
    },
    {   
        linkURL: "link3",
        imgURL: "img3",
        product: "Product3"
    },
    {   
        linkURL: "link4",
        imgURL: "img4",
        product: "Product4"
    }
]

CONTENT BLOCK CODE

<table>
    <tr>
        <td>
            <a href="%%=RedirectTo(@cb_link)=%%">
                <img src="%%=v(@cb_img)=%%">
            </a>
        </td>
    </tr>
    %%=v(@cb_productName)=%%
</table>
1
  • Thanks a lot. It's quite helpful for me.
    – XWang
    Commented Dec 7, 2022 at 16:43

You must log in to answer this question.

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