As any tutorial in programming, we will first create a Hello World script.
Just to get a sneak peek on how we do stuff in Sales Cloud Groovy Scripting.
First and foremost, every customization or script you do in Fusion Application, please do it in a Sandbox.
I cannot stress this enough, we are always getting cases when users are not making their customization in sandboxes to first test them and then publish, but they do it directly in mainline.
Things go wrong with their customization, and then they are faced with bigger issues.
So to avoid all this, let's do all our customization in a sandbox and if everything is okay, after we test, then we can go ahead and publish the sandbox.
Better to be safe than sorry later, I always like to say.
Steps to go into a sandbox:
- Click Your Username
- Select Manage Sandboxes
- Click New Button or Actions > New
- Give a Name > Save and Close
- Search your newly created sandbox in the list
- Select it and click Activate
- now you should be in a sandbox
Go to Navigator > Application Composer and select the Sales Area and expand Opportunity Object.
Now go ahead and select Fields > Click Tab Custom Fields and click create new custom field button
From the popup, select field type as text and click OK.
You should be now in the details page of the field, as shown in the picture above
Give it a name as Hello_World and then click save and close
Okay, so we have a field now, we need to display it on the simplified pages.
Select Pages > Click Details Page Layer and Click Edit
Now you Edit Summary and add the field we created in the list of Selected Fields.
Save everything and now our field is displayed on the Simplified Details Page of Opportunity.
Now let's make our Hello World Script.
For this, we will use a trigger (you will see that most of the scripting in Sales Cloud is done in triggers).
So go to Server Scripts and select Object Triggers Tab
Now create a new Trigger and make it of type Before Update in Database (We will discuss trigger types later).
To be noted, once you choose and save a trigger type, it cannot be changed to a different type.
So if we make this one as Before Update in Database, then save, we cannot go back later and change it to After Create, for example.
Okay, so now let's make our code.
Keep in mind, Before Update in Database triggers execute when we are on the details of a record, modify some fields, and then click save.
Just type:
setAttribute('Hello_World_c', "Hello World")
Save everything > go to the Opportunities Landing page > open an existing opportunity > edit some random field and click save
Our field upon saving should display Hello World.
And you can congratulate yourself; you just made your first script in Groovy.
Let's take a look at the syntax we did.
setAttribute('Hello_World_c', "Hello World")
It is a very simple syntax, as it is a very simple script, but one thing needs to be remembered here, we used a method called setAttribute().
As you will learn when discussing and making more complex scripts, this function is the only way we can set a value to a field.
This function takes 2 arguments:
1 - Name of the field we want to set a value to, in our case is Hello_World_c this is the API name of our field
2 - A value we want to set on the field, in our case is the text Hello World
As we go deeper into scripting, we will learn that we can pass as the second argument all sorts of stuff like variables, numbers, even other functions.
And we will also be looking into what are methods/functions and their arguments.
Let me give you an example of passing a variable into the field Hello_World_c:
def var_A = "Hello World"
setAttribute('Hello_World_c', var_A)
Now if you were to replace the code in the trigger with the new one we made, it would have the same result, only the method by which we obtained this result has changed.
In any case, to fully understand the second script, we will have to discuss variables and how they are used in Groovy.