I’m a programmer, a developer at heart. When I encounter a new programming environment and/or language, I want to ‘see’ it working. No manuals, no training, no nothing. Just get it working on my own. For this we have the ‘Hello World’ approach. The most simple ‘one-line’ code which works and by which you have touched the basic parts of the new environment to get things going.
Here I’m getting to know Apex, the programming language of Salesforce. As I start from zero, I know nothing. I heard someone talk about ‘triggers’. Apparently you can use Apex triggers to perform some stuff. A trigger, in this case, is an action which does something at the moment you make changes to the database (to a record).
Ok, how can I ‘see’ this working? Well, I simply try a very easy thing. In Salesforce there is the Contact object. I create an extra field on this Contact object. A ‘custom field’ which I call ‘Apextest’ (text(50)). And now I want this ‘Apextest’ field filled with the LastName of the Contact (a copy). So, I need a ‘trigger’ which does this copy action whenever I insert or update the Contact (record). And in this trigger I need to specify the ‘copy’ action. Below you will find the steps in shorthand:
– Create custom field Apextest (text(50)) on Contact
– Put it on a Page-layout for you to see it:)
– The Api name of that Apextest field comes out as Apextest__c
Now we need to create the trigger on Contact
– Setup > Build > Develop > Apex triggers
– Click ‘Developer Console’
– File > New > Apex Trigger
Paste the following code
trigger Apextest on Contact (before insert, before update) {
for (Contact c : Trigger.new) {
c.Apextest__c = c.LastName;
}
}
– Save
In the Apex Trigger list (after refresh) you will now see the trigger ‘Apextest’. And it is active
– Goto to Contacts
– Pick a Contact
– Probably the Apextest field is still EMPTY (because the record has not been changed/touched yet)
– Click Edit
– Click Save
Now the (update) triggers which are defined on Contact are activated
– Check the content of the Apextest field
It should hold a copy of the LastName of the Contact
Congratulations, and welcome to the world of Salesforce Apex programming :)