befor Update Triggers ======================================================================= 1.On the records which are Trigger.New in before Update Trigger.New ------------------------------ Read Write SOQL DML ------------------------------- Yes Yes No No 2.On the records which are Trigger.NewMap in before Update Trigger.NewMap ------------------------------ Read Write SOQL DML ------------------------------- Yes Yes No No 3.On the records which are Trigger.old in before Update Trigger.old ------------------------------ Read Write SOQL DML ------------------------------- Yes No Yes No 4. On the records which are Trigger.oldMap in before Update Trigger.oldMap ------------------------------ Read Write SOQL DML ------------------------------- Yes No Yes No =============================================================== after Update Trigger ================================================================ 1.On the records which are Trigger.New in after Update Trigger.New ------------------------------ Read Write SOQL DML ------------------------------- Yes No Yes Yes 2. On the records which are Trigger.NewMap in after Update Trigger.NewMap ------------------------------ Read Write SOQL DML ------------------------------- Yes No Yes Yes 3.On the records which are Trigger.old in after Update Trigger.old ------------------------------ Read Write SOQL DML ------------------------------- Yes No No No 4. On the records which are Trigger.oldMap in after Update Trigger.oldMap ------------------------------ Read Write SOQL DML ------------------------------- Yes No No No Basic Scenario: 1. Object : Account Fields : Phone 2. Object : Contact Field : Account(Lookup) MobilePhone Scenario 6: When ever the Account' object phone no is modified ,Its corresponding child contacts records mobilePhone values should be replaced with Account's New phoneNo Trigger : ---------- trigger Scenario6 on Account (before update) { List<Account> oldList=[select id,phone,(select mobilephone from Contacts) from Account where id In: Trigger.Old]; List<Contact> cons=new List<Contact>(); for(Account a:oldList){ for(Contact c:a.contacts){ c.MobilePhone=Trigger.newMap.get(a.id).Phone; cons.add(c); } } update cons; } TestClass : ----------- @isTest private class scenario6 { @isTest static void testme(){ Account a=new Account(Name='aaa',phone='111'); insert a; Contact c1=new Contact(lastname='test',mobilePhone='111',accountid=a.id); insert c1; a.phone='123'; try{ update a; }catch(Exception e){ System.debug(e); } Account acc=[select id ,phone from Account where id=:a.id]; Contact con=[select id,mobilePhone from Contact where Id=:c1.id]; System.assertEquals(acc.phone,'123'); System.assertEquals(con.MobilePhone,'123'); } } Scenario 7: Object : Account Fields :Manager : Text (Create this custom Field first) : Industry : PickList( Standard Field) Object : User Fields : Account_Manager : Text Fields Task : When ever the industry field value is modified as banking then 1.Fetch the user who is the owner of the Account Record 2.Update the Account_Manager field on the user with Manager field value Trigger: trigger Scenario7 on Account (before update ) { Map<Id,Account> oldMap=Trigger.oldMap; Map<Id,Account> newMap=Trigger.newMap; List<Id> accs=new List<Id>(); for(Id aid:oldMap.keySet()){ if(oldMap.get(aid).Industry !=newMap.get(aid).Industry && newMap.get(aid).Industry=='Banking'){ accs.add(aid); } } List<Id> userids=new List<Id>(); for(Id mid:accs){ userids.add(newMap.get(mid).ownerid); } List<user> newUsers=new List<User>(); Map<Id,User> userMap=new Map<Id,User>([select id,Account_Manager__c from User where id in :userids]); for(Id mid:accs){ Id uid=newMap.get(mid).ownerid; User u=userMap.get(uid); u.Account_Manager__c=newMap.get(mid).Manager__c; newusers.add(u); } update newusers; } Scenario 8: Object : Account Field : Name Object : Contact 1.Before insert 1.When we are inserting new account record first check if there are any duplicate records with same name already existing 2. if they are existing throw error message 2.after update : 1. Check wheather there are any contact records already existing for the give account 2. if no contacts are existing create new contact Trigger Class : public class TriggerExample { public static void beforeInsert(List<Account> accs){ List<String> names=new List<String>(); for(Account a:accs){ names.add(a.name); } List<Account> duplicates=[select id,name from Account where name in:names]; if(duplicates.size()>0){ Set<String> dupnames=new Set<String>(); for(Account a:duplicates){ dupNames.add(a.name); } for(Account ac:accs){ if(dupNames.contains(ac.Name)) ac.addError('Duplicate name Exists'); } } } public static void afterUpdate(List<Account> accs){ List<Contact> cons=new List<Contact>(); List<Account> accounts=[select id,name,phone,(select id from Contacts) from Account where id in:accs]; for(Account a:accounts){ if(a.contacts.size()==0){ Contact c=new Contact(); c.accountid=a.id; c.lastname=a.name; c.phone=a.phone; cons.add(c); } } insert cons; } } Trigger: --------- trigger scenario8 on Account (before insert,after update) { if(Trigger.isBefore && Trigger.isInsert){ TriggerExample.beforeInsert(Trigger.New); } if(Trigger.isAfter && Trigger.isUpdate){ TriggerExample.afterUpdate(Trigger.New); } } Test Class : @isTest private class Scenario8 { @isTest static void testme(){ Account a1=new Account(Name='aaa'); insert a1; Integer count=[select count() from Account]; Integer size=[select count() from Contact]; Account a2=new Account(Name='aaa'); try{ insert a2; }Catch(Exception e){ System.debug(e); } Integer newCount=[select count() from Account]; System.assertEquals(count,newCount); a1.phone='123'; update a1; Account acc=[select id,phone from Account where phone='123']; System.assertEquals(acc.phone,'123'); Integer newSize=[select count() from Contact]; System.assertEquals(size+1,newSize); } }
Tuesday, 12 April 2016
before and After triggers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment