Practice Exams:

CRT-450 Salesforce Certified Platform Developer – Process Automation and Logic – 38%

  1. 3.7- SOQL, SOSL and DML – Part 4 – DML Demo

First of all, I will create an account so it will be account acceleration. And then I will specify the mandatory field. So ACC name equals test account. And then I will use the DML statement insert. So this is the stand alone statement and let’s see what would happen. So I did create an account and then I specified the field name to be test account. And then I did insert this by using the standalone DML statement insert. So if I click on execute and then I go to my salesforce. org, I need to check that this account was created. So this is the list of accounts. If I click on all accounts and on Go, I can see that the test account was created. I can also create another account. So test account two and execute f five. There you go. I can also delete this by using a sockwall query to retrieve this and then performing a delete DML statement on the result. I can also insert a list of accounts.

So let me create a list of accounts. Thank you. So what I did, I created a list of accounts called ACC list. I then created a for loop that loops 20 time and each time it creates an account called ACC with this name test account plus I also test account zero, test account one up to test account 19. And then I added this ACC to the list ACC list. And then I will use the DML operator insert on the whole list. So let’s see what will happen. Now, I will highlight these lines of code and I will execute that. So let me go back to salesforce and let me see if these accounts were created or not. There you go. So zero, one, oneone until 19. So, this is the list of accounts that I have added using the DMR operator insert. Now let me try to change this. Let me insert one faulty account record.

To do that, I will use the F statement. So if I is, let’s say ten. So what I did, I have added an F statement inside the for loop. So if I stand create the account but do not specify the name field and insert this account into the list. So else it will be this. So there you go. I have a list of account called acclist and then I have a loop that loops 20 times. And if I attend do this, I’ll do that. So I would have one faulty account record and I would have 19 good ones. Let me see what will happen. Now. Let me remove the first lines and I will try to execute these. What will happen?

I will have an exception. Why? Because there was an error with one of the records. Now, if I replace this with the database method, let’s see what will happen. So I will use database that insert and I will give the account list and I will specify the old or none to be false. So let’s see what will happen. Now, as you can see, the account insertion succeeded. So going back to Salesforce and hitting on Refresh, I can see now that I have duplicate accounts which means that the second insertion succeeded. But if I go to account number ten, I have only one. Why? Because this was inserted by using the send alone DMs statement in the first example. But I have twice 1112, 13 and so on, which means that this succeeded. So this is the use of the database that insert a method. I used the all or none to be false. So this will omit this faulty record and it will insert the Nike records. Now, let’s try to delete these. How to do that? First of all, we need to build a so called query.

So it will be a list of account. So I will call it ACC to delete equal. It will be a so called query. So select ID from account where name like test account percentage. So this so called query will get me all the accounts that start with test account and then I will perform a delete DML statement on the ACC to delete list of account. Let’s see what would happen. Now, going back to Salesforce, let me hit refresh and you can see there is no test accounts. So all of the test accounts were deleted using this DML statement. So this is briefly the use of the standalone DML statements and the database method. As we have seen, there is an advantage of using the DML method which is the old or non-parameter. Let me go back to the slides. So, as a summary for this part, we have talked about DML or the data manipulation language, where and when we can write DML, what are the different statements that we can use with DML? What’s the difference between the standalone DML statements and the database methods? And finally, what is the return type of the database method and how we can use it? And finally, as a summary for this whole lecture, we had many parts on this lecture. The first parts were mainly about a sock well, the Salesforce object query language. And then we have talked about the SOSL, the Salesforce object search language. And finally we have talked about the DML, the data manipulation language. And finally, as usual, thanks for watching.

  1. 3.8- Apex Triggers

In this lecture, we’ll talk about Apex triggers. What are they? When can we use them? Where can we type them? And what are the different rules and the syntax of an Apex trigger? First of all, what are Apex triggers? Apex triggers are blocks of Apex code. They are chunk of lines of Apex code that are executed under certain conditions, just like a workflow rule or a process. And Apex trigger a tie to exactly one object and it waits for an event to happen on this object to execute its actions. So let me give you an example. In workflow rules or in a processes, we first define the object and then we define what events should be mapped before the actions of this process are executed. And then we define the actions to be executed. This is exactly the same thing. But this is without any fancy graphical user interface. Everything in the code. This is number one. Number two, there is no limit. We don’t have any limit here if the action can be coded. So it can be done by an Apex trigger. Let me give you some example of triggers. Event trigger events are like before the insertion of a record, before the update of a record, after the insertion of a record, after the update, and so on. Triggers can be defined for top level standard objects like account, like opportunity, like contact and so on.

