You need to sign in to do that
Don't have an account?
Shreya 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;
}
}
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;
}
}
Here is your code reply
Please let me know your feedback