【问题标题】:have uncommitted work pending. Please commit or rollback before calling out有未提交的工作待处理。请在调用前提交或回滚
【发布时间】:2013-02-18 13:20:25
【问题描述】:

我有一个批处理类

 public class BatchCreateGCalendars implements Database.Batchable<SObject>, Database.Stateful, Database.AllowsCallouts {

        private String query;
        private String pageToken ;
        private String accessToken;
        private String CalendarId;

        public BatchCreateGCalendars(){

            this.query = 'Select Id, Name, CalendarId__c from CalendarSettings__c'; // Query to get the CalendardID (Google ID)

        }

        public Database.Querylocator start(Database.BatchableContext BC){

            return Database.getQueryLocator(query);

        }

        public void execute(Database.BatchableContext BC, List<sObject> scope){

            GoogleForce__c gApp           = GoogleForce__c.getAll().values()[0];        
            GCalendarUtil.Calendar cal    = new GCalendarUtil.Calendar();
            GCalendarUtil.GResponse gResp = new GCalendarUtil.GResponse();  


            String endPoint = GCalendarUtil.CALENDAR_BASE + GCalendarUtil.CAL_URL; // Calendar Endpoint

            // Iterate over the records, delete each calendar and create the calendar again.

            for(CalendarSettings__c c : (List<CalendarSettings__c>)scope){



                String delEndpoint = endpoint + '/' + c.CalendarId__c;

                if(gApp.ExpiresIn__c < system.now()){
                    gResp               = GCalendarUtil.getNewToken(gApp);
                    accessToken         = gResp.access_token;
                    gApp.AccessToken__c = gResp.access_token;
                    gApp.ExpiresIn__c   = System.now().addSeconds(gResp.expires_in);

                }else{
                    accessToken = gApp.AccessToken__c;
                    System.debug('Calendar Id  is '+ c.CalendarId__c);
                }
              String re=  GCalendarUtil.doApiCall(null,'GET','https://www.googleapis.com/calendar/v3/calendars/'+c.CalendarId__c+'/events',accessToken);

            re = re.replaceAll('"end":','"end1":');
    re = re.replaceAll('"dateTime":','"dateTime1":');  
              System.debug('response is' +re);
               JSON2Apex1 aa = JSON2Apex1.parse(re);

               List<JSON2Apex1.Items> ii = aa.items ;
                        System.debug('next token is'+ aa.nextPageToken);
               List<String> event_id =new List<String>();
               if(ii!= null){
               for(JSON2Apex1.Items i: ii){
               event_id.add(i.id);
               }
               }
           for(String s:event_id)
           {GCalendarUtil.doApiCall(null,'DELETE','https://www.googleapis.com/calendar/v3/calendars/'+c.CalendarId__c+'/events/'+s,accessToken);

           }
       pageToken = aa.nextPageToken;
       CalendarId=c.CalendarId__c;

               if(pageToken!=null)
               deleteEvents(accessToken , pageToken,c.CalendarId__c);

            }

            update gApp;
        }

        /*
            After the batch job is finished, trigger the other batch jobs where the Campaigns are sent as Events to the calendars. 
            Use the CalendarQuery field on the each calendarSettings record.
        */

        public void finish(Database.BatchableContext BC){   

        BatchDeleteEvents bjob1 = new BatchDeleteEvents(CalendarId,accessToken);
                Database.executeBatch(bjob1,9); 


            for(CalendarSettings__c c  : [Select Id, Name, CalendarQuery__c, CalendarId__c,FieldToDisplay__c from CalendarSettings__c WHERE Name IN ('Public Calendar', 'Internal marketing Calendar')]){

                BatchPublicCampaignsToGoogle bjob = new BatchPublicCampaignsToGoogle(c.CalendarQuery__c,c.CalendarId__c,c.FieldToDisplay__c);
                Database.executeBatch(bjob,9); // This is set to process 9 records, allowing 1 extra callout for refresh token in case needed.

            }


        }

        public static void deleteEvents(String accessToken,String pageToken,String CalendarId){
         List<Event__c> event_id =new List<Event__c>();

        while(pageToken != null)
    {   String re = GCalendarUtil.doApiCall(null,'GET','https://www.googleapis.com/calendar/v3/calendars/'+CalendarId+'/events?pageToken='+pageToken,accessToken);
                    System.debug('next page response is'+ re);
                     re = re.replaceAll('"end":','"end1":');
    re = re.replaceAll('"dateTime":','"dateTime1":');  
      JSON2Apex1 aa = JSON2Apex1.parse(re);
       List<JSON2Apex1.Items> ii = aa.items ;
      System.debug('size of ii'+ii.size());
      pageToken = aa.nextPageToken ;

               if(ii!= null){
               for(JSON2Apex1.Items i: ii){
               event_id.add(new Event__c(name = i.id));
               }
               }



     }   
     insert event_id;


        }

    }

在这段代码中,我在这一行遇到错误

String re=  GCalendarUtil.doApiCall(null,'GET','https://www.googleapis.com/calendar/v3/calendars/'+c.CalendarId__c+'/events',accessToken);

错误是

You have uncommitted work pending. Please commit or rollback before calling out

为什么我会遇到这个错误,请帮忙,还有一个疑问是当我在完成方法中调用此代码时

 BatchDeleteEvents bjob1 = new BatchDeleteEvents(CalendarId,accessToken);
                    Database.executeBatch(bjob1,9); 

它将同步执行此代码 na 意味着处理 9 个批次中的所有对象,然后控制将在此代码上进行

BatchPublicCampaignsToGoogle bjob = new BatchPublicCampaignsToGoogle(c.CalendarQuery__c,c.CalendarId__c,c.FieldToDisplay__c);
                    Database.executeBatch(bjob,9);

【问题讨论】:

  • 嗨 Ritesh!恐怕除了stackoverflow.com/questions/14545072/… 的评论之外,我没有其他答案。你必须重新排序它们。首先是标注,然后是 DML(插入、更新等)。或者 DML + @future 所以标注发生在不同的上下文中(然后你需要弄清楚如果标注失败如何处理插入的数据)。甚至Database.setSavepoint() 都不会帮助你。
  • 当我从执行中注释掉调用 deleteEvents 函数时,请仔细查看代码,然后没有错误,但调用 deleteEvents 函数会导致错误。但在该函数中,我首先调用标注然后插入它. 和执行“更新 gApp ;”中的最后一条语句不是 DML 语句 gApp 是自定义设置,当我忽略它时仍然出现相同的错误。deleteEvent 函数有什么问题我首先调用 api,然后按照规则插入仍然出现错误
  • @eyescream 如果我遵守规则,请回复我为什么会出错??
  • sorry got it will tr​​y to solve it 30分钟后抱歉打扰

标签: salesforce apex-code


【解决方案1】:

您不能在 Salesforce/Apex 中与 DML 语句一起制作标注。

您可以先根据可以调用 DML Statments 的 Callout 状态执行 CallOut。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 2022-07-01
    相关资源
    最近更新 更多