function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC Developer1SFDC Developer1 

Apex upsert not working

I have two object temp__c and NFSave___c. I am dumping all the data in temp object through data loader in rich text area field(Section__c).then i am sending the data with apex class from Section field to NFSave object fields as per the column position.I am not able to perform upsert as duplicate records are adding in NFSave object.I have mark SSN__c field from NFSave object as external id.Please help as i am stuck here from so long.


global class NJImport implements Database.Batchable<sObject>{
    
global Database.QueryLocator start(Database.BatchableContext BC) {

String query = 'select Section__c from temp__c';

return Database.getQueryLocator(query);

}


global void execute(Database.BatchableContext BC, List<temp__c> accList){

     String textImportSection;

    
     List< NFSave___c > list = new List< NFSave___c>();
     for(NFSave___c Import : accList){
         
     textImportSection =  Import.Section__c;
     if(textImportSection !=null && textImportSection !=''){
     NFSave___c Obj = new NFSave___c();
     Obj.PAAD_SSN__c = textImportSection.mid(0,9);
     list.add(Obj); 
    }
}

upsert list;

}
mukesh guptamukesh gupta
Hi SFDC,

Please use below code:-
 
global class NJImport implements Database.Batchable<sObject>{
    
global Database.QueryLocator start(Database.BatchableContext BC) {

String query = 'select Section__c from temp__c';

return Database.getQueryLocator(query);

}


global void execute(Database.BatchableContext BC, List<temp__c> accList){

     String textImportSection;

    
     List< NFSave___c > list = new List< NFSave___c>();
     for(temp__c Import : accList){
         
     textImportSection =  Import.Section__c;
     if(textImportSection !=null && textImportSection !=''){
     NFSave___c Obj = new NFSave___c();
     Obj.PAAD_SSN__c = textImportSection.mid(0,9);
     list.add(Obj); 
    }
}

upsert list;

}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
Suraj Tripathi 47Suraj Tripathi 47
Hi SFDC,

you are using the wrong List type  in for loop " for(NFSave___c Import: accList)"
instead of 'NFSave___c'  you have to use 'temp__c' in for loop.

Here is the updated code:
global class NJImport implements Database.Batchable<sObject>{
    
global Database.QueryLocator start(Database.BatchableContext BC) {

String query = 'select Section__c from temp__c';

return Database.getQueryLocator(query);

}


global void execute(Database.BatchableContext BC, List<temp__c> accList){

     String textImportSection;

    
     List< NFSave___c > list = new List< NFSave___c>();
     for(temp__c Import : accList){
         
     textImportSection =  Import.Section__c;
     if(textImportSection !=null && textImportSection !=''){
     NFSave___c Obj = new NFSave___c();
     Obj.PAAD_SSN__c = textImportSection.mid(0,9);
     list.add(Obj); 
    }
}

upsert list;

}
Thanks.
sunil giri 20sunil giri 20
your site is great, and it's helping us a lot.  https://play.google.com/store/apps/details?id=com.zeasoft.videoplayer