You need to sign in to do that
Don't have an account?
Problem with a trigger when bulk data import from dataloader
trigger IncrementData on IB_Data__c (before insert) {
List<IB_Data__c> objOldI = [select id,IncrementNO__c from IB_Data__c orderby createddate desc limit 1];
for(IB_Data__c objI : trigger.new){
if(objOldI.size()>0){
if(objOldI[0].IncrementNo__c != null){
objI.IncrementNo__c = objOldI[0].incrementNo__c + 1;
}
}
else{
objI.incrementNo__c = 1;
}
}
}
Hi guys i have written above trigger. Its working fine with record by recod insertion. But when i import the bulk data trhough data loader its not working and showing incrementNo__c is only '1' for all the records. But as per my requirement based on record count i have to increase this number.
In this situation all the records that are imported by data loader is having the same createddate and time. So its showing 1 for all the records. How can i increase this number when i import the data through data loader for all the records imported.
Please let me give your suggestions.
You didn't increament the counter.
Maybe the code should be something like this:
Hi ,
Thanks for your reply.
But its not showing the incremented number.
Result is same as the first one.
Hello SFDC_Leander,
I think the first mistake is that "objOldI[0].incrementNo__c" is fixed in the entire process.
Therefore, it will always be a fixed value +1 and will give the same value for all of your bulk inserts.
Also you should try to limit the amount of IF statements inside the FOR loop.
The way i would write the trigger is as follows:
trigger IncrementData on IB_Data__c (before insert) {
List<IB_Data__c> objOldI = [select id,IncrementNO__c from IB_Data__c where isDeleted=false and IncrementNO__c!=null orderby createddate desc limit 1]; //isDeleted and null-checking will help performance in the long-run
Integer myCountInteger = 0; //set default to 0 and doesnt require an else statement
if(!objOldI.isEmpty()) //faster than counting the records & we moved this outside the for statement so it executes only once
{
myCountInteger = objOldI[0].incrementNo__c; //set the initial counter if we found records
}
for(IB_Data__c objI : trigger.new){
objI.IncrementNo__c = myCountInteger++; //increments to 1 if we have no data OR increments from the value we got from the query.
}
}
I hope I answered your question.
Try this code may be halpful
trigger IncrementData on IB_Data__c (before insert) {
List<IB_Data__c> temp=new List<IB_Data__c>();
List<IB_Data__c> objOldI = [select id,IncrementNO__c from IB_Data__c orderby createddate desc limit 1];
for(IB_Data__c objI : trigger.new){
if(objOldI.size()>0){
if(objOldI[0].IncrementNo__c != null){
objI.IncrementNo__c = objOldI[0].incrementNo__c + 1;
}
else{
objI.incrementNo__c = 1;
}
}
else{
objI.incrementNo__c = 1;
}
temp.add(objI);
insert temp; // other wise update temp; (or) use upsert
}
}
If it helpful plz make it as solution for others it may benfit
Prem