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.
Best Answer
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 exampleI'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.