Wednesday, July 5, 2017

Trigger Scenario 4 in salesforce: Parent to child


-------------------------------
trigger BestPracticesforaccountandcon on account (after insert,after update) 
{
list<contact> lstcon = new list<contact>();
if(trigger.isafter && trigger.isinsert){
    for(account acc : trigger.new){
    contact con = new contact();
    con.lastname = acc.name;
    con.accountid = acc.id;
    con.email = acc.email__c;
    lstcon.add(con);
    
    }
    insert lstcon;
    }
if(trigger.isafter && trigger.isupdate){    
    for(contact con :[select id,lastname,email,accountid from contact where accountid in :trigger.newmap.keyset()]){
       con.accountid = trigger.newmap.get(con.accountid).id;
        con.lastname = trigger.newmap.get(con.accountid).name;
        con.email = trigger.newmap.get(con.accountid).email__c;
        lstcon.add(con);
    }
    }
update lstcon;
-------------------------------
Note :trigger UpdateActiveProspect1 on Opportunity (before update) {
           (or)

trigger UpdateActiveProspect1 on Opportunity (after insert,after update) {

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

   set<id> accids = new set<id>();

    list<account> updatelstacc = new list<account>();

    for(Opportunity opp : trigger.new){

      //  accids.add(opp.accountid);
        mapacc.put(opp.AccountId, opp);
           }
 //   for(account acc : [select id,name from account where id IN:accids]){

for(account acc : [select id,name from account where id IN :mapacc.keyset()]){
        if(mapacc.containsKey(acc.id)){
            if(mapacc.get(acc.id).stageName=='Qualification'||mapacc.get(acc.id).stageName=='Needs Analysis'||
 mapacc.get(acc.id).stageName=='Value Proposition'||mapacc.get(acc.id).stageName=='Proposal/Price Quote'||
                 mapacc.get(acc.id).stageName=='Id. Decision Makers'||mapacc.get(acc.id).stageName=='Perception Analysis'||
               mapacc.get(acc.id).stageName=='Negotiation/Review'){
                acc.Active_Prospect__c = true;
                 acc.Email__c = mapacc.get(acc.id).email__c;
            }else{
                acc.Active_Prospect__c = false;
                 acc.Email__c = mapacc.get(acc.id).email__c;
            }
            updatelstacc.add(acc);
        }
    }
  update updatelstacc;
}
--------------------------------------------------------------------------
Parent to Child:
trigger AcctoconTrigger on Account (before update,before delete) {
    if(trigger.isbefore && trigger.isupdate){
    list<contact> lstcon = new list<contact>();
    for(contact con : [select id,lastname,accountid,email from contact where accountid IN: trigger.newmap.keyset()]){
        if(trigger.newmap.containsKey(con.AccountId)){
            con.Email = trigger.newmap.get(con.AccountId).email__c;
            lstcon.add(con);
        }
    }
    if(lstcon.size()>0 || lstcon.size() != null){
        update lstcon;
    }
    }
    if(trigger.isbefore && trigger.isdelete){
     list<contact> lstcon1 = [select id,lastname,email from contact where accountid IN:trigger.oldmap.keyset()];
        delete lstcon1;
    }
------------------------------------------------------------------------

trigger AcctoconTrigger on Account (before update) {
    list<contact> lstcon = new list<contact>();

    for(contact con : [select id,lastname,accountid,email from contact where accountid IN :trigger.newmap.keyset()]){
    if(trigger.newmap.containsKey(con.AccountId)){
    con.Email =trigger.newmap.get(con.accountid).email__c;

            lstcon.add(con);
        }
    }
    if(lstcon.size()>0 || lstcon.size() != null){
        update lstcon;
    }
}
------------------------------------------------------------------------------
Child to Parent:

trigger childtoparent on Contact (before update) {
    list<account> lstacc = new list<account>();
    map<id,contact> mapcon = new map<id,contact>();
    for(contact con : trigger.new){
        mapcon.put(con.AccountId,con);
    }
    for(account acc :[select id,name,email__c from account where id IN:mapcon.keyset()]){
        acc.Email__c = mapcon.get(acc.Id).email;
        lstacc.add(acc);
    }
    if(lstacc.size()>0 || lstacc.size() != null){
        update lstacc;
    }
----------------------------------
Write like below :
trigger childtoparent on Contact (after update,after insert) {
    list<account> lstacc = new list<account>();
    for(contact con : trigger.new){
        if(con.RMA_Shipping_Contact_Only__c == true){
        account acc = new account(name = con.lastname,id = con.AccountId,email__c = con.Email);
        lstacc.add(acc);
    }
    }
    update lstacc;

}

When we run below code from Developer console get the error like:

list<contact> lstcon = new list<contact>();
contact con = new contact(lastname ='venkat',phone = '9916767632',email ='test@gmail.com',accountid='0012800001S1LTZ',RMA_Shipping_Contact_Only__c =true);
contact con1 = new contact(lastname ='venkat1',phone = '9916767632',email ='test1@gmail.com',accountid='0012800001S1LTZ',RMA_Shipping_Contact_Only__c =true);
lstcon.add(con);
lstcon.add(con1);
insert lstcon;

Error:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, childtoparent: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 0012800001S1LTZAA3 Trigger.childtoparent: line 9, column 1: []

No comments:

Post a Comment