As any tutorial in programming, we will first create a Hello World script.
Just to get a sneak peak 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 can not 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 lets do all our customization in a sandbox and if everything is ok, after we test, then we can go ahead and publish the sandbox.
Better to be safe then sorry later, I always like to say.
Steps to go into a sandbox.
- Click Your User name
- 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 Active
– now you should be in a sandbox
Go to Navigator > Application Composer and Select 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 the picture above
Give it a name as Hello_World and then click save and close
Oky ,so we have a field now, we need to display it on the 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 Lets 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 Scrips 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 chose and save a trigger type, it can not be changed to a different type .
So if we make this one as Before Update in Database then save, we can not go back later and change to After Create, for example.
Oky, so now lets make our code.
Keep in mind Before Update in Database triggers, execute when we are on the details of a record, we 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.
Lets take a look at the syntax we did.
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 functions 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”
Now if you would 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.