+ Start a Discussion
Vasu.PVasu.P 

Trigger is not working while Updating through Dataloadeer

Hi All,

trigger UpdateActiveProspect on Opportunity (after insert,after update){
 list<account> ali=new list<account>();

  if(trigger.isafter && (trigger.isinsert || trigger.isupdate)){
     list<Opportunity> Opp=new list<Opportunity>();
     List<Opportunity> opp1=new List<Opportunity>();
     set<id> ids=new set<id>(); 
      for(Opportunity o:trigger.new){
       ids.add(o.accountid);
       }
     
     list<account> accli=[select id,name,Account_Status__c,Active_Prospect__c ,(select id,accountID,StageName,Contract_Expiration_Date__c from Opportunities) from account where id=:ids];
     for(account a1:accli){  
          for(Opportunity op:a1.opportunities){
            if(op.stageName=='Intro Meeting / Discovery'||op.stageName=='Relationship Building'||
                op.stageName=='Opportunity Identification'||op.stageName=='Solution/Submit RFI'||
                 op.stageName=='Solution/Submit RFP'||op.stageName=='Solution/Submit Pre-emptive Bid'||
                  op.stageName=='Presentation / Pitching'||op.stageName=='Red Zone / Awarded'){
                  
            opp.add(op);
              }
              if(op.stageName=='Win (Contract Signed)'&& op.Contract_Expiration_Date__c > System.TODAY()){
              opp1.add(op);
            }
            }
            if(opp.size()>0){
            a1.Active_Prospect__c=true;  
            }else{
        a1.Active_Prospect__c=false;

        } 
        if(opp1.Size()>0){
        a1.Account_Status__c='Active';
        }else{
        a1.Account_Status__c='Inactive';
        }
        ali.add(a1);
         
         }          
     }

   update ali;
}

Above is the Trigger. Any one can Help

Thanks In advance
Best Answer chosen by Vasu.P
rajat Maheshwari 6rajat Maheshwari 6

@Satya,

Please use this code and marked as best answer :)

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



     List<Opportunity> opp1=new List<Opportunity>();
Set<id> set_AccountId = new Set<id>();

for(Opportunity op:trigger.new)
 {
    set_AccountId.add(op.AccountId);
  }
   
Map<Id,Account> mp_Account = new Map<Id,Account>([Select Id,Active_Prospect__c,Account_Status__c from Account where Id IN:set_AccountId]);

  
          for(Opportunity op:trigger.new){
            if((op.stageName=='Intro Meeting / Discovery'||op.stageName=='Relationship Building'||
                op.stageName=='Opportunity Identification'||op.stageName=='Solution/Submit RFI'||
                 op.stageName=='Solution/Submit RFP'||op.stageName=='Solution/Submit Pre-emptive Bid'||
                  op.stageName=='Presentation / Pitching'||op.stageName=='Red Zone / Awarded') && mp_Account!=null && mp_Account.containsKey(op.AccountId))
                            {
                         mp_Account.get(op.AccountId).Active_Prospect__c = true;
                             }

                    else if(mp_Account!=null && mp_Account.containsKey(op.AccountId))
                      {   
                             
                              mp_Account.get(op.AccountId).Active_Prospect__c = false;
                              }
             if(op.stageName=='Win (Contract Signed)' && op.Contract_Expiration_Date__c > System.TODAY() && mp_Account!=null && mp_Account.containsKey(op.AccountId))
            {    
              mp_Account.get(op.AccountId).Account_Status__c = 'Active';
            }
            
        else if(mp_Account!=null && mp_Account.containsKey(op.AccountId))
             
           {
              mp_Account.get(op.AccountId).Account_Status__c = '';
            }

}
 
if(mp_Account!=null && mp_Account.values()!=null)
     update mp_Account.values();
}

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

All Answers

rajat Maheshwari 6rajat Maheshwari 6




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

Map<Id,Account> mp_Account = new Map<Id,Account>();

     List<Opportunity> opp1=new List<Opportunity>();
   
  
          for(Opportunity op:trigger.new){
            if(op.stageName=='Intro Meeting / Discovery'||op.stageName=='Relationship Building'||
                op.stageName=='Opportunity Identification'||op.stageName=='Solution/Submit RFI'||
                 op.stageName=='Solution/Submit RFP'||op.stageName=='Solution/Submit Pre-emptive Bid'||
                  op.stageName=='Presentation / Pitching'||op.stageName=='Red Zone / Awarded'){
                     op.Account.Active_Prospect__c = true;
                      mp_Account.put(op.AccountId,op.Account);
                   }
            else if(op.stageName=='Win (Contract Signed)'&& op.Contract_Expiration_Date__c > System.TODAY()){
              op.Account.Active_Prospect__c = true;
              mp_Account.put(op.AccountId,op.Account);
            }
            }
 
if(mp_Account!=null && mp_Account.values()!=null)
     update mp_Account.values();
}


@Satya, Use this code and let me know the results :)

 

Thanks
Rajat maheshwari
rajatzmaheshwari@gmail.com

Vasu.PVasu.P
Thanks Rajat,

i have made some Change in the code ,

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

