+ Start a Discussion
SHAIK MOHAMMAD YASEENSHAIK MOHAMMAD YASEEN 

how to update existing records, insert new records, delete old record by comparing two lists(old list, new list) of sobject

Hi All,

I have a custom object custObj with fields Question__c, answer__c, i am querying old records from database into a list oldRecsList

now i have a new List newRecsList i am getting from UI which doesnot have ids, now i have to compare two lists check if Question__c of old list and Question__c of new list are same if yes then have to check if answer__c are different. based on the criteria i have to insert new records which are not in database and update the records with changes in to old records, delete the old records if they are not in new list.

I have tried iterating two lists but was unable achieve may be i was unable to include some complex logics. here is the sameple code. please help me wha are all should be considered and where was i doing wrong. Thanks

for(custObj__c newRec :newRecsList )
            {
                for(custObj__c savedRec: oldRecsList)
                {
                    if(savedRec.Question__c ==newRec.Question__c && savedRec.answer__c!= newRec.answer__c)
                    {
                       savedRec.answer__c=newRec.answer__c;
                        ListtoUpdateRecs.add(savedRec);
                    }
                    else if(savedRec.question__c!=newRec.question__c)
                    {
                        ListtodeleteRecs.add(savedRec);
                    }
                    else
                    {
                        ListtoInsertRecs.add(newRec);
                    }
                }
            }
            update ListtoUpdateRecs;
            delete ListtodeleteRecs;
            insert ListtoInsertRecs;
Best Answer chosen by SHAIK MOHAMMAD YASEEN
Malni Chandrasekaran 2Malni Chandrasekaran 2
Shaik,
You have not given details about where you are failing. Are you getting duplicate entries in all 3 lists.
If that is your problem, please try the below code.

Boolean oldLoopFlag = True;
for(custObj__c newRec :newRecsList )
            {
                oldLoopFlag = True; 
                for(custObj__c savedRec: oldRecsList)
                {
                    if(savedRec.Question__c ==newRec.Question__c )
                        { 
                            if (savedRec.answer__c!= newRec.answer__c)
                                 {
                                   savedRec.answer__c=newRec.answer__c;
                                   ListtoUpdateRecs.add(savedRec);
                                  }
                             else 
                             {
                              ListtodeleteRecs.add(savedRec);
                              }
                           oldLoopFlag = False;       // Sets to false when the new value is available in old list to avoid insert
                           break;         //To come out of the inner loop
                           } 
                      }   // End of Inner Loop  
                    if (oldLoopFlag)           // Checks if it is already in old list
                    {
                        ListtoInsertRecs.add(newRec);
                    }
                
            }
            update ListtoUpdateRecs;
            delete ListtodeleteRecs;
            insert ListtoInsertRecs;

Hope this helps!
You may let me know if you face any other problem than duplicate entry issue.
 

All Answers

Malni Chandrasekaran 2Malni Chandrasekaran 2
Shaik,
You have not given details about where you are failing. Are you getting duplicate entries in all 3 lists.
If that is your problem, please try the below code.

Boolean oldLoopFlag = True;
for(custObj__c newRec :newRecsList )
            {
                oldLoopFlag = True; 
                for(custObj__c savedRec: oldRecsList)
                {
                    if(savedRec.Question__c ==newRec.Question__c )
                        { 
                            if (savedRec.answer__c!= newRec.answer__c)
                                 {
                                   savedRec.answer__c=newRec.answer__c;
                                   ListtoUpdateRecs.add(savedRec);
                                  }
                             else 
                             {
                              ListtodeleteRecs.add(savedRec);
                              }
                           oldLoopFlag = False;       // Sets to false when the new value is available in old list to avoid insert
                           break;         //To come out of the inner loop
                           } 
                      }   // End of Inner Loop  
                    if (oldLoopFlag)           // Checks if it is already in old list
                    {
                        ListtoInsertRecs.add(newRec);
                    }
                
            }
            update ListtoUpdateRecs;
            delete ListtodeleteRecs;
            insert ListtoInsertRecs;

Hope this helps!
You may let me know if you face any other problem than duplicate entry issue.
 
This was selected as the best answer
SHAIK MOHAMMAD YASEENSHAIK MOHAMMAD YASEEN
Thanks Malni Chandrasekaran 2,

you really understood my problem , ii used your code and it working good for me. Thanks