Sunday 12 June 2016

Apex - basic trigger and unit test explained

Apex - basic trigger and its unit test explained


Basic Trigger

// trigger name "addcompanyname" on "user" (sobject)
trigger addcompanyname on User (before insert) {

//for every user in trigger.new do whats in the bracket (known as loop)
    for (User thisuser : Trigger.new) {
//companyname field   is apre ltd  (is possible to add more values)           
        thisuser.companyname = 'aspre ltd';          
    }
}


notes:
trigger - starting point for all triggers

addcompanyname - name of trigger can be anything

user - sobject  for a full list of standard salesforce objects and for a full list of object refeence

before insert - trigger will fire before record inserted/created in database for list of trigger operations

thisuser - variable that represent each specific user in the loop as it iterates one by one across all users in the list trigger.new

trigger.new -returns a list of the new versions of the sObject recordsthat are captured in trigger. this list only available in insert/update triggers and record can only be changed in before triggers for list of trigger context variables

Unit Test for basic trigger
every trigger requires a unit test to push code from sandbox to production -Salesforce requires at least 75% of your code to be “tested” before deploying

tests what the code should do

//all tests start with @isTest 
@isTest
//this line defines the class method access and name (for tests below will never chnge)
public class test_addcompanyname
    static testMethod void insertNewUser() {
     
// how to instantiate new user
       User userToCreate = new User();
     
       // create all mandatory fields
       userToCreate.FirstName = 'Iain';
       userToCreate.LastName  = 'Banks';
       userToCreate.Email     = 'iain.banks@email.com';
       userToCreate.Username  = 'iain.banks@email.com.sandbox';
       userToCreate.Alias     = 'ilearnapex';
       userToCreate.ProfileId = '00ei0000000rTfp';
       userToCreate.TimeZoneSidKey    = 'America/Denver';
       userToCreate.LocaleSidKey      = 'en_US';
       userToCreate.EmailEncodingKey  = 'UTF-8';
       userToCreate.LanguageLocaleKey = 'en_US';

       // we explicitly insert our user into our Salesforce database using this DML code otherwise it will not eneter the dsatabase
       insert userToCreate;
    }
}

notes

@isTest – this tells Salesforce we’re writing test code

test_addcompanyname - name of test class

testMethod – this tells Salesforce once again that we’re doing a test

insertNewUser – the name we gave our test method.

User – the API name of any Salesforce sObject, in this case, the standard User object.

userToCreate – the variable we named to store our user record.

’00ei0000000rTfp’ – this is the ID of the profile we’ll give to our user. Note that your org will have different IDs. 15-digit record IDs can be found in the URL of any record. Like text, IDs need to be wrapped in single quotes.

insert – the DML statement to “create” a record in an org. The record only exists in our code until we save it using DML.






No comments:

Post a Comment