I need write insert or update trigger, but with WHEN condition with compare OLD and NEW rows.
According documentation OLD is null for insert operation. How i can use
Trigger.oldMap.keySet(); will give the Id's present in the Trigger.oldMap. It is a set type collection of all the Id's. have a look at the following example, change the DML events in the trigger events every time and see the debug logs. you will understand which trigger context variable is available for which DML event.
CREATE TRIGGER Email_Check_On_Contact before update{ oldMap=new Map(); o = trigger.oldMap; for(Contact newcont: trigger.new) { if(newcont.Email != o.get(newcont.Id).Email) { newcont.Email.addError('Email cannot be changed'); } } }