This is because you need to do all your DML AFTER you finish with any callouts, not before. Therefore, any insert / update / upsert or delete statements should follow any http.send(req); calls http.send(req); .
** It looks like your list is re-populated after calling the add () method, because your list is in the getter method **
This depends on the thread and must be performed in sequence for each thread. So, for example, when a user clicks a button using an action method, all DML statements in this call must follow any callouts that occur in the same thread. The same goes for an Apex trigger or batch.
Having getter / setter somewhere updating the data might somehow cause this. For instance:
public String someProperty { get { return [SELECT Name FROM CustomObject__c WHERE Id = :this.someId]; } set(String s) { CustomObject__c c = [SELECT Name FROM CustomObject__C WHERE Id = :this.someId] c.Name = s; update c; } }
Also, never put a leader in a getter. Always put a leader in an explicit method that does this once and only once. Getters will fire several times, and callouts have severe restrictions in Apex.
source share