They can be defined for any custom object and they can be defined for some standard child object. And finally, triggers are active by default. I don’t need to activate them like workload rules, like processes. Whenever I finish coding a trigger, it becomes active by default. Now, where can I write apex triggers? I can write Apex triggers in any IDE, like the fault IDE on Eclipse, like the developer console. And I can also write them in the setup menu. Now, when to use Apex triggers? We use Apex triggers to perform tasks that cannot be done by using the point and click tools and the Salesforce user interface. So if the action that I want to be done cannot be performed by using any point and click tool, like any automation tool, like the workflow rules, like the Process builder, like the Flows and so on, if it cannot be done by any of these, I go to the Apex trigger. This is a very important point. First of all, we need to make sure that our requirement cannot be performed by any automation tool and the graphical user interface. And then if this is the case, we revert to the Apex triggers. Using triggers, we can combine our Apex code with any sockwell statement to retrieve any S object in our salesforce. org. And we can also use any DML statement to change and to manipulate this data. This is the real power of triggers. It’s not only a simple Apex code, it’s simple Apex code plus any socket statement, plus any DML statement. So this is the real power of Apex triggers. Now, what’s the syntax of Apex triggers? The syntax of a trigger definition is different from a class definition.

So a trigger definition starts with the keyword trigger, and then it is followed by the name of the trigger. So we can give any name, and then we type the keyword on, and after that, we type the object where we want this trigger to be tied. And finally we type the condition under which this trigger will fire. So you can see here a trigger, and then the trigger name, and then the on keyword, and then the name of the object, and then between parentheses, the event or the events in which this trigger will fire. This is an example. We have a trigger called Opportunity Trigger on the Opportunity object, and the event is the before insert event. And then on the body of the trigger, we type our Apex code and we can type any Socalled statement and any DML statement there. Now, what are the different events that we can use with the trigger? We can have more than one event to define on each Apex trigger to have more than one event, we separate them by a comma. These are the different events we have before insert, before update, before delete, and we have after insert, after update, after delete, and after undelete. To remember this, I used the UID, so everything before is UID, so U is for the update, I for the insert, and D for the delete. And anything after is UUID.

So it’s the same as the UID plus the undelete. Now what’s the difference between before triggers and after triggers? Before triggers will use any before events, like before insert, before delete, and before update. And after triggers will use any after event, like after insert, after update, after delete, and after undelete. But what’s the real difference between these two? Well, before triggers will execute the actions before saving the records to the database, which means that we can modify these records before being saved. And we don’t need to explicitly state a DMI statement here. After triggers are executed after the records are saved to the database, which means that we need to explicitly state a DMR statement. Using before triggers, we cannot access fields that are set automatically. We cannot access the value of the ID. Why? Because the record is not saved yet.

There is no ID yet. But using the after trigger, the ID is set and we can access it. Before triggers can be used to update and validate the same record that initiated the trigger before it is saved. But after triggers can be used to create other records that are tied to the record that initiated the trigger. Why? Because we can access the ID field. Let me give you an example. With before trigger, we can set a field called Maximum Salary and the Position Object, and we can set this to a certain value if it is empty when the position record is created. So this is using the before a trigger. But using the after trigger we can create a record that is tied to the record that initiated the trigger. Why? Because with after triggers we have access to the ID field so we can create another record and we can link this record to the initial record because we have access to the ID field.

But this is not the case when using the before triggers. So, to conclude, we use the before triggers when you want to change the same object, the same records, and we don’t need to state, we don’t need to type a DML statement. And we use after triggers when we need to create other records, and we need to link these new records to the record that initiated that trigger. Why? Because we need to have access to the ID field. We need to know the ID field of the record that initiated the trigger because we need to link a new record to this record. Up until now we have learned that triggers will fire when events happen and they will fire on one record or more than one record. But how can we access these? How can we access this list of records? To do that, salesforce offers something called context variable. Context variables are variables that are set by salesforce and that are used in triggers. Let me give you an example. We have trigger new. This is a context variable that represents the list of records that initiated this trigger. So if you want to access what are the records that initiated this trigger, we use the variable trigger new and we can also use other variables like trigger old.

Trigger old will provide the old version of records before they were updated and an update trigger. So if let’s say we are using an update trigger and we want to access the values of the field before being updated, we use the trigger that old. These are the whole context variables. First of all, we have triggered a new this will return a list of the new versions of the S object records and they are available and insert and update and undelete and the records can only be modified n before triggers. Why? Because N before triggers with a drop save yet we also have the trigger new map. This will return a map of IDs to the new versions of the S object records. So instead of a list it will be a map and this is available and before update, after insert, after update and after undeleted trigger events we also have the trigger old variable. This will return a list of the old versions of the S object records and it is read only and it is available on the update and delete triggers.

