trigger tg1 on account(before insert,before update){
if(trigger.isbefore && trigger.isinsert){
for(account ac:trigger.new){
if(ac.industry==null){
ac.industry='banking';
}
}
}
if(trigger.isbefore && trigger.isupdate){
for(account ac:trigger.new){
if(ac.industry==null){
ac.industry='Education';
}
}
}
}
========================================================================trigger tg1 on account(before insert,before update){
if(trigger.isbefore && trigger.isinsert){
trg1_handler.before_insert(trigger.new);
}
if(trigger.isbefore && trigger.isupdate){
trg1_handler.update(trigger.new);
}
}
--------------------------------------------------------------------------------
public class trg1_handler{
public static void before_insert(list<account> acc){
for(account ac:trigger.new){
if(ac.industry==null){
ac.industry='banking';
}
}
}
public static void before_insert(list<account> acc){
for(account ac:trigger.new){
if(ac.industry==null){
ac.industry='Education';
}
}
}
}
========================================================================rigger trg3 on account(before delete){
for(account a:trigger.old){
a.adderror('you can't delete account record');
}
}
========================================================================
trigger trg4 on account(before insert,before update){
for(account a:trigger.new){
if(a.DOB__c==Null){
a.adderror("DOB is mandatory");
}
}
}
========================================================================
trigger duplicateacc on Account (before insert,before update) {
set<string> names=new set<string>();
for(account a:trigger.new){
names.add(a.name);
}
list<account> acc=[select id,name from account where name in : names];
map<string,account> accmap=new map<string,account>();
for(account a:acc){
accmap.put(a.name,a);
}
for(account a:trigger.new){
if(accmap.get(a.name)!=Null){
a.adderror('duplicate account found');
}
}
}
========================================================================when we insert account delete contact whose name as account name
========================================================================
trigger trg6 on account(before insert){
set<string> accnameset=new set<string>();
for(account a:trigger.new){
accnameset.add(a.name);
}
list<contact> conList=[select id, name from contact where name in:accnameset];
if(conList.size()>0 && conList!=Null){
delete conList;
}
}
========================================================================
previous account deatail save in backup cutom object
========================================================================
trigger tag7 on account(before update){
list<account_backup__c> accbck=new list<account_backup__c>();
for(account a:trigger.old){
account_backup__c ab=new account_backup__c();
a.name=ab.name;
a.phone=ab.phone__c;
a.industry=ab.industry__c;
accbck.add(a);
}
insert accbck;
}
========================================================================
when ever change/update case status "working" case assign to current user
========================================================================trigger tag8 on case(before update){
for(case c:trigger.new){
if(c.status='working'){
c.ownerID=c.LastModifiedById;
}
}
}
========================================================================
do not insert duplicate lead with email address
========================================================================
trigger tag9 on lead(before insert,before update){
for(lead l:trigger.new){
if(l.eamil!=Null){
list<conatact> conList=[select id,lastname,email from contact where email in:l.email];
if(conList.size()>0 && conList!=Null){
l.adderror('Contact is Exsited with same email');
}
}
}
}
========================================================================
when erver change account phone no that is assign to contact phone no
========================================================================
trigger tag10 on account(){
map<id,account> myacc=trigger.new;
list<contact> updateconList=new list<contact>();
list<contact> conList=[select id,lastName,Phone,accountID from contact where accountID in:myacc.keyset()];
for(contact c:conList){
c.phone=myacc.get(c.accountID).phone;
updateconList.add(c);
}
update updateconList;
}
========================================================================
trigger tag10 on account(before update){
list<contact> updateconList=new list<contact>();
list<account> acc=[select id,name,decription,(select id,lastname,description from contacts) from account where id in:trigger.newmap.keyset()];
for(account a:acc){
for(contact c:a.contacts){
c.description=a.description;
updateconList.add(c);
}
}
update updatecontactList;
}
========================================================================
After Trigger
========================================================================
when ever opporunity is closedWon create cheld record
========================================================================
trigger tag11 on opportunity(after insert,after update){
list<opp_child> updateoppchild=new list<opp_child>();
for(opportunity op:trigger.new){
if(triggrt.oldmap.get(op.id).stagename!='closedWon' && op.stageName==ClosedWon){
opp_child__c oppchild=new opp_child();
oppchild.name=op.name;
oppchild.opportunity__c=o.id;
updateoppchild.add(oppchild);
}
}
update updateoppchild;
}
========================================================================
when we create account contact and opportunity will created
========================================================================
trigger tag12 on account(after insert){
for(account a:trigger.new){
contact c:new contact();
c.lastname='default contact' +a.name;
c.accountID=a.id
insert c;
opportunity op=new opportunity();
op.name='default opp'+a.name;
op.accountID=a.id;
op.stagename=closedWon;
op.closedate=system.today()+60;
insert op;
}
}
========================================================================
when ever we update account billingstreet update contact billingstreet (not good pratice)
========================================================================
trigger tag13 on account(after update){
list<contact> updatcont=new list<contact>();
for(account a:trigger.new){
if(trigger.oldmap.get(a.id).billingstreet!=a.billingstreet){
list<contact> conList=[select id,name,accountId,otherstreet from conatct where accountId in:a.id];
for(contact c:conList){
c.otherstreet=a.billingstreet;
updatecont.add(c);
}
}
}
update updatcont;
}
========================================================================
========================================================================
Trigger UpdateAddressOnContact on Account (after update) {
Set<Id> accountIds = Trigger.newMap.keySet();
//query contacts related to triggering accounts
List<Contact> contacts = [SELECT accountId, <other additional fields> FROM Contact WHERE AccountId IN :accountIds]
Account a;
for(Contact c : contacts) {
a = Trigger.newMap.get(c.AccountId);
c.MailingStreet = a.BillingStreet;
c.MailingCity = a.BillingCity;
c.MailingState = a.BillingState;
c.MailingPostalCode = a.BillingPostalCode;
c.MailingCountry = a.BillingCountry;
}
update contacts;
}
========================================================================
when ever delete account delete correspondence contact
========================================================================
trigger tag14 on account(after delete){
list<contact> conList=[select id,name from contact where accountId in:trigger.oldmap.keyset()];
delete conList;
}
=======================================================================
cascade delete enable for that
======================================================================
after Undelete
=======================================================================
when ever undelete parent record then undelete child also
=======================================================================
trigger tag15 faculty__c(after undelete){
for(faculty__c a: trigger.new){
list<cource__c> couList=[select id,name,faculty__c from cource__c where faculty__c:a.id ALL ROWS];
undlete couList;
}
}
========================================================================
Recursive Tigger error-maximun trigger depth exceeded
========================================================================
trigger tag17 on contact(after update){
if(recuriveness.flag==true){
recuriveness.flag=false;
id contID;
for(contact c:trigger.new){
conID=c.id
}
contact con=[select id,lastname from contact where id in!=:contID limit 1]
con.eamil='sanjay@usa.com';
update con;
}
}
---------------------------------------------------------------------------------------------------------------
public class recuriveness{
public static boolean flag=true;
}
========================================================================
create multiple contact..
========================================================================
trigger tag18 on Account (after insert,after update,after delete) {
map<id,decimal> accmap=new map<id,decimal>();
for(account a:trigger.new){
accmap.put(a.id,a.Count_contact__c);
}
list<contact> conList=new list<contact>();
if(trigger.isinsert && trigger.isafter){
if(accmap.size()>0 && accmap!=Null){
for(id a:accmap.keyset()){
for(integer i=1;i<accmap.get(a);i++){
contact ca=new contact();
ca.lastName='default'+i;
ca.accountID=a;
ca.phone='879731928379';
conList.add(ca);
}
}
}
insert conList;
}
if(trigger.isupdate && trigger.isafter)
{map<id,account> omap=trigger.oldmap;
map<id,account> nmap=trigger.newmap;
decimal Contactcreate;
for(account a:trigger.new)
{
if(nmap.get(a.id).Count_contact__c > omap.get(a.id).Count_contact__c)
{
Contactcreate=nmap.get(a.id).Count_contact__c - omap.get(a.id).Count_contact__c;
for(integer i=1;i<=Contactcreate;i++)
{
contact c=new contact();
c.lastname='contact is '+i;
c.AccountId=a.id;
conlist.add(c);
}
insert conlist;
}
else
{
Contactcreate= omap.get(a.id).Count_contact__c - nmap.get(a.id).Count_contact__c;
list<contact> co=[select id,lastname from contact where accountid=:a.id order by id desc limit :(integer)Contactcreate];
delete co;
}
}
}
if(trigger.isdelete && trigger.isafter){
}
}
================================================================
when ever industry is education wilson is account team mamber
=======================================================================
trigger tag19 on account(after insert){
user u=[select id,name from user where lastname='wilson' limit 1];
list<AccountTeamMember> accountteam=new list<AccountTemaMember>();
for(account a:trigger.new){
if(a.industry=='Banking'){
AccountTeamMember atm=new AccountTeamMember();
atm.accountID=a.id;
atm.UserId=u.id;
atm.teammemberrole='Account Manager';
atm.AccountAccessLevel='edit';
accountteam.add(atm);
}
}
insert accountteam;
}
========================================================================when ever opportunity is closedwon share with manager
========================================================================trigger tag20 on opportunity(after insert,after update){
user u=[select id, lastname from user where name='wilson' limit 1];
list<opportunityshare> opshare=new list<opportunityshare>();
for(opportunity op:trigger.new){
opportunityshare os=opportunityshare();
os.opportunityId=op.id;
os.userofgroupId=u.id;
os.opportunityAccessLever='edit';
os.rowcouse='manual';
opshare.add(os);
}
insert opshare;
}
========================================================================update child to parent update (phone no);
========================================================================trigger phupdate on Contact (after insert,after update) {
List<account> li=new list<Account>();
List<Id> ids = new List<Id>();
for(Contact c: trigger.new)
ids.add(c.AccountId);
Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Phone From Account Where Id In :ids]);
for(Contact c: trigger.new)
{
Account a = accountMap.get(c.AccountId);
if(a != null)
{
a.Phone= c.MobilePhone;
li.add(a);
}
}
update li;
}
========================================================================trigger countcontactonacc on Contact (after insert,after update,after delete,after undelete) {
set<id> accountIds=new set<id>();
list<account> lstupdateacc=new list<account>();
if(trigger.isinsert || trigger.isUndelete){
for(contact c:trigger.new){
accountIds.add(c.accountID);
}
}
if(trigger.isupdate || trigger.isdelete){
for(contact c:trigger.old){
accountIds.add(c.AccountId);
}
}
for(account acc:[select id,name,Count_contact__c,(select id,name from contacts) from account where id IN:accountIds]){
list<contact> con=new list<contact>();
for(contact c:acc.contacts){
con.add(c);
}
acc.Count_contact__c=con.size();
lstupdateacc.add(acc);
}
update lstupdateacc;
}
=============================================================
trigger ContactTriggerWithMap on Contact (before insert){
Set<Id> SetAccountId = new Set<Id>();
for(Contact cont: Trigger.new){
if(cont.AccountId != null){
SetAccountId.add(cont.AccountId);
}
}
if(SetAccountId.size() > 0 ){
Map<Id, Account> mapAccount = new Map<Id, Account>([Select Id, Name, Type from Account where Id IN :SetAccountId ]);
for(Contact cont: Trigger.new){
if(cont.AccountId != null && mapAccount.containsKey(cont.AccountId) ){
cont.Type__c = mapAccount.get(cont.AccountId).Type;
}
}
}
}
=============================================================
No comments:
Post a Comment