0

this is my first post, I don't speak english...ok.

I will consume an API and I have this method:

public without sharing class BP_ExpiredProductsCtr {

public static id accID;

public static void manageProducts(List<BP_ExpiredProductsParser> exprod){ 
                 System.debug(' ---- iniciando ---- ' + exprod);
          Account iAccount=[SELECT Id,FinServ__SourceSystemId__c FROM Account WHERE Id=:accID LIMIT 1];

         List<BP_ExpiredProducts__c> lstobjproduct=new List<BP_ExpiredProducts__c>();

             for (BP_ExpiredProductsParser objParser : exprod){

             BP_ExpiredProducts__c objProd = new BP_ExpiredProducts__c();

             for (BP_ExpiredProductsParser.Product objexpproduct:objParser.product ) {

             objProd.BP_Type__c=objexpproduct.productDescription;
             objProd.BP_Document__c= objexpproduct.productNumber;
             }
             objProd.BP_ExpiredDays__c = objParser.expirationDays;
             objProd.BP_NationalCurrencyAmount__c = objParser.tradeAmount;
             objProd.BP_OriginCurrencyAmount__c = objParser.amount;
             objProd.CurrencyIsoCode = objParser.currencyCode;
             objProd.BP_RelatedAccount__c=iAccount.Id;

             lstobjproduct.add(objProd);

         insert lstobjproduct;

        System.debug('AQUI ESTOY: '+lstobjproduct);
         }
         }
     }

And need try it in debug anonnymous.

The Json it comes like this:

 [
      {
          "product": {
             "productNumber": "0000000001",
              "productDescription": "PRODUCTDESCRIPTION"
          },
          "expirationDays": 400,
          "amount": 1000000.00,
          "tradeAmount": 1704500.00,
          "currencyCode": "USD"
       },
        {
          "product": {
             "productNumber": "0000000002",
              "productDescription": "PRODUCTDESCRIPTION2"
          },
          "expirationDays": 800,
          "amount": 2000000.00,
          "tradeAmount": 1805600.00,
          "currencyCode": "PEN"  },
... 
]

and the Parser class:

public class BP_ExpiredProductsParser {

    public List<Product> product;
    public Decimal expirationDays;
    public Decimal amount;
    public Decimal tradeAmount;
    public String currencyCode;

    public class Product{
        public String productNumber;
        public String productDescription;
    }
}

I try it with this:

BP_ExpiredProductsParser objproductparser=new BP_ExpiredProductsParser();

        objproductparser.expirationDays=11;
        objproductparser.amount=12;
        objproductparser.tradeAmount=12.3;
        objproductparser.currencyCode='USD';
        //lstobjproductparser.product = ;
        System.debug(objproductparser);

List<BP_ExpiredProductsParser.product> lstproduct1=new List<BP_ExpiredProductsParser.product>();

            lstproduct1[0].productNumber();
            lstproduct1[0].productDescription();

            lstproduct1[1].productNumber();
            lstproduct1[1].productDescription();


           objproductparser.product.add(lstproduct1);
            System.debug(lstproduct1);

But,appears the next message:

Line 12, Column: 28 Method does not exist or incorrect signature: void productNumber() from the type BP_ExpiredProductsParser.Product

the line 12 is: lstproduct1[0].productNumber();

Thank You

0

This productNumber() is the syntax for invoking a method but productNumber is a class property so instead reference it like this:

lstproduct1[0].productNumber

though it is unclear what you are trying to do with that value. You could log it like this:

System.debug(lstproduct1[0].productNumber);
2
  • Hi, the correct code would be like this: lstproduct[0].productNumber='Example1'; I tried your code, but the debug returned the message: System.ListException: List index out of bounds: 0 Dec 12 '19 at 15:01
  • I tried your code, but the debug returned the message: System.ListException: List index out of bounds: 0 The line of error point to: System.debug(lstproduct1[0].productNumber); Dec 12 '19 at 15:08

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.