CRT-450 Salesforce Certified Platform Developer – Process Automation and Logic – 38% Part 6
- 3.16- Using Declarative functionality and Apex together – Demo
Okay. So now we will show you how to use Apex with their processes and flows. For that we will open our and then we will use the developer console. So this is where we will create the classes with the vocable method annotation and with the methods that will be shown in the processes and the flows. So let’s go to Setup and let’s open Process Builder. Okay, so let’s now create a process, a simple process on the account object. Let’s call this account process and it will start when a record changes. So we would use this on the account. And it’s not only on the creation. I want this to be on creation and update and to simplify things. We will not add any criteria. Okay? So now, if I go to the actions, you can see that I can add actions. This is the list of the actions that I can add. One of these at the Apex action. So as you can see here, if I have invokeable methods, they will show there. Now let’s go back to the developer console and let’s see. So, this is a class and this class has this method.
And then we have this annotation. Before this method, you can see that we have a label and we have a description and we have a category. So the name of this method is get account names. And then it will take the list of IDs. It will return the list of string. And then what it will do, it will take the list of IDs, it will take this list, it will do a for loop on the accounts of the list of these IDs. And then it will output the names of these accounts. And then it will put them in this list of strings. It’s so simple. Okay. So now, because we have this annotation on the top, you can see that if I go back to the process, you can see here, I have now a list of all the methods and classes that I can call.
So basically, what you see there is what is the label. So, as you can see, we have invokeable method and then we have a label get Account Names. You can see here that I have get account names. Now, if let’s say I remove this line and I save Controllers, then I go back and I refresh. I open the account process again, go to add Actions Apex. As you can see here, I don’t have it because it’s not tagged with this annotation. Now let’s get it back and let’s change this control s and then F Five.
Now you can see that I have this and this list with the new label name. What happens if I don’t add this? So let’s get rid of all of that. Again, this is all optional control X and CTRL S to save go back and F Five. I have here instead. Now the Account query action so the name of the class. So if I don’t specify any label, I will have the name of the class, but I will not have the name of the method. So that’s why it’s better to add a label. And this label will be shown in the list of classes that I can call. And because each one of these classes can have only one method with this annotation, this is the method that will get called when we go to this class. So let’s save this. And now let’s refresh this page. Now we would get the label instead of the class name. Okay, get the account names. So let’s do that. Now we have to specify the action name. It can be anything. And now this is where we specify the parameter. So we have IDs. IDs is what? IDs is simply this one. And then we can reference this by an ID or by a feed reference. So let’s pass it the ID of the account itself. And that’s it. I don’t have any output because this is again a process. It’s not a flow. Once we move to the flow, you can see that I can specify the output. And again, the output is dependent on the return of the method.
So if I go back to the method, you can see here that I am returning a list of string. But because I am on a process, I don’t use this return. Now let’s click on save. And this is how we can call Apex from a process. But if we save now, nothing will happen because I don’t have anything there that will say that this method ran. For that, let’s add a system debug control s. So now what I’m doing, because this is a process, I don’t get to use the return. I specified a debug which will show me this in the debug log if this method ran. So let’s save this again and let’s activate. Okay, now this is active. Let’s go to any account. So I will open the application. Oh, I don’t have the account tab here. So, okay, let’s use the account tab directly and let’s open this account. We will edit this account without changing anything, which will launch the process which will call this method. So what we have to do, we have to save. And once we save, you will notice that I will have a new log there. So this is the log, and you can see that this method ran. Okay, so this is how to call an Apex method from a process. Again, the most important thing is this annotation. If I don’t have it, I cannot see this method in the process. And then I can see the label name from the list of actions. So this is the use of the label. And again, we have a specific set of parameters that we can specify. So we cannot specify.
Let’s say something that is not a list. We have a set of things that we can specify and the same thing for the return type as long as all of these are respected. This and this. And the most important thing is the annotation. We can see this and the process. Now let’s do the same, but this time within a flow. So let’s create a new screen flow. And this screen flow will use the math class that we have created. Again, this is a very simple class called Math Util. It has an invocable method. You can see that it has a label. And then it has this method which is called add. It will take a list of flow input. Flow input is a subclass. As you can see there why we use this. We use this to pass more than one input to this method. Again, we cannot pass more than one parameter to an invocable method. We can only pass one. This is how we can pass more than one by creating a subclass.
And then within this subclass we have invokeable variables. So now we can specify each one of these and one and two. And we can add as many as we want. So the purpose of this method is to take n two one and n two. As you can see here, I have a list of integers and then I am having a for loop that will loop against all of the variables of the list Fi. So again, list Fi has two variables. You can see here I am looping against it. And then once I loop, I will add the first variable and the second variable of Fi. So again, list of Fi is a list of flow input. Each one of these, each one of the flow input has two variables, nt one and n two. So nt one and n two. On each loop F-I-N one is being added to Fi, n two and my end is the result. I am again adding my end to this list which is called Results and I am returning this list. Okay, so now let’s go to the flow and let’s use this method and the flow.
So we will add a screen. We will also specify two input. So these will be numbers and I will make them required. Okay, it’s so simple to input of type number. Okay, now let me call the Apex method within the flow. So I will drag action and then let me go back to the method which is called add from the Math util class. And because I did not specify where I want this to appear, so if I go back to the first class, you can see that I have category. I can use the same CTRL C and I can specify here a category first. Let’s not do that. Let’s go there and let’s see. You can see that I have uncategorized and you can see now I can see this method there. But if I go back and let’s say I want this to appear under account control. S to save. Now I think I need to refresh. Let’s do them again. So this will be number one required. And this will be number two and required. Let’s see what we have to fix. And then let’s go to the action and drag it. Now you will see that this action will be under account.
Okay, so this is the use of the category. Now we specify the name. And there we will specify int one and into. Why? Because if I go back to the class, I have list of flow input. And then I have invocable variable within this subclass. So each one of the list of flow input has two variables. Nt one and n two. And these two should be specified within the flow. So let’s say that n one would be what would be the result of the input field that we have specified in the first screen. And the same for int two. So int one is number one, n two is number two. So this is the first input. This is the second input. Let’s specify the output. Now, because this is a flow, we can take the output. So in this case, let’s create a new resource. This will be a variable. Let’s call this result data. Type will be number. I don’t need to specify anything else. It should not be available for output because I don’t need to use this outside of the flow. And I click on done. So now what will happen? This apex class will run. It will take number one, number two and it will put them in the result variable. Now we have to show this in the next page. So next now we will add a screen and this will be result. And let’s add a display text. It doesn’t accept any space. So this is what I will type. And then this is the variable that is being set by the Apex. Done. And let’s now connect. That’s it. Let’s now save and activate.
Now let’s see how we can use this. Let’s go to the home page of this application. And let’s try to edit. Now we will go to the flow on the left side. And then we would drag this and choose where we want it on the page. So let’s say that we want it there. Click on it. And as you can see here, I will have all of the flows that I have, all of the screen flows. And that’s it. Now I need to click on Save. And I don’t think I need to activate because it is already activated. Yeah, it’s assigned to this app. So click on Close and click on Back. Now let’s test this. So let’s say I want to add 25 and 20. So if I click on Next, this flow will run the Apex class in the background. And because we set the different variables and the flow, we will have the result and the result variable which will be shown on the next screen. Next. That’s it. So this is a very simple example on how to invoke Apex from a flow. Of course, we will not use Apex to add two numbers. This can be easily done within the flow itself. But this is just to show you that we can indeed call Apex from flow. And it is, of course, for much more complex scenarios. Now, the last thing to show you is to call a flow from within Apex. So let’s go and let’s click on flows. So as you can see here, we have a list of flows. Let’s go to the square flow.
This is an auto launch flow. We cannot call a screen flow from within Apex, but it should be an auto launch flow. So what will this flow do? It will simply take a value which is square and it will assign it to the result variable. So if I go to the manager, I can see that I have different elements in this flow. So you can see that I have input variable, I have a formula and then I have an assignment. So if I click on input, this is mainly the input that we should pass to this flow. It’s called input. And then I have this formula. It will do what, it will take the input and it will make input times input. So this is the square of the input. And then we have an assignment element that will assign the square to the result. So again, square is the formula. And then we are assigning this to another variable called result. So this is a very simple flow. It will take one input and then it will output one output, which is called result, which is the square of the input. So how we can call this from within Apex. Let’s go to the anonymous Apex and let’s see how we can do that. So I will go to anonymous Apex by pressing CTRL E. So as you can see here, we have different steps that we need to do. The first step is to specify the input. Because we have only one input, we use this map which is called params, which will take string and object. And then this map will be used to specify the input that we need to pass to the flow.
Because we clearly specified that the input name is input. This is where we type input. And then we map this to number five. So this is the first step. The second step is to use this, which is calling, which is creating a new flow interview. So as you can see here, we are creating a new flow interview called Myflow. And then you can see here that I am using the name of the flow within the parameter of this method. And then I am passing the params map as the parameters of this flow and then I am starting it and then I will fetch the result back from the flow. So as you can see the name is my flow. This is the flow interview. I’m using this flow interview name to get the variable which is called result and then the last step is to use the debug to get the result.
Let’s try to run this. Let’s say that I want to take the square of six which are 36 so I would highlight this code and I will click on execute highlighted and then I would open the debug log so the same thing can be done another way. So if I go up I am using the name of the flow not as a parameter but I’m using it as part of the method. So I am using the dot notation to append the flow to this method and then I am passing the parameter prams. So this is exactly the same thing but this is another way. So you can see here flow interview square flow new flow interview square parameters if I go down flow interview my flow I did not specify the name of the flow as that notation there but I specified it as parameter. So these are the two ways to call flows from within in apex.
- 3.17- Platform Events
This lecture is about platform events. The topics of this lecture are eventdriven architecture, Salesforce Platform Event Platform Event Considerations how does platform event work? Creating a platform event, publishing the event with a trigger and a process, and subscribing to an event with a trigger and a process. First, and before talking about an eventdriven architecture, let’s talk about point-to-point integration. Imagine having so many systems that need to share data among them, for example, databases, CRM system, ERP system, custom applications, and so on. Integrating these systems is a very hard task and presents lots of challenges. This is because every system should integrate with every other system. So imagine having only four systems that need to integrate with each other’s. In this case, you will need six different integration points. This is a tight coupled environment where every system is talking to other system independently. Before jumping into Salesforce Platform Event, we need to understand the meaning of an eventdriven architecture. With eventdriven architecture, there are three main components. First, a service that publishes an event whenever it updates or creates data. Second, the event bus is the way to carry the event.
And third, other services can subscribe to those events produced by the publishing service. This model enables an application to maintain data consistency across multiple services without using distributed transactions. Eventdriven architecture can be achieved in Salesforce using platform events, where a service broadcasts an event and another service subscribes to this event and acts when it detects this event. The following are the terms used in an eventdriven architecture first of all, we have the event producer, which is the publisher of an event message. This publishing happens over a channel. Then we have Event Consumer, which is a subscriber to a channel that receives messages from the channel. Again, the publisher does not know the consumer. The job of a publisher is to publish an event and then consumers will subscribe and consume these events. And then we have the channel, which is the medium in which an event producer transmits a message. Event consumers subscribe to the channel to receive messages.
And then we have the event itself, which is a change in state that is meaningful in a business process. For example, a placement of an order is a meaningful event because the Order Fulfillment Center requires notification to process this order. And finally, an event notifier is a message that contains data about the event itself, also known as an event notification. Salesforce platform events are just like custom objects. They can be defined in setup and then platform events. As you can see on the right side, we are defining a platform event called Review Event or EVT. And you can see that the API name ends with underscore E as opposed to underscore C. For custom objects, a platform event has standard fields created by created date and Replay ID. The Replay ID is used instead of an ID to identify a platform event. You can also add custom fields on a platform event and you can attach triggers, processes and flows. These are some considerations when it comes to platform events. A Platform Event API name ends with underscore E, as you mentioned, rather than underscore C. For custom objects, you can control a Platform event permissions on profiles and permission sets, just like any custom object. So you can specify who can read and who can create a specific platform event. You can’t query platform events using Suckle or Social. You can’t use Platform events in reports and list views and in search.
These are some more considerations. Unlike custom objects, platform events do not have an associated tab. We cannot create a tab for Platform events. Platform events can have their own triggers, but only after insert triggers are supported with Platform Event. So we can create a trigger on a platform event. But the only way to create this trigger is using the after insert trigger type. Custom events are immutable. Once an event has been published, you cannot update it or delete or query it. Instead, you subscribe to a channel to receive events.
Platform events are executed under the Automated Process Entity, so you have to set the right configuration under Automated Process and Debug logs. You can see all the subscriptions and triggers of a Platform event under the Subscriptions and Triggers lists on the Platform Event page. Now that we know more about Eventdriven architecture and Salesforce Platform Events, let’s see the different steps involved with Platform events. The first step is to create the Platform event itself with its custom fields. Again, this is done from the Setup menu after creating the Platform event. The second step is to specify when the event would be published by creating a process or a trigger that creates the custom event from the source object. For example, when an invoice object status changes to Paid, an invoice trigger or an invoice process would create a Platform event. And now that the publishing side is created, the final step is to subscribe to the Platform event. We can do so by a trigger or by a process on the Platform event itself.
So any service that subscribes to this Platform event will be notified with the field values of the event. Now let’s see these steps in details. Platform Events are just like custom objects. They can be defined within the Setup menu. Overall, an event is the definition of the message you would like to be exchanged between the publisher and the subscriber. It defines the blueprint of the message. The very first step is to define a Platform event. To do so, we have to go to Setup and search for Platform Event and click on New Platform Event and create the Platform Event. We can also add custom fields to the Platform event. So let’s create, let’s say, five such fields.
And note that with Platform Events you are limited to these field types. Checkbox date, time number text and text area. And finally, note that instead of an ID field we have the replay ID field which is created and used to identify and replay the event if needed. Now that the platform event is created, we need to find ways to publish it. These are the salesforce tools that we can use to publish an event. We can publish a platform event using a process builder or Flow for declarative option. We can also use the programmatic option with triggers and Rest and Soap API. This is the first example that uses a trigger to publish platform events. As you can see we have a very simple trigger on the review object. So we have a review custom object and then we have a trigger called Review event and then this trigger will fire after insert. The first step is to create a list of the review EVT platform event called list review event. So lstrev EVT you can see that we are defining this list just like we define any other list. And then we are using a for loop. This for loop will do a loop on each one of the records of trigger new. So we will have a number of review records that are in trigger new list and on each one we are creating a new platform event of type review EVT underscore underscore E. This platform event has some fields as we have seen. Now we are specifying these fields. We are specifying the account ID field, the amount field, the date field and the status field. So the account ID field is for example the account underscore underscore C of the rev record which is this record. So on each loop we are creating EVT. We are specifying the fields and then we are adding EVT to the list of platform event.
Now that this is done, the next step is to check if this list is not empty and if this is the case we are using this method. So the method is event bus that publish and the parameter is the list of platform events. So again we are creating a list of platform events. We are adding each one of the event to this list and then we are checking if this list is not zero. If this is the case, we are using the event bus that publish method and using this method will publish the list of platform events which is called List Revt. This is the second example that uses a process instead of a trigger to publish platform events. So this process is on the review object and it should start on record creation when a review record is created. So just like any other process and process builder we can specify the object. But now as you can see here, the action is to create a record.
So we can specify the name of the action and then we can specify the record type. So this is the difference. Instead of specifying a record type of a custom object or of a standard object, we specify the record type of the platform event and then you can see each one of the fields of this platform event is there and then we are adding the value to these fields. So the process should start on record creation. The process action should be create a record. The record type should be Review EVT and then we have to specify the platform event fields from the review record fields. So as we have seen we have the first option a trigger and then we have this option a process. We cannot keep both of them, we need to choose one of these two. Now that a platform event is defined and the platform events are published, we need to subscribe to these events. These are the salesforce tools that we can use to subscribe to an event. We can subscribe to an event using Process Builder or Flow for the clarity option. We can also use the programmatic option with triggers.
Apex triggers can be written for platform events but only after install triggers can be used with platform events. Finally there is the option of Commitd. Comet d is the streaming API. A platform event can be seen as simply a broadcasted message. For that a system should subscribe to a platform event to get its updates. This is the first example that uses a trigger to subscribe to a platform event. To do so a trigger on the platform event can be used to subscribe to the platform event itself. So we have an example here we have a trigger and then we have the name of the trigger and then we have the keyword on and then we have the platform event name. So it’s not a custom object, it’s not a standard object, it is the platform event. And remember, only after insert is supported. So now once the events are being published they will be called by this trigger and then we will have a for loop against trigger new on each one of the events. We are using system debug to mimic that. Now we are calling a thirdparty application. So again we can have a trigger on the platform event.
This trigger will capture any platform event that is being broadcasted and then on each one of these it can call a third party application. This is the second example that uses a process to subscribe to a platform event. The process will be used to handle this. The process starts when a platform event message is received. So once an event is being broadcasted it can be received. You can add the object to act on and the relation with the platform event. And the conditions can also be based on the event record or on the object. So we are simply creating a new process. But this time this process will start when a platform event message is received. So this is the main difference between a regular process, if you want to call it, and a platform event process.
Now, as you can see here, we have to specify when to start the process. We can specify the object and then we can specify the matching conditions and that’s it for this texture. In this texture, we have talked about the event driven architecture where a specific service can publish an event whenever it updates or creates data, and other services can subscribe to those events produced by the first service. When using this approach, there is no need for specific distributed transactions. Read and edit permissions can be given to platform events through profile and permission sets.
They do not support circle or Social, and they don’t support reports, list, reviews, or search for platform events. To work, we need three different steps. Step one you define a platform event in the same way that you define a custom object. Platform events are s objects that are similar to custom objects, but they end in underscore underscore E. Step two after a platform event has been defined in your Salesforce. org, publish this event from a Salesforce app using Processes or Apex or even Flows. And finally, step three is to subscribe to the platform event. You can receive platform events and Processes and triggers, or even Flows. And finally, as usual, thanks for watching.