0

逻辑:

   public static void wmgRemoveOldGroupQueue(Map<Id,Case> newMap, Map<Id,Case> oldMap){
        String wmgRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('WMG_Operations_Workflow').getRecordTypeId();
        for (Case o : newMap.values()) {
            
            system.debug('o.OwnerId == '+o.OwnerId+' oldMap.get(o.Id).OwnerId =='+oldMap.get(o.Id).OwnerId);
            system.debug('o.RecordTypeId '+o.RecordTypeId+ 'wmgRecordTypeId'+wmgRecordTypeId);
         
            if (o.RecordTypeId == wmgRecordTypeId && !((String)o.OwnerId).startsWith('005') &&
                (o.OwnerId != oldMap.get(o.Id).OwnerId)) {
                o.ANN_Accepted_From_Queue__c = '';
                    
                    
            }
            system.debug('o.ANN_Accepted_From_Queue__c' +o.ANN_Accepted_From_Queue__c);
            
        }
    }

测试类:

 static testMethod void wmgRemoveOldQueueTest() {        
        //setup 
       	User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()]; 
        String wmgRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('WMG_Operations_Workflow').getRecordTypeId();
        String assertMsg = 'Accepted From Queue could not clear out';        
        //QueueSobject q = [SELECT QueueId, Queue.Name FROM QueueSobject WHERE SObjectType = 'Case' and Queue.Name = 'WMG Ops - Licensing' LIMIT 1];
  
        Case c = new Case();
        c.OwnerId = thisUser.Id;
        c.RecordTypeId = wmgRecordTypeId;
        c.ANN_Accepted_From_Queue__c = 'Test';
        insert c;
        System.debug('Owner id at insert is '+c.OwnerId);
 
       
        
       Group testGroup = new Group(Name='test group', Type='Queue');
       insert testGroup;
        
       System.runAs(new User(Id=UserInfo.getUserId()))
  {
    QueuesObject q = new QueueSObject(QueueID = testGroup.id, SObjectType = 'Case');
    insert q;
   } 
      
        //c = [SELECT OwnerId, ANN_Accepted_From_Queue__c, RecordTypeId FROM Case WHERE Id = :c.Id];
        //c.OwnerId = thisUser.Id;
		//update c;
		//System.assertEquals(Test, [SELECT ANN_Accepted_From_Queue__c FROM Case WHERE Id = :c.Id].ANN_Accepted_From_Queue__c, 'Failed');
		system.assertEquals(c.OwnerId, thisUser.Id, 'Failed2');
        

        //c.RecordTypeId = wmgRecordTypeId;
        //system.assert(((String)c.OwnerId).startsWith('00G'), 'Owner issue');
     
        //system.assert(((String)c.OwnerId).startsWith('00G'), 'Owner issue');

                
        case newCase = [SELECT id, Ownerid, ANN_Accepted_From_Queue__c FROM Case WHERE Id = :c.Id];
        newCase.OwnerId=testGroup.Id;
        //QueueSobject q1 = [SELECT QueueId, Queue.Name FROM QueueSobject WHERE SObjectType = 'Case' and QueueID=testGroup.Id LIMIT 1];
          Test.startTest();
         update newCase;
         Test.stopTest();
        
        system.debug('newCase.OwnerId1 =='+newCase.OwnerId);
        case r = [SELECT id, Ownerid, ANN_Accepted_From_Queue__c FROM Case WHERE Id = :newCase.Id];
        system.debug('r Owner Id =='+r.OwnerId);
        System.assertEquals('',[SELECT ANN_Accepted_From_Queue__c FROM Case Where Id=:r.Id].ANN_Accepted_From_Queue__c, assertMsg);
        
        
    }

在这一步失败: System.assertEquals('',[SELECT ANN_Accepted_From_Queue__c FROM Case Where Id=:r.Id].ANN_Accepted_From_Queue__c, assertMsg);

在这一步: system.debug('o.RecordTypeId '+o.RecordTypeId+ 'wmgRecordTypeId'+wmgRecordTypeId);

18:14:23:180 USER_DEBUG [720]|DEBUG|o.OwnerId == 00518000003xRhcAAE oldMap.get(o.Id).OwnerId ==00518000003xRhcAAE 无法找到问题所在:

4

1 回答 1

0

这行不通。

当您有一个内存记录(例如您的案例)并插入它时 - 只有生成的记录的 ID 会“神奇地”发送回您的代码。无论记录发生什么其他情况(触发代码、工作流、流程、流程构建器、公式字段...),您都不会立即在记录中看到它。

您必须查询才能看到新值(好吧,对于公式,有一个很好的技巧,但其他一切 - 您必须查询)。

制作类似的东西Case newCase = [SELECT OwnerId, ANN_Accepted_From_Queue__c FROM Case WHERE Id = :c.Id];,看看它是否具有您想要的值。

于 2019-08-31T21:12:36.320 回答