0

Logic:

   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);
            
        }
    }

Test Class :

 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);
        
        
    }

Failing at this step: System.assertEquals('',[SELECT ANN_Accepted_From_Queue__c FROM Case Where Id=:r.Id].ANN_Accepted_From_Queue__c, assertMsg);

and at this step : 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 not able to find the whats going wrong:

  • no i am struggling to pass this Unit test!! – Ashmita ranjan Aug 30 '19 at 14:50
  • I'm assuming this is called from a trigger. Can you verify the method is actually being called from the trigger AND it is being called within the context defined on the trigger `on` definition? I.E Before Update, After Update, etc. – TemporaryFix Aug 30 '19 at 15:51
  • Yes this is called from trigger before update : My Unit test is not able to get into the condition : o.OwnerId != oldMap.get(o.Id).OwnerId.....Please help me out as i need to complete my unit test as soon as possible – Ashmita ranjan Aug 31 '19 at 02:38
  • The main issue i am facing is not able to catch the difference between update of Owner id from oldMap and NewMap – Ashmita ranjan Aug 31 '19 at 04:55
  • Still no solution!! – Ashmita ranjan Aug 31 '19 at 08:01

1 Answers1

0

This won't work.

When you have an in-memory record (like your Case) and you insert it - only the generated record's ID is "magically" sent back to your code. Whatever else is happening to the record (changes resulting from trigger code, workflows, flows, process builders, formula fields...) you will not see it immediately in your record.

You have to query to see fresh values (well, for formulas there's a nice trick but everything else - you must query).

Make something like Case newCase = [SELECT OwnerId, ANN_Accepted_From_Queue__c FROM Case WHERE Id = :c.Id]; and see if it has the values you want.

eyescream
  • 18,088
  • 2
  • 34
  • 46
  • Hi : So the approach i should follow to query the record after inserting and then it should go for update ... right?? – Ashmita ranjan Sep 01 '19 at 07:25
  • When your trigger code fires? Looks like it's a "before update"? Insert the case, change owner, update, query the fresh case values & run assert on that new case, should be OK then. – eyescream Sep 01 '19 at 07:40
  • Thanks i will try it :) but just have issue ....When i checked the code coverage , My Test Class is not going inside the condition : (o.OwnerId != oldMap.get(o.Id).OwnerId) , hence i guess not setting the blank values in the particular field : ANN_Accepted_From_Queue__c = ' '; – Ashmita ranjan Sep 01 '19 at 09:22
  • is there anything wrong i am doing in the Test Class..?? – Ashmita ranjan Sep 01 '19 at 13:11
  • Not Working by this way:( – Ashmita ranjan Sep 02 '19 at 12:41
  • Update your question with new code of the trigger and the unit test. What does your test's debug log say if you put `System.debug(o);` immediately inside the `for (Case o : newMap.values()) {`? And it's "just" the test that you struggle with? When clicked manually functionality works OK? – eyescream Sep 02 '19 at 14:02
  • Yes i am only struggling with the Test Class only: – Ashmita ranjan Sep 03 '19 at 07:19
  • o.OwnerId != oldMap.get(o.Id).OwnerId) in debug log both the Owner id is same:( – Ashmita ranjan Sep 03 '19 at 10:46
  • (o.OwnerId != oldMap.get(o.Id).OwnerId) i am not able to differentiate between the two hence my code is not going inside – Ashmita ranjan Sep 03 '19 at 10:54
  • 18:14:23:180 USER_DEBUG [720]|DEBUG|o.OwnerId == 00518000003xRhcAAE oldMap.get(o.Id).OwnerId ==00518000003xRhcAAE as per your suggestion i mplemeted the debug statement but still you can see there is no change in the owner id of trigger.newMap and Trigger.oldMap. – Ashmita ranjan Sep 03 '19 at 12:47
  • You're missing 2nd update I think. Take a step back and think. You inserted the case with owner = user. Then you updated the case with owner = queue. But your trigger works on owner change to user (because it looks for changes and new value must be user). Either make 1 more update that transfers it back to user and then assert. Or insert the case with owner = queue (and then update that transfers to user). I don't care if you transfer queue -> user or user -> another user. But this is "correct". Your test doesn't meet your business logic. – eyescream Sep 03 '19 at 14:15
  • Even i tried that but still same...can u please guide me if i need to write the test class assert statement ; or what will the functionality of the test class – Ashmita ranjan Sep 03 '19 at 15:06