APEX:标注后进行更新-DML错误

问题描述 投票:0回答:1

当我进行标注时,我想将结果存储在一个对象中。但我收到以下错误消息:

当前不允许DML。发生了意外错误。您的开发组织已收到通知。

有人可以帮忙吗?

这是代码:

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;
            } } }
salesforce apex dml
1个回答
0
投票

DML在构造函数中是不允许的,在您的情况下,问题是Tower_clientID方法。

无耻的插件,我将交叉链接我6岁的答案以获取更多信息:https://salesforce.stackexchange.com/questions/28833/why-is-dml-not-allowed-in-constructor

将代码(标注和更新)移至另一种方法。或者将标注保留在构造函数中,您可以显示结果吗?但是更新将是明确的动作,人类单击按钮,了解他/她在做什么,接受“副作用”。

  • 如果从另一个顶点类调用它-首先调用构造函数,然后调用方法。
  • [如果您是从Visualforce呼叫的(我的意思是您那里有StandardController)-您可以按下一个按钮或使用<apex:page action="{!calloutHere}">(虽然不是很好,但是如果您真的希望它在页面加载并您了解风险...
  • 类似光环(使用init处理程序)和闪电Web组件(使用@wireconnectedCallback?]] >>
© www.soinside.com 2019 - 2024. All rights reserved.