1

I have written a test class to cover logic written on before update trigger. The test class is failing at the assert step and upon checking i found that OwnerId in both trigger.new and trigger.oldmap are same even when i am updating it. Below is the code, request you yo kindly help me find the issue.

Method of handler class called from trigger:

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()) {
            if (o.RecordTypeId == wmgRecordTypeId && !((String)o.OwnerId).startsWith('005') 
                && (o.OwnerId != oldMap.get(o.Id).OwnerId)) {
                newMap.get(o.Id).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 did not clear out';
    Group q = new Group();
    q.Name = 'WMG Test';
    q.Type = 'Queue';
    insert q;

    QueueSObject qSobj = new QueueSObject();
    qSobj.QueueId = q.Id;
    qSobj.SobjectType = 'Case';
    System.runAs(thisUser){
        insert qSobj;
    }

    Case c = new Case();
    c.OwnerId = thisUser.Id;
    c.RecordTypeId = wmgRecordTypeId;
    c.ANN_Accepted_From_Queue__c = 'WMG Test';
    insert c; 
    c = [SELECT Id, OwnerId FROM Case WHERE Id =: c.Id];
    Test.startTest();
    c.OwnerId = q.Id;
    update c;
    Test.stopTest();

    c = [SELECT Id, OwnerId, ANN_Accepted_From_Queue__c FROM Case WHERE Id =: c.Id];
    System.assertEquals(null, c.ANN_Accepted_From_Queue__c, assertMsg);
} // -- failing at this step
4
  • Nice first question! After staring at your code for a bit, I don't see anything obviously wrong. I think your issue might lie elsewhere. Seeing how wmgRemoveOldGroupQueue() is called by your trigger might help. How are you determining that the ownerId in trigger.new and trigger.oldMap are the same?
    – Derek F
    Commented Aug 21, 2019 at 17:23
  • 4
    Pro tip: Instead of (String)o.OwnerId).startsWith('005') ; use o.OwnerId.getSobjectType() == User.SobjectType
    – cropredy
    Commented Aug 21, 2019 at 17:35
  • 2
    newMap.get(o.Id).ANN_Accepted_From_Queue__c = ''; but you assert System.assertEquals(null, c.ANN_Accepted_From_Queue__c, assertMsg); empty string is not same as null
    – cropredy
    Commented Aug 21, 2019 at 17:37
  • @cropredy, +1 I think what you said is the actual answer Commented Aug 21, 2019 at 17:53

1 Answer 1

2

I note in your code that you set:

newMap.get(o.Id).ANN_Accepted_From_Queue__c = '';  // empty string

But in your testmethod you:

c = [SELECT Id, OwnerId, ANN_Accepted_From_Queue__c FROM Case WHERE Id =: c.Id];
System.assertEquals(null, c.ANN_Accepted_From_Queue__c, assertMsg);  // null

Empty string is not the same as null so the assert fails

I recommend that your PROD code do this instead:

newMap.get(o.Id).ANN_Accepted_From_Queue__c = null;  // don't use empty string

N.B.
this is not the clearest to someone not au fait with key prefixes:

(String)o.OwnerId).startsWith('005')

instead, use

o.OwnerId.getSobjectType() == User.SobjectType
5
  • Hi, Actually, c.ANN_Accepted_From_Queue__c has the queue name and not empty string or null value. Also, i tried matching the SobjectType of owner with user object but no luck.
    – Shreya
    Commented Aug 22, 2019 at 18:47
  • I had used system.debug in the handler class method with which i could see that o.OwnerId and oldmap had the same value i.e. newmap also has previous value only and not updated value for ownerId.
    – Shreya
    Commented Aug 22, 2019 at 18:48
  • c.OwnerId = q.Id; update c; - in the before update trigger the value of c.ownerid is not q.Id ? Do you have other triggers on Case that might be firing in an unexpected order?
    – cropredy
    Commented Aug 23, 2019 at 0:22
  • Yes, its not q.Id but rather thisuser.id
    – Shreya
    Commented Aug 24, 2019 at 9:56
  • There are other triggers running amd i also tried inactivating all the before update and after insert triggers but even then same issue
    – Shreya
    Commented Aug 24, 2019 at 9:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .