+ Start a Discussion
Sushma  RamakrishnanSushma Ramakrishnan 

Need Help In After Insert Trigger Logic

Hi All,

I have a logic on one object that like when a new record is created the status should be set as "Current" and when the second record is created for the same object the status of the previous record should become "Previous" and current record should become "Current".
trigger UpdatePerformanceStatusTrigger on Performance__c (after insert)
{

 UpdatePerformanceStatusUtils.updatePerformanceStatus(Trigger.new);  
  
}


public class UpdatePerformanceStatusUtils
{

//Update existing Performance record Status On Creation Of a New Performance Record

    public static void updatePerformanceStatus(List<Performance__c> performanceList)
    {
      List<Performance__c> objPerformance1 = new List<Performance__c>();
      try
      {
         String vendorId = performanceList[0].Vendor__c; 
         String performanceId = performanceList[0].Id;          
         List<Performance__c> performanceRecords = [select Id , Performance_Status__c from Performance__c where id !=: performanceId and Performance_Status__c = 'Current Record' and Vendor__c =: vendorId];
         for(Performance__c performance1: performanceRecords)
         {
             performance1.Performance_Status__c = 'Previous Record';
             objPerformance1.add(performance1); 
         }
         update objPerformance1;
       }
       catch(DmlException e)
       {
           System.debug('The following exception has occurred: ' + e.getMessage()); 
       }          
    }
    
}

Each Vendor object has many Performance records.When i do this manually it works but when i do it in bulk it doesnt meaning all records have by default status as "Current".
Thanks for any help in advance.
KeshabKeshab
Hi  Sushma 

Here is your code.
 
trigger UpdatePerformanceStatusTrigger on Performance__c (after insert)
{

 UpdatePerformanceStatusUtils.updatePerformanceStatus(Trigger.new,Trigger.newMap);  
  
}


 
trigger UpdatePerformanceStatusTrigger on Performance__c (after insert)
{

 UpdatePerformanceStatusUtils.updatePerformanceStatus(Trigger.new,Trigger.newMap);  
  
}


public class UpdatePerformanceStatusUtils
{

//Update existing Performance record Status On Creation Of a New Performance Record

    public static void updatePerformanceStatus(map<id,Performance__c> performanceNewMap)
    {
      List<Performance__c> objPerformance1 = new List<Performance__c>();
      try
      {
		  Set<id>vendorIds=new Set<id>();
		  for(Performance__c perf : performanceNewMap.values()){			  
			  vendorIds.add(perf.Vendor__c);
		  }
		  
         String vendorId = performanceList[0].Vendor__c; 
         String performanceId = performanceList[0].Id;          
         List<Performance__c> performanceRecords = [select Id , Performance_Status__c from Performance__c where id not in =: performanceNewMap.keyset() and Performance_Status__c = 'Current Record' and Vendor__c in :vendorIds];
         for(Performance__c performance1: performanceRecords)
         {
             performance1.Performance_Status__c = 'Previous Record';
             objPerformance1.add(performance1); 
         }
         update objPerformance1;
       }
       catch(DmlException e)
       {
           System.debug('The following exception has occurred: ' + e.getMessage()); 
       }          
    }
    
}


Please check and mark as best aswer if you like this. 
KeshabKeshab
In the bove post 
 
UpdatePerformanceStatusUtils.updatePerformanceStatus(Trigger.newMap);
//////////////
public static void updatePerformanceStatus(map<id,Performance__c> performanceNewMap){

}

was not chnaged.


trigger UpdatePerformanceStatusTrigger on Performance__c (after insert)
{

 UpdatePerformanceStatusUtils.updatePerformanceStatus(Trigger.newMap);  
  
}


public class UpdatePerformanceStatusUtils
{

//Update existing Performance record Status On Creation Of a New Performance Record

    public static void updatePerformanceStatus(map<id,Performance__c> performanceNewMap)
    {
      List<Performance__c> objPerformance1 = new List<Performance__c>();
      try
      {
          Set<id>vendorIds=new Set<id>();
          for(Performance__c perf : performanceNewMap.values()){              
              vendorIds.add(perf.Vendor__c);
          }
          
         String vendorId = performanceList[0].Vendor__c; 
         String performanceId = performanceList[0].Id;          
         List<Performance__c> performanceRecords = [select Id , Performance_Status__c from Performance__c where id not in =: performanceNewMap.keyset() and Performance_Status__c = 'Current Record' and Vendor__c in :vendorIds];
         for(Performance__c performance1: performanceRecords)
         {
             performance1.Performance_Status__c = 'Previous Record';
             objPerformance1.add(performance1); 
         }
         update objPerformance1;
       }
       catch(DmlException e)
       {
           System.debug('The following exception has occurred: ' + e.getMessage()); 
       }          
    }
    
}
Ajay K DubediAjay K Dubedi
Hi Sushma,
Try this trigger :
trigger testtrigger on Performance__c (before insert) {
    List<Performance__c> listtobeUpdated = new List<Performance__c>();
    set<Id> setofVendorID = new set<Id>();
    for(Performance__c pr : trigger.new){
        setofVendorID.add(pr.Vendor__C);
        pr.Performance_Status__c = 'Current';
    }
    listtobeUpdated = [SELECT Performance_Status__c from Performance__c where Performance_Status__c = 'Current' and Vendor__C In :setofVendorID];
    for(Performance__c prupdate : listtobeUpdated){
        if(!listtobeUpdated.isempty()){
           prupdate.Performance_Status__c = 'Previous';  
       }
    }
    update listtobeUpdated;
}
Regards,
Ajay
 
Sushma  RamakrishnanSushma Ramakrishnan
Sorry the above work around didnt work out....:(
Currently my existing logic works when i manually create performance records for vendors.
Only during data upload it fails...:(