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
akaaka 

"list<CustomObject3__c>" and "list<CustomObject3__c>" please tell me how to transfer to

Please tell me

 

 

 public CustomObject3__c  getProducts() {  ...

 

The above records are returned in an OK

 

 public  list<CustomObject3__c>  getProducts() { ....


I expect to have multiple records are returned above, please tell me the hint error occurs

 

code------------------------------------------------------------------------------------

    CustomObject1__c  names;
    CustomObject3__c fee_new;
    public  list<CustomObject3__c>  getProducts() {
        integer i =1;
        List<CustomObject3__c> fee_dat = new CustomObject3__c[]{};
   //     list dis_fee = new CustomObject3__c();
        fee_new = new CustomObject3__c();
        for (list <CustomObject1__c >  names :
            [SELECT id,name,Field4__c
                , (SELECT link__c,Field6__c from link4U59__r )
                FROM CustomObject1__c
                WHERE Field4__c = null and Field6__c <= '2011'
                order by name,Field6__c]
            )
         {   List<CustomObject1__c> idname =  [select id  from CustomObject1__c where name =: names[i].name LIMIT 1 ];
                fee_new.link__c = idname[0].id;
                fee_new.Field6__c = null;
                fee_new.Field2__c =  null;
                fee_new.Field5__c = 0;
               fee_dat.[i]=fee_new;              /* error occurece */
              i++;
            }
        return fee_dat;
    }

I expect to have multiple records are returned above, please tell me the hint error occurs

errormessege-------------------------------------------------------------------------------------------------

 System.ListException: List index out of bounds: 1

 

bob_buzzardbob_buzzard

There's a couple of issues here:

 

In your code, you set the variable i to 1 and then use that to extract an element from the names list, which effectively looks for the 2nd element in the list.  If your list has one element, that will cause a problem.  Lists are index origin 0, so change your initialisation of i to 0.

 

Secondly, use the add notation to append an entry to the list, otherwise you will be trying to overwrite an element that doesn't exist.  Revised code below:

 

 

  CustomObject1__c  names;
    CustomObject3__c fee_new;
    public  list<CustomObject3__c>  getProducts() {
        integer i =0;
        List<CustomObject3__c> fee_dat = new List<CustomObject3__c>();
   //     list dis_fee = new CustomObject3__c();
        fee_new = new CustomObject3__c();
        for (list <CustomObject1__c >  names :
            [SELECT id,name,Field4__c
                , (SELECT link__c,Field6__c from link4U59__r )
                FROM CustomObject1__c
                WHERE Field4__c = null and Field6__c <= '2011'
                order by name,Field6__c]
            )
         {   List<CustomObject1__c> idname =  [select id  from CustomObject1__c where name =: names[i].name LIMIT 1 ];
                fee_new.link__c = idname[0].id;
                fee_new.Field6__c = null;
                fee_new.Field2__c =  null;
                fee_new.Field5__c = 0;
               fee_dat.add(fee_new);
              i++;
            }
        return fee_dat;
    }

 

 

 

akaaka

Worked as expected and changed some of the access method. Thank you bob_buzzard.