You need to sign in to do that
Don't have an account?
help needed in Handling bulk records
HI ,
Hepl needed in writing the code the code in such a way that it handles bulk records
Here is the code and iam not sure if it handles the bulk records
public with sharing class SalesTargetHandler {
Map<String, Sales_Target__c> offCode = new Map<String, Sales_Target__c>(); Sales Target id
Set<ID> salesTargetId = new Set<ID>();
public SalesTargetHandler(){
}
public void OnBeforeInsert(Sales_Target__c[] newRecords){
for(Sales_Target__c newSalesTarget : newRecords){
offCode.put(newSalesTarget.Officer_Code__c, newSalesTarget);
for(Sales_Target__c salesTarget :[Select Id ,Officer_Code__c, Target_Period_Start__c ,Target_Period_End__c from Sales_Target__c where Officer_Code__c IN :offCode.keySet()])
{
Sales_Target__c st1 = offCode.get(newSalesTarget.Officer_Code__c);
if(salesTarget.Officer_Code__c == st1.Officer_Code__c &&
((salesTarget.Target_Period_Start__c <= newSalesTarget.Target_Period_Start__c &&
salesTarget.Target_Period_End__c >= newSalesTarget.Target_Period_Start__c)
|| (salesTarget.Target_Period_Start__c<= newSalesTarget.Target_Period_End__c &&
salesTarget.Target_Period_End__c>= newSalesTarget.Target_Period_End__c)
)){
String errorMsg = System.Label.Sales_Target_Error_Message;
newSalesTarget.addError(errorMsg);
}
}
}
}
Thanks
You need to pull all SOQL queries and DML statements out of your loops - use Lists, Sets, and Maps to bulkify.
-Andy
You have a good start, I think you are just missing a bracket after your first for loop that creates the map:
Hi Starz26
Thanks for the reply. Iam the same one holding both usernames . developer_force is also mine.
I tried ur way but it'sgiving me the error as
variable newSalesTarget.Target_Period_Start__c does not exist.
Thanks
I did not see that you were using that first for variable in subsequent lines. This will work as far as the code goes, not sure if it meets your use case or not....
**You may need to create a Set<ID> in the first for loop to hold the ID of the newRecords and then use that in the second for loop as the bind variable in the WHERE clause depending on what you are trying to return...***
Hi Starz26,
Thanks for the reply
No, I guess the logic there is not right,
The requirement we need to meet is
if(existing. Officer code = new . officer code AND existing.Target_Period_Start__c <=new.Target_Period_Start__c AND existing.Target_Period_End__c >=new.Target_Period_End__c OR ...........)
throw an error message.
Not sure how to compare the new records which are going to be inserted with the old records
Thanks
You will have to SOQL for the old records that you wish to search through then compar the old, vs new....
What fields relate the old with the new?
The fields that relate old to new are
Officer_Code__c , Target_Period_Start__c , Target_Period_End__c of the Sales_Target__c object
Thanks