Monday, February 22, 2021

Automatically Covert a lead to Account,contact,Opportunity Trigger.

 How  to  convert  Automatically  lead to Account,contact,Opportunity  Trigger.

trigger LeadHandler on Lead (after insert)
{
    list<Account>  listacc=new list<Account>();
    list<contact>  listcon=new list<contact>();
    list<Opportunity> listopp=new list<Opportunity>();
    
    for(Lead    l:trigger.new)
    {
        Account    acc=new    Account();
        acc.name=l.Company;
        acc.phone=l.phone;
        listacc.add(acc);
        
        Contact    con=new    Contact();
        con.lastName=l.lastName;
        listcon.add(con);
        
        Opportunity  opp=new  Opportunity();
        opp.Amount=l.AnnualRevenue;
        opp.Name=l.FirstName;
        opp.CloseDate=system.today();
        opp.stageName='Closed Won';
        opp.Name=l.FirstName;
        listopp.add(opp);
    }
    
    insert  listacc;
    insert  listcon;
    insert  listopp;
}

Sunday, February 21, 2021

Opportunity StageName is changed then change Account Rating to Hot

Requirement: When ever Opportunity stageName is changed to 'Closed Won' then update the Account Rating to 'HOT'. First we have to set the id with AccountId. Then we should query the records. Then we have to give condition in(if Statement) Then we have to update the acclist. =====================================================================
trigger OpportunityHandler  on  opportunity(after insert,after update)
{
    set<id> setid=new set<id>();
    list<Account>   acclist=new list<Account>();
    for(opportunity opp:trigger.new)
    {
        setid.add(opp.Accountid);
    
    acclist =[select id,Name from  Account where id in:setid];
    if(opp.StageName=='Closed Won')
        {
            
            for(Account acc:acclist)
            {
                acc.Rating='Hot';
            }
            update acclist;
        }  
   } 
}

Friday, February 19, 2021

How to count numberofcontacts trigger

To Count numberofcontacts using trigger.We can use RollupSummary for master-detail  relationship,but we  can create  

only 2 master-detail  reationships  for an  object.

1.we  have  to  create  a read-only field with  number to get count

2.we should create read-only field  in parent object.

3.we  have  to get count from  the child  object,so   we  write   trigger only  child object.

Note:If any queries please comment.

trigger NumberofContacts on contact(after insert,after update,after delete)

{

map<id,integer> mapacc=new map<id,integer>();

list<Account> 1stacc=new list<Account>();

if(trigger.isInsert)

{

for(contact con:trigger.new)

{

integer count=0;

if(mapacc.containskey(con.Accountid))

{

count+=mapacc.get(con.accountid);

}

count++;

mapacc.put(con.Accountid,count);

}

if(!mapacc.isempty())

{

for(Account acc:[select id,name,numberofcontacts__c from Account where id in:mapacc.keyset()])

{

Account a:new Account(id=acc.id);

mapacc.containskey(acc.id);

if(a.numberofcontacts__c!=null)

{

a.numbeeofcontacts__c=acc.numberofcontacts__c+mapacc.get(acc.id);

}

else

{

a.numberofcontacts__c=mapacc.get(acc.id);

}

firstacc.add(a);

}

}

}

if(trigger.isUpdate)

{

for(contact con:trigger.new)

{

integer count=0;

if(mapacc.containskey(con.Accountid))

{

count+=mapacc.get(con.accountid);

}

count++;

mapacc.put(con.Accountid,count);

}

for(contact con:trigger.old)

{

integer count=0;

if(mapacc.containskey(con.Accountid))

{

count+=mapacc.get(con.accountid);

}

count--;

mapacc.put(con.Accountid,count);

}

if(!mapacc.isempty())

{

for(Account acc:[select id,name,numberofcontacts__c from Account where id in:mapacc.keyset()])

{

Account a:new Account(id=acc.id);

mapacc.containskey(acc.id);

if(a.numberofcontacts__c!=null)

{

a.numbeeofcontacts__c=acc.numberofcontacts__c+mapacc.get(acc.id);

}

else

{

a.numberofcontacts__c=mapacc.get(acc.id);

}

firstacc.add(a);

}

}

}

if(trigger.isDelete)

{

for(contact con:trigger.old)

{

integer count=0;

if(mapacc.containskey(con.Accountid))

{

count+=mapacc.get(con.accountid);

}

count--;

mapacc.put(con.Accountid,count);

}

if(!mapacc.isempty())

{

for(Account acc:[select id,name,numberofcontacts__c from Account where id in:mapacc.keyset()])

{

Account a:new Account(id=acc.id);

mapacc.containskey(acc.id);

if(a.numberofcontacts__c!=null)

{

a.numbeeofcontacts__c=acc.numberofcontacts__c+mapacc.get(acc.id);

}

else

{

a.numberofcontacts__c=mapacc.get(acc.id);

}

firstacc.add(a);

}

}

}

if(!firstacc.isempty())

           {

               update firstacc;

           }


}


Automatically Covert a lead to Account,contact,Opportunity Trigger.

 How   to   convert    Automatically  lead to Account,contact,Opportunity   Trigger. trigger LeadHandler on Lead (after insert) {     list&l...