调用 Web 服务 API 时出现 Salesforce 错误
当我调用函数add()时出现以下错误 您有未提交的工作待处理。请在调用之前提交或回滚
我调用 getItems()
来填充下拉列表,然后调用 add
函数从下拉列表中插入所选项目
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您需要在完成任何标注之后而不是之前执行所有 DML。因此,任何 insert/update/upsert 或删除语句都必须遵循任何
http.send(req);
调用。** 看起来您的列表在调用 add() 方法后正在重新填充,因为您的列表驻留在 getter 方法中 **
这是特定于线程的,并且必须按任何给定线程的顺序出现。因此,例如,当用户单击带有操作方法的按钮时,该调用中的所有 DML 语句必须遵循同一线程中发生的任何标注。对于触发器或批处理 Apex 来说也是如此。
在某处以某种方式更新数据的 getter/setter 可能会导致这种情况。例如:
另外,永远不要在 getter 中添加标注。始终将标注放在执行一次且仅执行一次的显式方法中。 Getters 会被多次解雇,而且 Apex 中的标注有严格的限制。
This is because you need to do all your DML AFTER you are done with any callouts, not before. So any insert/update/upsert or delete statements must follow any
http.send(req);
calls.** Looks like your list is getting repopulated after you call the add() method, because your list resides in a getter method **
This is thread-specific and must occur in the sequence per any given thread. So, for example, when a user clicks a button with an action method, all DML statements in that call must follow any callouts that happen in the same thread. Same for a trigger or batch Apex.
Having a getter/setter somewhere that is updating data somehow can cause this. Eg:
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.