Last active
December 3, 2020 05:09
-
-
Save MamaiRachid/09ebd7c3b90adbe1bb229f61b0df75a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 +" / "; | |
} | |
/* Write rows to a Data Extension */ | |
rows = Platform.Function.UpsertData("AUTOMATIONS_SCHEDULE",["Name"],[automationContent.entry[i].name] | |
,["schedule","scheduleStatus","scheduledTime","scheduleFrequency", | |
"scheduleInterval", "scheduleDays", "hoursSchedule"], | |
[automationScheduleObject.schedule.icalRecur, | |
automationScheduleObject.schedule.scheduleStatus, | |
automationScheduleObject.schedule.scheduledTime, | |
scheduleFrequency, scheduleInterval, scheduleDays, | |
hoursSchedule]); | |
} | |
} catch (e) { | |
e = Stringify(e).replace(/[\n\r]/g, '') | |
Write(e); | |
} | |
} | |
} | |
} | |
} catch (e) { | |
e = Stringify(e).replace(/[\n\r]/g, '') | |
Write(e); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment