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
Shreya SalesforceShreya Salesforce 

How to avoid overwrite a list over another list under the for loop??

 I collect the data in the list<Test_object__c> through OpportunityLineItem Id.  One List is coming and list size will be 1. Another List is coming, again list size is 0. But I want to store every List and store it into 'TestObjectJson' class. How it is possible??
List<Test_object__c> testList =[SELECT Id,Name,Product__c,Product__r.Name,Opportunity__c,Value1__c,Value2__c,Value3__c,OpportunityLineItemId_del__c FROM Test_object__c WHERE OpportunityLineItemId_del__c =:oid]; 
                List<Test_object__c> finaltestList= new List<Test_object__c>();
                System.debug('testObject List==' +testList);
                System.debug('testList == ' +testList.size()); 
                
                if(testList.size()>0)
                {   
                    oobj.testobject= new TestObjectJson[testList.size()];
                    for(Integer b=0;b<testList.size();b++)
                    {
                    //Test_object__c tobj = testList[0];
                    //System.debug('tname : '+tobj.Name); 
                    finaltestList.add(testList[b]);
                    System.debug('finaltestList == ' +finaltestList.size()); 
                    
                    Test_object__c tobj = testList[b];
                    TestObjectJson t = new TestObjectJson();
                    t.Name = tobj.Name;            
                    System.debug('t.Name : '+t.Name);
                    t.Product = tobj.Product__c;
                    System.debug('t.Product : '+t.Product);
                    t.ProductName = tobj.Product__r.Name;
                    System.debug('t.ProductName :' +t.ProductName);
                    t.Value1 = tobj.Value1__c;
                    System.debug('t.Value1 : '+t.Value1);
                    t.Value2 = tobj.Value2__c;
                    System.debug('t.Value2 : '+t.Value2);
                    t.Value3 = tobj.Value3__c;
                    System.debug('t.Value3 : '+t.Value3);
                    oobj.testobject[b] = t;
                    }
                } 
Sanjay Bhati 95Sanjay Bhati 95
Hi Shreya,

Here is your code reply
 
List<Test_object__c> testList =[SELECT Id,Name,Product__c,Product__r.Name,Opportunity__c,Value1__c,Value2__c,Value3__c,OpportunityLineItemId_del__c FROM Test_object__c WHERE OpportunityLineItemId_del__c =:oid]; 
    List<Test_object__c> finaltestList= new List<Test_object__c>();
    List<TestObjectJson> testJosnList = new List<TestObjectJson>();
    if(testList.size()>0)
    {   
        for(Test_object__c tobj :  testList){
            TestObjectJson t = new TestObjectJson();
            t.Name = tobj.Name;        
            t.Product = tobj.Product__c;
            t.ProductName = tobj.Product__r.Name;
            t.Value1 = tobj.Value1__c;
            t.Value2 = tobj.Value2__c;
            t.Value3 = tobj.Value3__c;
            
            testJosnList.add(t);
            finaltestList.add(tobj);
        }
        system.debug(JSON.serialize(testJosnList));
    }

Please let me know your feedback