+ Start a Discussion
sasya.ravisasya.ravi 

Help on Opportunity Split trigger

Hi All,
Opportunity having lookup with Sales Target and master detail with Opportunity.
I want to update Sales Target field in Opportunity Split,based on common values of 3 fields in Sales target_c custom object.
and opportunity fields.
I am able to update the "Sales Target" field while creating the opportunity and editing ooportunity split and adding one more split person and saving the the split it is not updating.once again if i am editing and saving the record it is updating.
But field value must be update while saving the record for first time.

here is my class & Trigger.

public  class SalesTargetUpdationOnOppSplit {
    
   public static void Targetupdation(list<OpportunitySplit> oppsplt)
     {
         if(SalesTargetRecurtion.vbool==true)
           {
               SalesTargetRecurtion.vbool=false;
         Map<Id, Sales_Target__c> TargetMap=new Map<Id, Sales_Target__c>([Select Id, RecordTypeId,Sales_Person__c,   Year__c,    Q1__c,Q2__c,Q3__c,Q4__c,Depot__c,Hub__c,January__c,February__c  ,March__c,April__c,May__c,June__c,July__c,August__c,September__c,October__c,November__c,December__c, JobType__c from Sales_Target__c]);
         Map<string, String> salesTargetMap=new Map<string, string>();
         for(Id targetMapIds: TargetMap.keySet())
         {
             String personId = TargetMap.get(targetMapIds).Sales_Person__c;
             String targetMapKey=TargetMap.get(targetMapIds).JobType__c+String.ValueOf(TargetMap.get(targetMapIds).Year__c)+personID.subString(0,15);
             string targetMapValue=targetMapIds;
             //system.debug('targetMapValue'+targetMapValue);
             system.debug('#####Target Map Key = '+targetMapKey);
             salesTargetMap.put(targetMapKey, targetMapValue);
         }

//Logic is to compare the Opportunity Job type, Sales Person /Opp Owner , Year of Opportunity Booking and associate to appropriate opportunity record.
      Map<Id, OpportunitySplit> opps = new Map<Id, OpportunitySplit>([SELECT  id,
                                                        Opportunity.Job_Type__c,
                                                        Opportunity.CloseDate,
                                                        SplitOwnerid,
                                                        Sales_Target__c
                                                        FROM OpportunitySplit where id in : oppsplt]);
                                                 
                                                        
  list<OpportunitySplit> oppsplits = opps.values(); 
               
       list<String> vlststr=new list<string>();
       list<OpportunitySplit> vlsttoupdate = new list<OpportunitySplit>();
         system.debug('salesTargetMap=='+salesTargetMap);
       for(OpportunitySplit op: oppsplits)
       {               
           string splitOwnId = op.SplitOwnerid;
           system.debug('SplitOwnerId'+splitOwnId);
           system.debug('Jobtype'+op.Opportunity.Job_Type__c+ 'Year---' + String.ValueOf(op.Opportunity.CloseDate.year())+'ownerid'+splitOwnId);
           String targetKey= op.Opportunity.Job_Type__c+String.ValueOf(op.Opportunity.CloseDate.year())+splitOwnId.subString(0,15);
           system.debug('###Record id to be insert--->'+salesTargetMap.get(targetKey));
           system.debug('#####targetKey--->'+ targetKey);
           vlststr.add(targetKey);
           if(!vlststr.isEmpty())
           {
               op.Sales_Target__c=salesTargetMap.get(targetKey);
               system.debug('###sales target-->'+op.Sales_Target__c);
               vlsttoupdate.add(op);
               system.debug('####vlsttoupdate'+vlsttoupdate);
               
           }
           
       }
           
            if(!vlsttoupdate.isEmpty())
            {
                    system.debug('#Before vlsttoupdate1'+ vlsttoupdate);
                    Database.upsert(vlsttoupdate,false) ;
                    system.debug('vlsttoupdate1'+ vlsttoupdate);
            
           }
            system.debug('Inserted Records---->'+vlsttoupdate.size());
     }
    
}    
   
}

**************************************************************
trigger updateQuoteSplitOnOpportunitySplitEdit on OpportunitySplit (after insert,after update) {
 //if(testIfAddSplitEditSplitDone.runOnce()) { 
  //Start---03-
        //Build Map Keys for Split Type Id 
        //start--
       
           if(Trigger.isInsert && Trigger.isAfter )
           {
               system.debug('@@@@Entering insert');
               SalesTargetUpdationOnOppSplit.Targetupdation(trigger.new);
               system.debug('@@@@Exiting insert');
           }
          if(Trigger.isUpdate && Trigger.isAfter )
           {
               system.debug('@@@@Entering Update');
               SalesTargetUpdationOnOppSplit.Targetupdation(trigger.old);
               system.debug('@@@@Exiting Update');
           }
       }