You need to sign in to do that
Don't have an account?
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".
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.
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.
Here is your code.
Please check and mark as best aswer if you like this.
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());
}
}
}
Try this trigger : Regards,
Ajay
Currently my existing logic works when i manually create performance records for vendors.
Only during data upload it fails...:(