Map<Id,Account> mp_Account = new Map<Id,Account>();

     List<Opportunity> opp1=new List<Opportunity>();
   
  
          for(Opportunity op:trigger.new){
            if(op.stageName=='Intro Meeting / Discovery'||op.stageName=='Relationship Building'||
                op.stageName=='Opportunity Identification'||op.stageName=='Solution/Submit RFI'||
                 op.stageName=='Solution/Submit RFP'||op.stageName=='Solution/Submit Pre-emptive Bid'||
                  op.stageName=='Presentation / Pitching'||op.stageName=='Red Zone / Awarded'){
                     op.Account.Active_Prospect__c = true;
                      mp_Account.put(op.AccountId,op.Account);
                   }
            else if(op.stageName=='Win (Contract Signed)'&& op.Contract_Expiration_Date__c > System.TODAY()){
              op.Account.Account_Status__c='Active';
              mp_Account.put(op.AccountId,op.Account);
            }
            else{
            op.Account.Account_Status__c='';
           }
            }
 
if(mp_Account!=null && mp_Account.values()!=null)
     update mp_Account.values();
}


when i tried to add an Opportunity it's giving the : caused by: System.NullPointerException: Attempt to de-reference a null object  

Please Suggest!
 
Vasu.PVasu.P
Hi Rajat, Thanks for your time, I just made some changes in the Code as per my Req. when i tried to add an Opportunity it's giving the *caused by: System.NullPointerException: Attempt to de-reference a null object* here is the Code i have modified: trigger UpdateActiveProspect on Opportunity (after insert,after update){ Map mp_Account = new Map(); List opp1=new List(); for(Opportunity op:trigger.new){ if(op.stageName=='Intro Meeting / Discovery'||op.stageName=='Relationship Building'|| op.stageName=='Opportunity Identification'||op.stageName=='Solution/Submit RFI'|| op.stageName=='Solution/Submit RFP'||op.stageName=='Solution/Submit Pre-emptive Bid'|| op.stageName=='Presentation / Pitching'||op.stageName=='Red Zone / Awarded'){ op.Account.Active_Prospect__c = true; mp_Account.put(op.AccountId,op.Account); } else if(op.stageName=='Win (Contract Signed)'&& op.Contract_Expiration_Date__c > System.TODAY()){ op.Account.Account_Status__c='Active'; mp_Account.put(op.AccountId,op.Account); } else{ op.Account.Account_Status__c=''; } } if(mp_Account!=null && mp_Account.values()!=null) update mp_Account.values(); } Can u Please Assist. Thanks Once Again !
rajat Maheshwari 6rajat Maheshwari 6

Hi Satya,

Please give a try with that : - 

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

Map<Id,Account> mp_Account = new Map<Id,Account>();

     List<Opportunity> opp1=new List<Opportunity>();
   
  
          for(Opportunity op:trigger.new){
            if(op.stageName!=null)
              {
            if(op.stageName=='Intro Meeting / Discovery'||op.stageName=='Relationship Building'||
                op.stageName=='Opportunity Identification'||op.stageName=='Solution/Submit RFI'||
                 op.stageName=='Solution/Submit RFP'||op.stageName=='Solution/Submit Pre-emptive Bid'||
                  op.stageName=='Presentation / Pitching'||op.stageName=='Red Zone / Awarded'){
                     op.Account.Active_Prospect__c = true;
                      mp_Account.put(op.AccountId,op.Account);
                   }
            else if(op.stageName=='Win (Contract Signed)'&& op.Contract_Expiration_Date__c > System.TODAY()){
              op.Account.Account_Status__c='Active';
              mp_Account.put(op.AccountId,op.Account);
            }
        }
            
            }
 
if(mp_Account!=null && mp_Account.values()!=null)
     update mp_Account.values();
}


Let me know the line number, where it gives error.

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

rajat Maheshwari 6rajat Maheshwari 6

@Satya,

Please use this code and marked as best answer :)

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



     List<Opportunity> opp1=new List<Opportunity>();
Set<id> set_AccountId = new Set<id>();

for(Opportunity op:trigger.new)
 {
    set_AccountId.add(op.AccountId);
  }
   
Map<Id,Account> mp_Account = new Map<Id,Account>([Select Id,Active_Prospect__c,Account_Status__c from Account where Id IN:set_AccountId]);

  
          for(Opportunity op:trigger.new){
            if((op.stageName=='Intro Meeting / Discovery'||op.stageName=='Relationship Building'||
                op.stageName=='Opportunity Identification'||op.stageName=='Solution/Submit RFI'||
                 op.stageName=='Solution/Submit RFP'||op.stageName=='Solution/Submit Pre-emptive Bid'||
                  op.stageName=='Presentation / Pitching'||op.stageName=='Red Zone / Awarded') && mp_Account!=null && mp_Account.containsKey(op.AccountId))
                            {
                         mp_Account.get(op.AccountId).Active_Prospect__c = true;
                             }

                    else if(mp_Account!=null && mp_Account.containsKey(op.AccountId))
                      {   
                             
                              mp_Account.get(op.AccountId).Active_Prospect__c = false;
                              }
             if(op.stageName=='Win (Contract Signed)' && op.Contract_Expiration_Date__c > System.TODAY() && mp_Account!=null && mp_Account.containsKey(op.AccountId))
            {    
              mp_Account.get(op.AccountId).Account_Status__c = 'Active';
            }
            
        else if(mp_Account!=null && mp_Account.containsKey(op.AccountId))
             
           {
              mp_Account.get(op.AccountId).Account_Status__c = '';
            }

}
 
if(mp_Account!=null && mp_Account.values()!=null)
     update mp_Account.values();
}

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
This was selected as the best answer
Vasu.PVasu.P
Thank You Very Much Rajat! 

It's Working Fine