0

I have this code:

String cantidad;
String total;

List<List<String>> pList = new List<List<String>>();
List<String> lstStr = new List<String>();
List<QuoteLineItem> acList = [SELECT Quantity, total__c FROM QuoteLineItem 
WHERE QuoteId = '0Q01D00'];

    for(QuoteLineItem ac : acList)
    {
        cantidad = string.valueOf(ac.Quantity); 
        total = string.valueOf(ac.total__c);
        lstStr.add(cantidad);
        lstStr.add(total);
    }
    pList.add(lstStr);
System.debug(pList);

the output is:

((2.00, 10.00, 1.00, 10.00, 1.00, 10.00, 2.00, 44.00))

Im trying to separate each 2 values. it should show this:

((2.00, 10.00), (1.00, 10.00), (1.00, 10.00), (2.00, 44.00))

So i can access them like pList[0], pList[1], pList[2], pList[3]

1

The problem is the instance of the list you are working with is constantly being built with new values. To solve your issue, you could use the following

List<List<String>> pList = new List<List<String>>();

for (QuoteLineItem ac : [SELECT Quantity, total__c FROM QuoteLineItem WHERE QuoteId = '0Q01D00']) {
    pList.add(new List<String>{String.valueOf(ac.Quantity), String.valueOf(ac.total__c)});
}
System.debug(pList[0]);
System.debug(pList[1]); 

Note, the for loop has become a soql for loop. You may also consider the use of a Map to related collection data but note, the get() method does not support indexing.

0

You can do like below to solve your issue. Array can be assigned to list.

String cantidad;
String total;

List<List<String>> pList = new List<List<String>>();
List<String> lstStr = null;
List<QuoteLineItem> acList = [SELECT Quantity, total__c FROM QuoteLineItem 
WHERE QuoteId = '0Q01D00'];

    for(QuoteLineItem ac : acList)
    {
        lstStr = new string[2];
        cantidad = string.valueOf(ac.Quantity); 
        total = string.valueOf(ac.total__c);
        lstStr[0]=cantidad;
        lstStr[1]=total;
        pList.add(lstStr);
    }
   System.debug(pList[0]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.