I am having trouble updating a variable because of various errors. I cannot get the value from my list to work with the value of my other list.

trigger StudentNoUpdate on Class_Size__c (before insert, before update) 
    List<ID> OppID = new List<ID>();         //create (empty) list of IDs (data type)
    for (Class_Size__c CS: trigger.new){     //For every CS being triggered (updated/inserted) do {}
            OppID.add(CS.Opportunity__c);    //to OppID list.add(in CS object.Opportunity field)        

    List<Opportunity> OppL = new List<Opportunity>  //create list from Opportunity with parameters below
        ([select id, Number_of_students__c          //List has two colums, ID and Number of students
        from Opportunity                            //List is created from all Opportunities
        where id in: OppID]);                       //Pulls only Opportunities with ID from previous list

    for(Opportunity o : OppL){                      //for every opportunity in OppL list {}
        List<ID> oID = new List<ID>();                  //Create a new ID List
        oID.add(o.ID);                                  //Add opportunity ID to oID list.

        List<Class_Size__c> CS = new List<Class_Size__c>([  //create a new list Class Size
            SELECT Number_of_students_CO__c                 //select number of students
            FROM Class_Size__c                              //from class size
            WHERE Opportunity__c in : oID                   //select only  when id in OppL list
            ORDER BY CreatedDate                            //order by CreatedDate
            DESC LIMIT 1                                    //descending, and pick only one

        OppL[o].Number_of_students__c = CS.get(Number_of_students_CO__c);
    update OppL; 

Error on line 25 (second to last one), List index must be of type Integer: SOBJECT:Opportunity

The goal is to update the Number_Of_Students field in Opportunity with the Number_of_students_CO__c field from the most recent Class_Size__c custom object.

  • 1
    OppL[o] should be just o - the foreach style of loop avoids the need for an integer intex. But you should also move the Opportunity__c query out of the loop - Google about blukifying truggers e.g. Apex Code Best Practices. – Keith C Jun 26 '14 at 21:35
  • Thank you Keith. I was trying to make that query only run on the list of things being updated, rather than the database. How might I do that? – denvergreene Jun 26 '14 at 21:43
  • To comment I would need to know what your overall aim is; your current logic appears to be accessing the last created Class_Size__c which may well not be in the list of things being updated. You have a lot of comments, but the most important one at the start of the trigger saying "what" the trigger does overall is missing. (Your comments are all "how" comments.) – Keith C Jun 27 '14 at 8:46

To get a value from a list, give it an index to pull it out, via the [] accessors. Then you access the object directly. For example

Integer numberOfStudents = CS[0].Number_of_students_CO__c;

I'd highly suggest doing some in-depth reading of the apex documentation in particularly the writing apex section. Accessing lists is a pretty fundamental and basic concept of any programming language and you're going to hate life if you don't put in the time to learn the basics. If you're brand new to programming try doing an intro tutorial to javascript or java to get the core concepts then come back to apex.

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.