Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Salesforce by (11.9k points)

I get the following error when I call the function add() You have uncommitted work pending. Please commit or rollback before calling out

I call the getItems() to populate the drop down and then the add function to insert the selected item from the drop down

public PageReference add() {

              insert technology;

              return null;


public List<SelectOption> getItems() {

    List<SelectOption> options = new List<SelectOption>();

    List<Technology__c> AddedT=[SELECT Name FROM Technology__c];

    HttpRequest req = new HttpRequest(); 



    Http http = new Http();

    HTTPResponse res = http.send(req);  

    String response=res.getBody();

    XmlStreamReader reader = new XmlStreamReader(response);

     List<String> AllTech = new List<String>();

     while(reader.hasNext()) {    

     if (reader.getEventType() == XmlTag.START_ELEMENT) {

        if ('string' == reader.getLocalName()) {

    while(reader.hasNext()) {

     if (reader.getEventType() == XmlTag.END_ELEMENT) {


     } else if (reader.getEventType() == XmlTag.CHARACTERS) {

        String tname = reader.getText();








1 Answer

0 votes
by (32.1k points)

public String someProperty




      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 callout in a getter. Always put a callout in an explicit method that does it once and only once. Getters will get fired multiple times and callouts have strict limitations in Apex.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94.2k users

Browse Categories