And then we have the trigger that old map. This will return a map of IDs to the old versions of the S object records and this is available and the update and delete triggers only. And finally we have the trigger that size. This is available in any trigger event invocation and it will return the total number of records that initiated this trigger. On top of that we have other variables. For example we have the is executing variable. This will return true or false true if the current context of the Apex code is indeed a trigger. So the source of the execution is a trigger and it’s not a VisualForce page nor a web service nor an execute anonymous call. Also we have the as insert. The S answer will return true if this trigger was fired due to an insert operation. As update will be true if this trigger was fired due to an update operation and same thing for the S and delete as before and is after. So note by using these variables we can use if and all of that to specify the action that will be fired in this trigger. These are some examples of triggers. The first one is the simplest one.

We start building it by using the keyword trigger and then we follow this by the name of the trigger. In this case it will be hello world trigger and then we type the keyword on and then we type the object, in this case it will be account and then we type between parentheses the event. In this case it will be before insert and after that between curly brackets we type the code that we want executed when this trigger is fired. So in this case it will be just a system that debug hello world. So this trigger will only do system debug and note that it will only act on one record. We did not specify any trigger that knew so we did not specify any list. So this will only act on one record and what it will do, it will only add hello world to the debug log. In the second example we are using the same name but now we are using the trigger that new context variable. Note that this is a list of accounts. So it’s a list of all the accounts that are used to fire this trigger.

And we are using A for each loop to loop through each one of these accounts. And we are using the variable name A to denote one of these accounts. And then in the body of the for loop we are adding a description string to the description field. So what will this trigger do? It will loop through each one of the accounts and trigger that’s new and it will add a new description and the description field. So this is the action of this trigger. And then we have an after insert trigger. This trigger will be also on the account object and the name is task trigger. Note that this is an after insert trigger. Why? Because we need to get the ID of the account. See here we are using the same context variable, a trigger that’s new to loop through each one of the account records. And then we are creating a new task called a task. And then we are linking this task to the ID of the account A. So we need to know the ID of the account. That’s why we are using the after insert. So these are three examples of triggers. The first two use before insert. So we don’t need to state any DML statement. But the third example uses after insert, which means that we need to use a DML statement.

This is also an example. In this example we have more than one event. So the events are separated by commands. And then we are using the context variables to go through each one of these and to specify actions based on the events. So if the trigger that insert is true so if this is an insert trigger and then if this is a before trigger so if it is a before insert, do this. And then if it is an after insert, do that. And also, as we have mentioned, we can use sockwell in our Apex triggers. As we have learned, sock will have many different return types. We can use all of these and our Apex triggers. And also we can use sockwell in our classes. So it’s not only triggers, it triggers and classes. And the sockwall code should be enclosed between two brackets. We knew this from previous textures. We can also use so called variable binding. So we can specify any variable. And then if we want to use this variable inside our so called query, we need to preceded by a column. So this is called variable binding. Moreover, we can use something called trigger exceptions inside out triggers.

Trigger exceptions are mainly used to add restrictions on certain database operation. Let’s say you want to prevent saving a record when certain conditions are met. To do that, you use the trigger exceptions. To prevent saving records and a trigger, you have to call the add error method. And this add error method will prevent saving the record and it will throw an error. The error message can be specified by you. You can specify it in the code. Let me give you an example. If you want to prevent the deletion of an account, if it has at least one opportunity, you can add the add error method and you can enclose this and if statement. So if account has one opportunity and then open the brackets and then call this add error method. This is an example. We have a trigger called account deletion on the account object and the event before delete, this trigger will have a list of accounts called accord. This will fetch all the accounts that have an opportunity and that are part of the trigger. That old. Remember that trigger that old is a list of all the accounts and this trigger and then it will use a for each loop to deny the deletion of these. Let me now go to salesforce and let me show you triggers in action.

  1. 3.8 – Apex Triggers Demo

This is the Apex triggers demo. First of all I will show you how to create Apex triggers and where we can create them. The first place is the setup menu. First thing under develop we have different links. One of the links is Apex triggers. In this link we can only view and edit the existing one. So let me click on it and see, this page allows you to view and modify all the triggers. To create the new triggers, we need to go to the object itself. So let’s go to the object. So in this case, let me go to the account object and there you can see there’s a node called the triggers. We can click on you and we can create our trigger here. Note that automatically we have the account object. But we need to supply the name of the trigger and the event or events. And then we put our code between the cookie brackets. So this is the first place. Now the second place that I will show you is the Force IDE which is on Eclipse. Let me open eclipse. So there you go. This is Eclipse. Open.

