0

I'm trying to retrieve a content block for a script in ssjs. I tried already several functions but it seems not working. The scope is with a script to retrieve a content from a content area and push this information in a email.

%%[/*SET @html = ContentBlockByKey("f716faaa-8170-4940-b1af-c84ee1f5862a")*/]%%

<script runat=server language=javascript>
Platform.Load("core", "1.1.5");
/************************ UTILITY FUNCTIONS *****************************************/
function getAccessToken(){
var request = new Script.Util.HttpRequest('https://auth.exacttargetapis.com/v1/requestToken');
request.method = "POST";
request.contentType = 'application/json';
request.postData = '{ "clientId": "'+ clientID + '", "clientSecret" : "' + clientSECRET + '"}';

response = request.send();

var json = Platform.Function.ParseJSON(''+response.content);

if (!json.accessToken) {
    exit();
}
return json.accessToken;
}  
   function updateDebugLog(textToAdd, activateDEBUG, eraseContentBlock){
// Retrieving old content
if(Boolean(activateDEBUG) && activateDEBUG){
    var request = new Script.Util.HttpRequest('https://www.exacttargetapis.com/asset/v1/content/assets/' + debugId + '?access_token=' + jsonACCESSTOKEN);
    request.method = "GET";
    request.contentType = 'application/json; charset=utf-8';
    response = request.send();
    // adding new content and updating
    var existingBody = '';
    if(eraseContentBlock != true ) existingBody = Platform.Function.ParseJSON('' + response.content).content + "<br/>";
    var newContent = existingBody + textToAdd ;
    request.method = "PATCH";
    request.postData = '{ "content": "' + newContent + '" }';
    response = request.send();
}
}   
function updateEmail(newContent){
// Retrieving old content
    var request = new Script.Util.HttpRequest('https://www.exacttargetapis.com/asset/v1/content/assets/' + emailid + '?access_token=' + jsonACCESSTOKEN);
    request.method = "GET";
    request.contentType = 'application/json; charset=utf-8';
    response = request.send();
    // adding new content and updating
    var existingBody = '';
    request.method = "PATCH";
    request.postData = '{ "content": "' + newContent + '" }';
    response = request.send();
}
/************* STARTING EXECUTION **********************/
var response;
var clientID = 'XXXXXXXXXXXXXXXXXXXXXXXXX'
var clientSECRET = 'YYYYYYYYYYYYYYYYYYYYYYYYY';
var jsonACCESSTOKEN = getAccessToken();
// Debug Content Area
var debugId = 383362;
var emailid = 397405;
var startDEBUG = true;
var eraseDEBUGeveryTime = false;
//var newCountent = Platform.Function.ContentBlockByKey("f716faaa-8170-4940-b1af-c84ee1f5862a")
//var newContent = Variable.GetValue('@html');
var newContent = ('THIS WAS MODIFIED');
// Configuration Values
var categoryId= 1433833;
updateDebugLog("Execution started at " + DateTime.SystemDateToLocalDate(Now()), true, true);
updateEmail(newContent);
updateDebugLog("Execution finished at " + DateTime.SystemDateToLocalDate(Now()), true, false);
</script>
THE EMAIL IS NOW UPDATED

This is the code I used. By writing directly the text, the script works fine, but if I try to get any HTML Code it will not work anymore. Anyone has a suggestion?

Thanks a lot.

Best Regards Massimo

1
0

You could try the function TreatAsContent to treat content with code differently than just contentblockbykey. See more info here: https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-programmatic-content.meta/mc-programmatic-content/treatascontent.htm

1
  • Thank you so much for the answer. I tried as well but it's not working. I figured out that the problem is related to the HTML. The script doesn't allow me to insert any content HTML with ". – M.Giorgio Dec 8 '20 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.