You need to sign in to do that
Don't have an account?
SHAIK 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;
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;
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;
{
if(savedRec.Question__c ==newRec.Question__c )
{
{
savedRec.answer__c=newRec.answer__c;
ListtoUpdateRecs.add(savedRec);
}
{
ListtodeleteRecs.add(savedRec);
}
break; //To come out of the inner loop
}
} // End of Inner Loop
{
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
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;
{
if(savedRec.Question__c ==newRec.Question__c )
{
{
savedRec.answer__c=newRec.answer__c;
ListtoUpdateRecs.add(savedRec);
}
{
ListtodeleteRecs.add(savedRec);
}
break; //To come out of the inner loop
}
} // End of Inner Loop
{
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.
you really understood my problem , ii used your code and it working good for me. Thanks