And as you can see on the top left we have our we need to first make sure that we are online. How to do that? Just go to the Force and make sure that we are online. If not, just click on Work online. Now let’s see the different folders. Under this we have the source folder and inside it we have different folders. One of them is the trigger folder. Here we can find all the triggers of the so if we create a trigger from this Eclipse or from the Force IDE or from the setup menu, it will come under triggers folder. Now how we can create a trigger just right to click new and choose Apex trigger. First thing we need to name our trigger. So it will be hello world trigger. And then we have to choose the object. So in this case it will be Account and the Torchion. So in this case it will be the latest one. And then we can specify the event. So it will be a before insert event finish. There you go. We have the first line all set trigger and the name and the account, the object that we have picked and the event. Now let’s specify the code of this record. So this record will just display a simple log and the debug log. So it will be the hello world. So this is the code of the trigger. And let me click on Save or control us.

Now let me go to the salesforce. org and let me go to the triggers link and there we can see our trigger. So this was created by the Eclipse. So this is the second place where we can create triggers. And the most used place is the developer console. Let me open it. And to create a trigger just go to file new and choose trigger. So we can give the name and we can give the object submit. And there you go. So here we can specify the code of this trigger.

So this trigger will do what? It will loop the different account records and this trigger. So for account a so in this case we are using the for each loop to loop on each one of the accounts and this trigger and it will set the description field so a dot description equals to new control s to save and then let’s test these in our salesforce. org. So this is salesforce. Let me go to accounts and let me create a new account. So we’ll call this test account one. And what should happen here?

If I open the logs a new log will be created, it should have the hello word a text and also by using this trigger we should have this as a description and the field of the new account. Why? Because we are mentioning here that before insertion do this. Notice that I did not specify any DML statement. Why? Because this is a before event. So the DML will happen by salesforce. I should not specify it. If it were a after insert I need to specify the DML statement. Now let’s click on save and let’s see what would happen.

First of all, there you go. We have the description field set. This is by using this trigger and if I open this log I can see that I have the hello world string in this log. So we have seen trigger creation from the force IDE and from the developer console and we have tested this trigger by creating a new account. Now let’s do another trigger which is in this case a after trigger.

So filed new trigger, it will be called task trigger and it will be on the account object. This trigger is an after trigger. Why? Because I need to take the ID of the accounts. Because I need to create a task and I need to link it to this account. So I need to have the ID of the account. What I will do, I will create a for each loop. So for account a new I will create a new task and then I will specify the fields that are mandatory. So in this case we can go to salesforce and we can check the mandatory fields or we can open the task object from here file open and task and we can see the different fields. But to know the mandatory fields we have to go to salesforce to check them up. So a task owner so this is the use of the ID field.

So we have owner ID of the account. So notice what we did here. We created a new task and we have filled its field and we have filled its ID fields by using the ID fields of the account that was already saved because this is an after trigger. After that, if we try to run this, we’ll get an error. Why? Because this is an after insert. So this record should be inserted by us. To do that, we’ll use the DML statement insert and let’s save. I have a problem. So let’s see. What I have expecting ismicolon found the rat parentheses. So it will be line item five. So there you go, control s again. So now we have our trigger. Notice that I should not activate any of these. It’s not like process or workflow rules. Once I create this, it will be automatically activated. Now let’s create a new account and let’s see if a task will be linked to this account. So test save. So three triggers will fire. Now the first one that will fill this field and then and the debug log will have the string to the debug log.

And then we have the new task that was created by the third trigger. So now we have three triggers on this account and all of them were fired. Because this is an insert operation. Let me add a new trigger. Also, this trigger will use the Add error method. So it will be account. So what it will do, it will loop through each one of the accounts records. And then if, let’s say the account name is equals to error account, we have to use the add error method. Let’s save this and let’s see what will happen if we add an account called Error account. So I will go to salesforce again and I will click on new account and I will have this account name. Now what will happen? This trigger will prevent the creation of this account and after trigger will not work because we did not save this record. Let’s try to save this and let’s see what would happen. There you go.

So this is the use of the add error method on the record itself. Of course, this is just a very simple example. You can use this in other more complex examples that cannot be achieved by the validation rules. But this is just a small example to show you this method. So this is the briefly apex triggers. We can create them and the developer console, and the setup menu and the force IDE. I advise you to use the developer console because it’s now mature. If not any IDE, it can be the post IDE on Eclipse, it can be Mavens Made and so on. But don’t use the setup menu because it’s very basic. And finally, as a summary in this lecture, you have learned about Apex triggers, about where and when to write them, their syntax, what’s the difference between before and after triggers? What are the different trigger events? What are contacts variables of triggers? And finally, what are trigger exceptions? What’s the add error method? And also we have seen all of these in the demo. And finally, as usual, thanks for watching.