Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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(); 

    req.setMethod('GET');

    req.setEndpoint('http://submit.toolsberry.com/sfdc/technologies');

    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) {

        break;

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

        String tname = reader.getText();

        AllTech.add(tname);

    }

    reader.next();

 }

        }

     }

    reader.next();

 }

}

1 Answer

0 votes
by (32.1k points)
edited by

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 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.

Go for this in-depth job-oriented salesforce course online!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...