APEX:在标注后进行更新 - DML 错误
APEX: Making an update following a callout - DML error
当我进行标注时,我想将结果存储在一个对象中。但是我收到以下错误消息:
DML currently not allowed An unexpected error has occurred. Your
development organization has been notified.
有人能帮忙吗?
这是代码:
public with sharing class Tower_clientID
{
public String city{get;set;}
public String temp{get;set;}
public String Surname1{get;set;}
public Tower_clientID(ApexPages.StandardController stdController)
{
Account account=(Account)stdController.getRecord();
account=[select Id,ShippingCity from Account where Id=:account.id];
String AccountCity=account.ShippingCity;
String reqBody='{"DocumentType":"ID","DocumentId":"'+AccountCity+'"}';
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://tow.turnkey.com/TOW30API/api/onboarding/get_clientByDocumentId');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setHeader('tow-domain', 'TR');
request.setHeader('tow-language','en-GB');
request.setHeader('tow-usercode', 'C32B3C53D238F6DCE44E34B');
request.setHeader('tow-apikey', '1122334477889900');
request.setBody(ReqBody);
if(response.getStatusCode()==200)
{
Map<String,Object> results=(Map<String,Object>)JSON.deserializeUntyped(response.getBody());
city=String.valueOf(results.get('Id'));
temp=String.valueOf(results.get('Name'));
Surname1=String.valueOf(results.get('Surname'));
account =[select Id,ShippingCity from Account where Id=:account.id];
account.ShippingCity = 'New York';
update account;
} } }
构造函数中不允许使用 DML,在您的情况下,问题出在 Tower_clientID
方法上。
无耻的插件,我会交叉link我6岁的答案以获取更多信息:https://salesforce.stackexchange.com/questions/28833/why-is-dml-not-allowed-in-constructor
将代码(标注和更新)移到另一个方法中。还是把 callout 保留在 constructor 中,你可以显示结果?但是更新将是明确的动作,人类点击一个按钮,了解 he/she 在做什么,接受 "side effects".
- 如果您从另一个顶点调用它 class - 首先调用构造函数,然后调用方法。
- 如果您从 Visualforce 调用(我的意思是您在其中有
StandardController
)- 您可以制作一个按钮或使用 <apex:page action="{!calloutHere}">
(虽然不是很好,但如果您真的想要它一旦页面加载并且您了解风险就会发生...
- 类似于 aura(使用
init
处理程序)和闪电网络组件(使用 @wire
?connectedCallback
?)
当我进行标注时,我想将结果存储在一个对象中。但是我收到以下错误消息:
DML currently not allowed An unexpected error has occurred. Your development organization has been notified.
有人能帮忙吗?
这是代码:
public with sharing class Tower_clientID
{
public String city{get;set;}
public String temp{get;set;}
public String Surname1{get;set;}
public Tower_clientID(ApexPages.StandardController stdController)
{
Account account=(Account)stdController.getRecord();
account=[select Id,ShippingCity from Account where Id=:account.id];
String AccountCity=account.ShippingCity;
String reqBody='{"DocumentType":"ID","DocumentId":"'+AccountCity+'"}';
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://tow.turnkey.com/TOW30API/api/onboarding/get_clientByDocumentId');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setHeader('tow-domain', 'TR');
request.setHeader('tow-language','en-GB');
request.setHeader('tow-usercode', 'C32B3C53D238F6DCE44E34B');
request.setHeader('tow-apikey', '1122334477889900');
request.setBody(ReqBody);
if(response.getStatusCode()==200)
{
Map<String,Object> results=(Map<String,Object>)JSON.deserializeUntyped(response.getBody());
city=String.valueOf(results.get('Id'));
temp=String.valueOf(results.get('Name'));
Surname1=String.valueOf(results.get('Surname'));
account =[select Id,ShippingCity from Account where Id=:account.id];
account.ShippingCity = 'New York';
update account;
} } }
构造函数中不允许使用 DML,在您的情况下,问题出在 Tower_clientID
方法上。
无耻的插件,我会交叉link我6岁的答案以获取更多信息:https://salesforce.stackexchange.com/questions/28833/why-is-dml-not-allowed-in-constructor
将代码(标注和更新)移到另一个方法中。还是把 callout 保留在 constructor 中,你可以显示结果?但是更新将是明确的动作,人类点击一个按钮,了解 he/she 在做什么,接受 "side effects".
- 如果您从另一个顶点调用它 class - 首先调用构造函数,然后调用方法。
- 如果您从 Visualforce 调用(我的意思是您在其中有
StandardController
)- 您可以制作一个按钮或使用<apex:page action="{!calloutHere}">
(虽然不是很好,但如果您真的想要它一旦页面加载并且您了解风险就会发生... - 类似于 aura(使用
init
处理程序)和闪电网络组件(使用@wire
?connectedCallback
?)