Trigger on Account:
Ex:1
trigger acctrig on Account (before insert, before update, after insert){
Account acc = trigger.new[0];
if(Trigger.isBefore && Trigger.isInsert){
System.debug('----------------->BEfore :'+acc);
acc.Type ='Prospect';
acc.rating = 'Hot';
System.debug('----------------->After assigning:'+acc);
}
if(Trigger.isAfter && trigger.isInsert){
System.debug('----------------->After inserting:'+acc);
}
if(trigger.isBEfore && Trigger.isUpdate){
Account oldacc=Trigger.old[0];
System.debug('----------------->Before values in Account'+oldacc );
Account newacc = trigger.new[0];
System.debug('----------------->New values in Account'+newacc );
}
}
Ex:2
trigger asccounttrig on Account (after insert) {
/*
Account ac = trigger.new[0];
System.debug('-------->'+ac);
//insertcontactfromtrigger.insertcon(ac.id);
Contact obj = new Contact();
obj.accountid = ac.id;
obj.lastname = ac.name;
obj.phone= ac.phone;
insert obj;
*/
//Bulk Trigger on Accounts inserted from Debug log
List<Account> lst =new List<Account>();
for(Account acc : trigger.new){
lst.add(acc);
}
System.debug('---------> inserted accounts'+lst);
List<Contact> lstcon =new List<Contact>();
for(account a:lst){
System.debug('---------->'+a);
Contact c = new Contact();
c.lastname = a.name;
c.accountid = a.id;
c.phone = a.phone;
lstcon.add(c);
}
insert lstcon;
}
Trigger on Contact:
Ex:1
trigger contactTrig on Contact (before insert, before update, after insert, after update) {
Contact c = Trigger.new[0];
System.debug('-------------->Before inserting into db--'+c);
if(Trigger.isBefore && Trigger.isInsert){
System.debug('------------->before isnert');
if(c.MobilePhone != null && c.email == null){
c.addError('Please enter your email id.. ');
}
c.LeadSource = 'Web';
}
if(trigger.isbefore && Trigger.isUpdate){
System.debug('----------->New record'+Trigger.new[0]);
System.debug('----------->Old record'+Trigger.old[0]);
}
if(Trigger.isAFter && (Trigger.isInsert|| Trigger.isUpdate)){
System.debug('------------>after insert');
classfromtrigger obj =new classfromtrigger();
obj.updaterec(c.id);
}
}
Apex Class for After Insert in the above Trigger:
public class classfromtrigger{
public void updaterec(string cid){
System.debug('----------->Method called from Apex Trigger');
System.debug('----------->Insrted Contact id is:'+cid);
Contact c =[Select id, name, mobilephone from Contact where id=:cid];
System.debug('------------> Insrted record '+c);
if(c.mobilephone == null){
c.mobilephone='123456789';
update c;
}
}
}
Ex:2
trigger Contacttrig on Contact (before insert, after insert, before update) {
Contact c = trigger.new[0];
if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
System.debug('--------->'+c);
System.debug('--------->'+c.accountid);
if(c.accountid != null){
if(c.Phone == null){
//c.addError('Please enter phone value');
Account acc = [Select id, phone from Account where id=:c.accountid];
c.phone= acc.phone;
}
}
}
if(Trigger.isAfter && Trigger.isInsert){
System.debug('--------->'+c);
afterupdatecls obj = new afterupdatecls();
obj.m1(c.id);
}
}
Apex Class for After Insert in the above Trigger:
public class afterupdatecls{
public void m1(string conid){
System.debug('------->'+conid);
Contact c =[Select id, email from Contact where id=:conid];
if(c.email == null){
c.email='C@k.com';
update c;
}
}
}
No comments:
Post a Comment