+ Start a Discussion
Ashu sharma 38Ashu sharma 38 

Upsert Problem

Hello,

I am doing Upsert Operation....
Below is my code.
I have a problem while performing Upsert operation:
Scenirio:
Firstly i have to insert a records
then perform Upsert Operation.
I am using external Id while performing DML (Non Atomic)
The records in inserted insted of upsert .




public class upsertDatabase {
    
    public list<Customer_Project__c> cPlist;
    
    public upsertDatabase(){
        cPlist=new list<Customer_Project__c>();
        list<Opportunity> opList=[select id,name from Opportunity where name='Sapna'];
        // insert the new Records
        for(Opportunity ops:opList){
            Customer_Project__c c1=new Customer_Project__c();
            c1.Name='Timakshi';
            c1.ExternalID__c=111;
           c1.Opportunity__c=ops.id;
            c1.Payment__c=78787;
            cPlist.add(c1);
        }
        database.SaveResult[] sr=database.insert(cpList,false);
        for(database.SaveResult s:sr){
            if(s.isSUCCESS()){
                system.debug('success Message' +s.getID());
            }
            else{
                database.Error[] myError=s.getErrors();
                for(database.Error er:myError){
                    system.debug('Error Mesaage for Inserting the records' +er.getMessage());    
                }
            }
        }
// upsert the Records 
        for(Opportunity oops:opList){
            Customer_Project__c c2=new Customer_Project__c();
            c2.Name='Taniya';
            c2.Opportunity__c=oops.id;
            c2.Payment__c=8888855;
            c2.ExternalID__c=989;
            cPlist.add(c2);
        }
        for(Opportunity oops:opList){
            Customer_Project__c c3=new Customer_Project__c();
            c3.Name='Geta';
            c3.Opportunity__c=oops.id;
            c3.Payment__c=8744;
            c3.ExternalID__c=99;
            cPlist.add(c3);
        }
        database.UpsertResult[] UpResult=database.upsert(cPlist,false);
        
        for(database.UpsertResult uR:UpResult){
            if(uR.isSUCCESS()){
                system.debug('Success for Upserting the records' +uR.getId());
            }
            else{
                database.Error[] myUpsertError=uR.getErrors();
                for(database.Error ero:myUpsertError){
                    system.debug('My upsert Errors Message' +ero.getMessage());
                }
                }
            }
        }
        
    }
Best Answer chosen by Ashu sharma 38
Niraj Kr SinghNiraj Kr Singh
Hi Nitish,

External Id filed (ExternalID__c) concept will work on the uniqueness of record and will check either that value is existing for any records or not.
But While you are preparing records for UPSERT not providing ExternalID__c field value which is existing.
 Like you are insering record with this value:
c1.ExternalID__c=111;

Then for update opertion using USERT you have provide same value:
c2.ExternalID__c=111; //It wiil be for update.
c2.ExternalID__c=989; //It will for Insert.

Hope it will help you!!

Thank
Niraj

All Answers

Niraj Kr SinghNiraj Kr Singh
Hi Nitish,

External Id filed (ExternalID__c) concept will work on the uniqueness of record and will check either that value is existing for any records or not.
But While you are preparing records for UPSERT not providing ExternalID__c field value which is existing.
 Like you are insering record with this value:
c1.ExternalID__c=111;

Then for update opertion using USERT you have provide same value:
c2.ExternalID__c=111; //It wiil be for update.
c2.ExternalID__c=989; //It will for Insert.

Hope it will help you!!

Thank
Niraj
This was selected as the best answer
Ashu sharma 38Ashu sharma 38
Yes its works,,some sort of change i have done,