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(); 

    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 (31.8k points)

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.

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


Categories

...