Skip to content

Instantly share code, notes, and snippets.

@MamaiRachid
Last active December 29, 2020 23:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MamaiRachid/bc66143dd3430d8d093e71e375a04bfb to your computer and use it in GitHub Desktop.
Save MamaiRachid/bc66143dd3430d8d093e71e375a04bfb to your computer and use it in GitHub Desktop.
%%[
SET @MID = '50000XXXX'
SET @apiCreds = LOOKUP('REST_Credentials', 'apiCreds', 'MID', @MID)
SET @apiCreds = DecryptSymmetric(@apiCreds, 'aes', 'INT_PWD', @null, 'INT_SALT', @null, 'INT_IV', @null)
]%%
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://cloud.actu.lamutuellegenerale.fr/salesforce-lightning-design-system.min.css" />
<style>
html {background-color: white}
</style>
</head>
<body>
<!-- Header Start -->
<div class="slds-m-around_large">
<div class="slds-page-header">
<div class="slds-page-header__row">
<div class="slds-page-header__col-title">
<div class="slds-media">
<div class="slds-media__figure">
<span class="slds-icon_container slds-icon-standard-opportunity" title="opportunity">
<svg class="slds-icon slds-page-header__icon" aria-hidden="true">
<use xlink:href="/assets/icons/standard-sprite/svg/symbols.svg#opportunity"></use>
</svg>
<span class="slds-assistive-text">Automation</span>
</span>
</div>
<div class="slds-media__body">
<div class="slds-page-header__name">
<div class="slds-page-header__name-title">
<h1>
<span class="slds-page-header__title slds-truncate">Automations Schedules Listing</span>
</h1>
</div>
</div>
<p class="slds-page-header__name-meta">All your automations schedules in one page</p>
</div>
</div>
</div>
</div>
</div>
<!-- Header End -->
<!-- Table headers -->
<table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_striped slds-table_col-bordered slds-max-medium-table_stacked-horizontal slds-table_fixed-layout">
<thead>
<tr class="slds-line-height_reset">
<th class="" scope="col">
<div class="slds-truncate" title="Automation Name">Automation Name</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Description">Description</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Status">Status</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Schedule Frequency">Schedule Frequency</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Schedule Days">Schedule Days</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Schedule Hours">Schedule Hours</div>
</th>
</tr>
</thead>
<tbody>
<!-- SSJS Script start-->
<script runat="server">
Platform.Load("Core","1.1.1");
/* script execution options */
var clearDataExtension = false;
var automationsCount = 25;
/* Passing apiCreds value from AMPScript to SSJS */
var apiCreds = Variable.GetValue('@apiCreds');
var credentialObj = Platform.Function.ParseJSON(apiCreds);
var clientId = credentialObj.clientId;
var clientSecret = credentialObj.clientSecret;
var authURL = credentialObj.authURL;
var tenantURL = authURL.substring(0,36);
/* Authentification */
var payload = {
client_id: clientId,
client_secret: clientSecret,
grant_type: "client_credentials"
};
var url = authURL + '/v2/token';
var contentType = 'application/json';
var accessToken;
try {
var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));
if(accessTokenRequest.StatusCode == 200) {
var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
accessToken = tokenResponse.access_token;
}
} catch (error) {
Write(Stringify(error));
}
/* Clear data extension before populating new data */
if(clearDataExtension) var DERows = Platform.Function.DeleteData('AUTOMATIONS_SCHEDULE',['secret'],[1]);
/* Getting automations */
url = tenantURL + ".rest.marketingcloudapis.com/legacy/v1/beta/automations/automation/definition/?$top="+ automationsCount +"&$skip=0&$sort=lastRunTime%20desc";
var headerNames = ["Authorization"];
var headerValues = ["Bearer " + accessToken];
var automations, automationContent, automation, automationScheduleObject, automationDescription;
var rows, scheduleSplit, scheduleFrequency, endDate, scheduleInterval, scheduleDays, hoursSchedule, scheduleFrequencySplitted;
try {
automations = HTTP.Get(url, headerNames, headerValues);
automationContent = Platform.Function.ParseJSON(Platform.Function.ParseJSON(Stringify(automations)).Content);
// loop through all results
if(automationContent.entry.length > 0 && automationContent.entry.length !== null) {
for (i = 0; i < automationContent.entry.length; i++) {
automationDescription = automationContent.entry[i].description;
/* Getting schedule */
if(Platform.Function.ParseJSON(automationContent.entry[i].id) !== "" && Platform.Function.ParseJSON(automationContent.entry[i].id) !== null) {
url = tenantURL + ".rest.marketingcloudapis.com/automation/v1/automations/" + Platform.Function.ParseJSON(automationContent.entry[i].id);
headerNames = ["Authorization"];
headerValues = ["Bearer " + accessToken];
try {
automation = HTTP.Get(url, headerNames, headerValues);
automationScheduleObject = Platform.Function.ParseJSON(Platform.Function.ParseJSON(Stringify(automation)).Content);
if(typeof automationScheduleObject.schedule.icalRecur !== "undefined" && automationScheduleObject.schedule.scheduleStatus === "active"){
// Split the schedule object to different columns
scheduleSplit = automationScheduleObject.schedule.icalRecur.split(';');
if(typeof scheduleSplit[0] !== 'undefined') scheduleFrequency = scheduleSplit[0];
if(typeof scheduleSplit[2] !== 'undefined') scheduleInterval = scheduleSplit[2];
/* When automation scheduled to run everyday, replace empty by Everyday */
scheduleDays = (typeof scheduleSplit[3] !== 'undefined' ? scheduleSplit[3].split("=")[1] : 'Everyday');
/* Get scheduletime on a Date object to manipulate hours */
var scheduleToDate = new Date(automationScheduleObject.schedule.scheduledTime);
var startingHour = scheduleToDate.getHours();
var startingMinutes = scheduleToDate.getMinutes();
scheduleFrequencySplitted = scheduleFrequency.split('=')[1];
/* When frequency is HOURLY, get all daily schedules by adding the interval to the scheduletime */
if(scheduleFrequencySplitted === 'HOURLY') {
var hoursInterval = scheduleInterval.split('=');
var initialInterval = parseInt(hoursInterval[1]);
var hoursNumber = 24/initialInterval;
hoursSchedule = "";
for(j = 0; j < hoursNumber; j++){
hoursSchedule += scheduleToDate.getHours() + ":" + startingMinutes +" / ";
scheduleToDate.setHours(scheduleToDate.getHours()+initialInterval);
}
} else {
hoursSchedule = scheduleToDate.getHours() + ":"+ startingMinutes +" / ";
}
// Writing table elements
Write('<tr class="slds-hint-parent"><th data-label="Automation Name" scope="row"><div class="slds-truncate" title='+automationContent.entry[i].name+'>'+ automationContent.entry[i].name +'</div></td>');
Write('<th data-label="Description" scope="row"><div class="slds-truncate" title="'+automationDescription+'" >'+ automationDescription +'</div></td>');
Write('<th data-label="Status" scope="row"><div class="slds-truncate" title="'+automationScheduleObject.schedule.scheduleStatus+'" >'+ automationScheduleObject.schedule.scheduleStatus +'</div></td>');
Write('<th data-label="Schedule Frequency" scope="row"><div class="slds-truncate" title="'+scheduleFrequencySplitted+'" >'+ scheduleFrequencySplitted +'</div></td>');
Write('<th data-label="Schedule Days" scope="row"><div class="slds-truncate" title="'+scheduleDays+'" >'+ scheduleDays +'</div></td>');
Write('<th data-label="Schedule Hours" scope="row"><div class="slds-truncate" title="'+hoursSchedule+'" >'+ hoursSchedule +'</div></td>');
Write('</tr>');
/* Write rows to a Data Extension */
rows = Platform.Function.UpsertData("AUTOMATIONS_SCHEDULE",["Name"],[automationContent.entry[i].name]
,["schedule","scheduleStatus","scheduledTime","scheduleFrequency",
"scheduleInterval", "scheduleDays", "scheduleHours", "description"],
[automationScheduleObject.schedule.icalRecur,
automationScheduleObject.schedule.scheduleStatus,
automationScheduleObject.schedule.scheduledTime,
scheduleFrequency, scheduleInterval, scheduleDays,
hoursSchedule, automationDescription]);
}
} catch (e) {
e = Stringify(e).replace(/[\n\r]/g, '')
Write(e);
}
}
}
}
} catch (e) {
e = Stringify(e).replace(/[\n\r]/g, '')
Write(e);
}
</script>
</tbody>
</table>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment