(upbeat music)
- Hi everyone, so for the last few weeks,
I have been working on this
really cool Salesforce labs app
that calls up to the tooling API,
and gets an org's security health check data,
then it sends to a central org for analysis.
It's really important to us as part of the app
that the scheduling process
is simple for sending that data.
And we want the user
to have full control over that.
And so we thought it'd be a great use case to try
and use scheduled flows
that then call an invocable method
for us to process data.
Now, unfortunately,
we learned that scheduled flows run as
the automated process
user who doesn't have a session ID.
And so we thought we might just write
a little Lightning web component
that could schedule the jobs, manage the jobs
and maybe allow the user to reschedule them,
and it ended up
being much easier than we thought.
So popping over to our IDE, we can see here that
we have an apex class
for managing the job scheduling
and interaction with our component.
There are quite a few classes in here
for managing the process, testing that first job
to make sure that it works.
But what we're really gonna focus
on today is scheduling that first job.
Here you can see that
the scheduled job method accept
the parameters of a cron string for scheduling,
and the job name
that has been set on the component.
And here you can see the
delete scheduled job method
just has an input of cron job name.
And we're gonna run a system dot abort
job after we go and query for
that cron trigger ID based on the name.
Looking at our web component, you can see here
we're using a lightning input time field to go ahead
and get the schedule of that cron job.
Now, we only need
the hour and minute at the moment
because we're scheduling
this job on a daily basis.
Now in the controller,
you can see here in the connected callback,
we're gonna go ahead
and get the currently scheduled cron job,
so that we can see if it's already scheduled,
and if so, what time it's scheduled for.
We're gonna take that result
and we're gonna make sure that
we can read that
as a time for our Lightning input field.
In addition to that, we also want to be able to
run the first job to test it out from the method.
And we want to then go back
and pull for that job status
to make sure it's completed.
But more importantly, before we can do anything,
we need to get time from the input field.
So on change of the time field,
we have the handle time change method,
that's gonna be tracking changes in that field.
So taking a look at our component,
we have two sections at the moment,
one for scheduling the job
and one for managing the currently scheduled job.
As you can see here on our time field,
if we change that value,
it's gonna log the time for us.
And you can see there,
it's just logging out just a standard time format.
But we want to do is
be able to take that time format
and convert that into a cron string
that is readable by the system.
So let's take a look at that string.
And how we can turn that time into a cron string
for us to then pass back to apex.
The format we're looking for,
are the seconds, the minutes, the hour,
and then we can
disregard the rest for the time being,
because this will give us our daily schedule.
So in order to get this into the format we need,
first let's assign that time
to a variable that we can use.
And then let's convert that
into array by splitting the array by the colon.
We can see that that will give us our minutes,
that will give us our hour.
And that will give us our seconds.
Now let's just go ahead and log that
and see what that looks like in our component.
Back in the component,
you can see if we change the time,
we're getting our timestamp,
and then we're getting our array.
And so it's working perfectly.
We've got our hours and we've got our minutes
that we can start utilizing within the component.
Normally, if you wanted to
isolate an element of the array,
you could just let hour equal index zero
of the time array
and the minutes could equal index one.
There's actually an easier way of doing this though.
By using destructuring.
Now destructuring works
by doing correctly what it says on the ten,
and destructuring
that array into a set of properties.
And just to find your properties within
the square brackets,
we have hour, minutes, seconds,
we'll just say equals the array.
And it will literally break
the array down by index into those properties.
And so if we just log out our hour,
and our minute, and our seconds,
let's go and take a look
at what that output looks like.
And so as we change the time in our input field,
we can see in the console,
that the array is done what we thought it would,
and it's destructured itself into those properties.
Now all we need to do is
use those properties to generate a cron string.
And to do this,
we're going to use an ESX feature
called template literals.
Now, template literals allow you to
generate strings that
are constructed using expressions.
And so let's generate our final cron string here.
Now it's super important to note that
our template literal is enclosed by back ticks,
not single quotes.
I've made that mistake
many times wondering why it's not working.
But otherwise,
it's like we're writing just a string.
And so we've got our back ticks.
Let's add in our seconds, so just a zero
because we know that
that's going to remain that way.
Then, to get our property in the string,
we just say dollar sign, curly brace,
and then our property name,
so minutes, and then we'll do the same for hours.
So dollar sign curly brace hours.
And then we'll go ahead
and add in the rest of our cron string.
And let's go ahead and just log that out
and go and check out the results in Salesforce.
And so back in Salesforce, you can see
as we changed the time that's brilliant,
generates the cron string for us right now.
And so we can now take that.
So I've gone ahead and assigned
that to the property
that's picked up by the Job Scheduler.
And so let's go ahead
and try and schedule our job now.
And as you can see,
that's come back successful, it's then gone
and queried the database to make sure that
that has been scheduled.
And so now we can delete the job,
it will clear that out and abort the job.
And at the same time,
we can reschedule it for a different time.
Now the last thing
you always wanna do is go in and see if you can
simplify the code a bit more.
And you can see here
I've taken out some of the log statements,
I've gotten rid of some of
the arbitrary assignments that we were doing,
and we've made it much cleaner.
Now to get a peek at what
the final product looks like.
This is our multi-org
security summary app that we've built.
And if we go into the setup config,
we can click on the Manage Sync button.
It's a very similar UI where we can see
the symptoms already scheduled,
we can reschedule it,
and then we can select a new time
to then reschedule that job.
And so the admin or the user
always has access to that Scheduler.
Thank you for watching this video everyone.
Hope you found it as informative as I did.
If you have any comments or questions,
please feel free
to reach out to me on twitter at StephanWCG.
And all the code from today's
quick take is available on GitHub. Thanks.
Thanks for watching! If you learned something, be sure to like this video.
If you want to get more content like this pushed directly to you
click subscribe and that bell to get notifications