Execute Salesforce Apex and Deploy Components across multiple orgs
Jun 18, 2026
Show More Show Less
View Video Transcript
0:08
Hello everyone. In this video we are
0:11
going to see how to automate
0:14
anonymous apex execution and also
0:18
deployment
0:20
in VS code.
0:24
The use case what I have today is
0:30
I have metadata.
0:34
For example, uh for this demo, I created
0:37
three Apex classes
0:40
and also I have a script an Apex script
0:44
which I have to execute in multiple
0:47
sandboxes and also I have to deploy
0:51
uh the classes into multiple sandboxes.
0:58
If I have to do this then what typically
1:02
we will do is we will create a project
1:05
in VS code and uh we will add the
1:09
metadata to it. We will execute the apex
1:12
code in developer console or any other u
1:17
uh u ID. For example, even in cursor we
1:21
can run uh anonymous apex and also in VS
1:24
code we can do the same.
1:26
and it contains multiple uh manual
1:28
steps. I have to rightclick the
1:30
components and then I have to click
1:33
deploy source to the org so that it can
1:36
deploy it. But I wanted to automate
1:40
this.
1:41
For that what I have done is I have made
1:44
use of tasks.json.
1:48
I'm using cursor IDE here. You can also
1:51
make use of VS code for this. In the
1:54
tasks.json
1:58
I have declared three tasks. One deploy
2:02
to org. It will deploy all the
2:06
components that are available under
2:07
force hyphen app. Under that we have
2:11
main. Under main we have default. Under
2:13
default we have classes. And also under
2:16
default if you have uh lightning web
2:19
component, lightning aura component,
2:20
visual force pages uh like that if you
2:23
have multiple uh metadata all those
2:26
metadata will be deployed as part of
2:28
this one. Next I I have created run apex
2:32
uh um so in order to run uh the apex
2:37
code I'm using sf apex run method for
2:39
deploy I'm using sf project deploy.
2:44
Next I created the third task which is
2:46
the default task. So whenever I run this
2:48
task only this task will be uh this task
2:51
will run and it has dependencies on run
2:55
Apex initial load and then deploy to or
3:00
here one important thing to note is
3:02
depend depends order. Here you can enter
3:06
sequence or you can enter parallel. If
3:09
you enter sequence then it will wait for
3:12
this particular first task to be
3:14
completed. Once this is completed then
3:17
it will execute the next task. Since I
3:20
have set parallel it will open two
3:22
terminals and then it will run run Apex
3:26
initial load Apex anonymous uh Apex
3:28
execution and in the next terminal it
3:31
will deploy the components that are
3:33
available under default.
3:36
So here uh I have a training org. I can
3:41
see there are no deployments recently,
3:43
no cases, only one contact is here and
3:46
there are no opportunities.
3:50
In the initial loadapex uh script what I
3:55
have done is I'm creating an account
3:57
record, contact record, case record and
3:59
an opportunity record.
4:03
Next it has to deploy all these three
4:05
apex classes. So I have authenticated
4:09
and then uh my org is connected to this
4:12
uh cursor ID. Now now what I have to do
4:15
is
4:19
if you are using Mac then use command
4:21
shiftp
4:24
and uh search for tasks run build task.
4:28
Once you execute it, it will run the
4:31
tasks.json. In the tasks dojson, I have
4:34
three tasks. It will run the default
4:36
task which will having dependency on the
4:39
other two tasks. And I have set it as
4:41
parallel. So, it will open two terminals
4:43
and then it will execute anonymous Apex
4:45
window uh sorry anonymous Apex code and
4:48
also it will deploy all the components.
4:54
So it opened uh two terminals
4:59
in one terminal.
5:03
It executed the
5:05
an Apex in an anonymous window. Next
5:09
here in this terminal it deployed
5:13
the Salesforce metadata. So now let's go
5:15
back to the Salesforce arc.
5:18
I'm able to see the opportunity.
5:23
I am able to see the account record.
5:28
I'm able to see the contact record.
5:33
It should also create a case record. So
5:35
it created the case record and it also
5:38
deployed three Apex classes
5:40
successfully.
5:43
So if you have repeated tasks then you
5:46
can make use of uh tasks.json JSON and
5:49
then using run tasks, you should be able
5:52
to automate all your repetitive uh
5:55
tasks.
6:02
I hope it was helpful. Please check the
6:04
video description. In the video
6:06
description, I have shared my blog post.
6:09
From the blog post, you should be able
6:10
to get the sample tasks.json
6:14
for your reference.
6:23
Thank you for watching.
#Internet Software
#Science
