2

Im trying to create an email via REST API, based on an existing template. The only real documentation I can find is https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/content.htm But it dosnt show how I use an existing template, only creating new HTML PASTE email.

Have anyone tried to do this, or have some examples of this?

EDIT: I have managed to get the correct template, but is dosnt put content in my slot called "contentarea2".

{
  "assetType": {
    "id": 207
  },
  "name": "Test_9",
  "views": {
    "subjectline": {
      "content": "Hello"
    },
    "preheader": {
         "content": "preheader text here"
    },
    "html": {
        "template": {
        "id": 86912,
        "slots": {
        "contentarea2": {
            "content": "<div data-type=\"block\" data-key=\"block1\"></div>",
            "design": "click here to start editing this block",
            "blocks":{
                "block1":{
                    "content":"123",
                        "design":"click here to start editing this block",
                        "assetType":{  
                           "name":"freeformblock",
                           "id":195
                        }
                }
            }
        }
      }
    }
    },
    "data": {
      "email": {
        "options": {
          "characterEncoding": "utf-8"
        }
      }
    }
  }
}
3
  • I tried this with the SOAP API a few years ago and it wasn't possible. I doubt if the REST can do it. The only way back then was to create an HTML Paste every time. Commented Apr 26, 2018 at 16:08
  • They write: 'the templated is pulled from an existing template asset and therefore has an id'. So I understand it as it is possible.
    – Bidstrup
    Commented Apr 26, 2018 at 16:41
  • Did you find a solution ?
    – TaouBen
    Commented Dec 9, 2022 at 22:28

1 Answer 1

0

It took some time but with some research and trial and error, I ended up with this relatively stable version (written in Apex).

// Not sure if it compiles, I had to remove some dependencies, but you will grasp the idea

Map<String, Object> template = yourConnection.fetch('your template by id');
Map<String, Object> modifiedCopy = adjustedCloneOf(template);

String mail = JSON.serialize(new Map<String, Object> {
    'name' => 'A unique Name',
    'category' => new Map<String, Object>{'id' => folderAssetId},
    'assetType' => new Map<String, Object>{'id' => 207},
    'views' => new Map<String, Object> {
        'subjectline' => new Map<String, Object>{'content' => 'this is your subject'},
        'html' => new Map<String, Object> {
            'template' => new Map<String, Object> {
                'id' => template.get('id'),
                'assetType' => new Map<String, Object>{'id' => 4},
                'content' => template.get('content'),
                'slots' => template.get('slots')
            },
            'content' => template.get('content'),
            'slots' =>  modifiedCopy.get('slots')// this is the most important part
        },
        'preheader' => new Map<String, Object> {} // also required
    },
    'data' => new Map<String, Object> {
        'email' => new Map<String, Object> {
            'options' => { characterEncoding: 'utf-8'},
            'attributes' => new List<Map<String, Object>> {
                new Map<String, Object> {
                    'DisplayName' => '__AdditionalEmailAttribute1',
                    'Name' => '__AdditionalEmailAttribute1',
                    'Value' => myDynamicValue,
                    'Order' => 1,
                    'Channel' => 'email',
                    'AttributeType' => 'AdditionalEmailAttribute'
                }
            }
        }
    }
});

The most important part is, that you have to pass in the current template (or at least some of the key values of it) completely unchanged! Including the htmls 'content' attribute. Your changes should then be made to the slots that are passed into the html.slots. If you follow the above structure, the template based email will behave like a manually created version (at least this is what I experienced and tested against).

You must log in to answer this question.

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