Salesforce Trailhead - Scheduled Apex - Challenge

Sharing buttons:


okay it's time to do our hands-on

challenge for the schedule a pax so this

is the challenge create an apex class

that uses scheduled effects to update

lead records so create an apex class

called daily lead processor I'm going to

copy that go to my playground go to my

developer console close the previous

ones from the previous trail file new a

pax class daily lead processor that's

the class name and then the execute

method must find the first 200 leads

with a blank lead source and then update

them with the lead source value of dream

force all right so let's copy the

schedulable sample code from this trail

which is this one I'm going to copy that

go down again to our challenge go to our

developer console so daily lead

processor I'm going to paste and work

from the shell so it's going to be daily

lead processor it implements a

schedulable interface so global void

execute schedule a ball and then list

off lead we're going to change this

let's call this leads then select ID

from leads from lead where lead source

equals blank or Nam okay

then name it 200

we just want 200 of that so I'm gonna

take this out so let's actually check

what's the problem so task utyos' well

we don't have this right so what we want

is to actually update this so I'm going

to loop through the through the list

here so we have a list from here and

then we're gonna look for lead right now

leads we're gonna change l dot lead lead

source equals dream force is it dream

force let me take a look

it's called dream force with a lower

case their dream force like that so I'm

going to make a new update leads okay

another list same thing lead leads to

update equals new list of lead

instantiate that so now I want to add

this list to update which that that add

now okay and this is outside the loop

here this is Ted and now - I'm just

going to update

houston we have a problem

variable does not exist list to update

or lead to update my bad

and then we update it in one go leads to

update save that so this is the

scheduler block class is called daily

live processor implement schedulable and

then this is the execute method from

this to this basically we do a circle

from lead select ID from lead wear lead

source equals no or blank limit just 200

right and we look through it we put each

individual record to a new list and then

we we update the leads to update here

and that's pretty much it so if I flip

back to the trail okay so execute must

find the first 200 leads with a blank

lead source field and update them with

the lead source value of team force okay

so next is the daily process or test

okay okay I'm going to copy this head

back to my developer console

new ethics class daily lead processor

test so I'm going to go back to our

trail and copy the test code sample so

I'm going to copy that

so daily lead processor test makes

basically I'm going to paste that and

change this to daily lead process or

test okay so we have a cron expression

let's skip that for now this is the test

scheduled job so basically I just want

to create 200 leads

okay so list of lead leads right list of

lead and then I'm not going to need a

date here

so 0 to 200 now so it's a lead and I'm

going to name it L new lead and let's

let's take a look at a lead object on

here if I go to my setup and I go to my

lead or Jack oops

what happens that dude Center

so fields and relationship I think

organization is required right for a

lead let me see your company company is

required probably so I'm going to do

company and then the name of the lead

probably first name last name right so

I'm gonna do first name first plus I

last name equals last name and then

company equals the Inc okay and then

leads leads that add now insert leads so

we basically are creating 200 leads in

here I just want to make sure my leads

required fields are filled in because we

don't use lead so much in our

organization so if I go to leads here

this way no make sure was required to

create a lead so I'm going to make news

on it check the required field so the

name last name and company right I'm

right yeah company and then we're good

to go so we have company in there in

certainly so we have our test data now

get the IDS of the opportunities we just

insert that we do not need this we just

need the leads for now so I'm going to

take this out

we don't need the map wanna take that

out so test start test and then remind

of the owners this is Daley lead

processor right and then verify the

scheduled job has not run yet oh this is

to actually verified well we don't need

that just going to take this out I'm

going to take this out make this simpler

so we just schedule it

we just schedule tear and then task

exist before a job run I'm going to take

this out to make it simpler

now that the scheduled job has executed

check that our tasks what was created

check that we have we have 200 leads

with Dreamforce

okay select ID from leads where

lead source equals lead source equals

what dream force and first or last name

her company equals because this is our

task company right the ink made the task

is good and then we want 200 yeah just

happen here or here I want the size

equals our t dot size so not up i didäôt

size I want I want the size

well this is just 200 right we know it's

200 is supposed to be 200 and then yeah

so I'll T equals select ID this is not


this is Leeds

so I'm gonna make this list Chuck leads

it this is lead right lead chocolates

equals new list of lead instantiate that

and then on our chocolates

yeah chocolates which is disk guy here

so check listed size has to be equal to

hundred okay or we can just say leads

were not created

oh yeah line 30 illegal assignment from

list to lead oh my bad

is leads list of lead Chuck leads

check check check check okay that should

be good now let's see now invalid type

schema leads 31 okay this is actually

lead alright

select ID from lead is not leads where

early source the cost reinforce and

Company Inc very good so what's what's

going on here

basically we create 200 leads right we

create 200 leads and then we run the

task and then we schedule de they will

lead processor right and then we assert

that with checking if the start if the

lead source now has been changed from

blank which we not define here to

Dreamforce because after this daily lead

processor is has run the lead source

should change to c2 Dreamforce right so

let's go back to the tail and see what


need to do next so in a task last insert

two-handed scheduled electron on a test

and that lead Arrakis updated correctly

it has to come and I run the test I

think we're done here

so I'm going to run the test and we

should be good to go we should be able

to see or to say by the bing bada boom

but let's see oh I think we have a green

Bing boom BAM boom so I'm gonna go sort

my percent and daily process or all

green we have a winner

okay so this is the daily read processor

it's pretty straightforward and this is

the test which I have discussed with you

and now let's gather the 500 points

what are you boom bada bing bada boom so

next we will check the monitor async

apex ah see you on the next video hit

that subscribe button and explore new

trailhead grounds and learn to implement

the most useful and popular apps on the

Salesforce app exchange and do yourself

a favor like this video and you'll be

surprised on how much more you

understand when watching this same video

after lighting it

don't take my word watch this one more

time after you like the video and see it

for yourself

bada bing bada boom