+ Start a Discussion
❤Code❤Code 

Trigger not working with data loader

Hi All,

I have an custom object where i have 3 record types. There is a trigger on this object which will calucate serial no for each record type and it will insert.

Ex - 
Custom Object X__c.
Record Types = A, B , C
if i select record type A then one custom field will get inserted by A001.
if i select record type B then one custom field will get inserted by B001. 

When i am uploading the data with data loader with batch size 20, for 20 records with record type A is having the same serial no A001.

Need help how to resolve this.
goabhigogoabhigo
Can you post the code snippet? I am sure there is something missing.

--
Abhi
JconsultantJconsultant
Sounds like you're checking the record type for the first record then applying the same logic to the entire list (probably using trigger.new in this case).  If you can post the trigger code we can provide better help.
❤Code❤Code
Hi Abhi,
Bleow is the code snipet - 

trigger ProjectOnUpdate on Project__c(before insert, before update, after update) 
{
   if(Trigger.isInsert){
    Integer samplecount;
    
    Integer Year = Date.Today().Year();
    Integer Month = Date.Today().Month();
    Project__c settings = Project__c.getInstance('SampleID');
    ID bsg = settings.BSG__c;
    ID it = settings.BS_IT__c;
    ID opex = settings.Operation_Excellence__c;
    ID cp = settings.Create_Project__c;
    
    for(Project__c p : Trigger.new){ 
        if(p.RecordTypeId == bsg) {
            samplecount= [SELECT count() FROM Project__c WHERE RecordTypeId =: p.RecordTypeId];
            p.Number__c = samplecount+1;
 
            String s = String.valueOf(p.Number__c);
            while (s.length() < 4) s = '0' + s;
            p.Project_Serial_Number__c = 'BSG-'+Date.Today().Month()+'-'+Date.Today().Year()+'-S-'+s;
            }
         if(p.RecordTypeId == it) {
            samplecount= [SELECT count() FROM Project__c WHERE RecordTypeId =: p.RecordTypeId];
            p.Number__c = samplecount+1;
            
            String s = String.valueOf(p.Number__c);
            while (s.length() < 4) s = '0' + s;
            p.Project_Serial_Number__c = 'BS IT-'+Date.Today().Month()+'-'+Date.Today().Year()+'-S-'+s;
            } 
         if(p.RecordTypeId == opex) {
            samplecount= [SELECT count() FROM Project__c WHERE RecordTypeId =: p.RecordTypeId];
            p.Number__c = samplecount+1;

            String s = String.valueOf(p.Number__c);
            while (s.length() < 4) s = '0' + s;
            p.Project_Serial_Number__c = 'OPEX-'+Date.Today().Month()+'-'+Date.Today().Year()+'-S-'+s;
            } 
         if(p.RecordTypeId == cp) {
            samplecount= [SELECT count() FROM Project__c WHERE RecordTypeId =: p.RecordTypeId];
            p.Number__c = samplecount+1;
            
            String s = String.valueOf(p.Number__c);
            while (s.length() < 4) s = '0' + s;
            p.Project_Serial_Number__c = p.Department_Name__c+'-'+Date.Today().Month()+'-'+Date.Today().Year()+'-S-'+s;
            } 
    }
  
  }  
JconsultantJconsultant
The first thing I would do is remove your SOQL queries from your FOR loop.  This is a best practice that will prevent you from hitting query limits when inserting or updating large numbers of records.  You could have an integer variable for each of your count queries then increment them just as you are within the for loop.  I'm not seeing anything that stands out as to why you are seeing the functionality you describe.  Other than making sure you have the correct IDs stored in your custom setting and make sure they match the IDs in your dataload file.