2

I've created a helper class and method which takes Trigger.new as parameter. The soql at a place is returning zero records despite a record is present.

public class CaseDispatcher {
public static Map<id,id> getCaseMap (List<Case> caseList)
{
    List<Case> listcase = new List<Case>();
    listcase = caseList;
    Map<Id, list<case>> caseMap = new Map<Id, list<case>>();
    list<user> creatorList = new list<user>();

    system.debug(listcase.size());

    for(case c:listcase)
    {                  
      if(caseMap.containsKey(c.createdbyId))
      {
        caseMap.get(c.createdbyId).add(c);  
      }
       else
      {
        caseMap.put(c.createdById, new list<case>{c});

      } 

    }        
    system.debug(caseMap.size());
    system.debug(caseMap.values().size());

    creatorList = [SELECT id from user where id in :caseMap.keySet()];
    system.debug(creatorList.size());

caseMap.size() returns 1 but the SOQL is returning zero records. Thus creatorList is empty

8
  • 1
    can you remove unnecessary codes, specially variable declarations sections and also is this helper class method is called before or after trigger insert/update not specified Commented Sep 25, 2016 at 20:25
  • @SantanuBoral I haven't pasted the whole class, so it should be ok
    – Jarvis
    Commented Sep 25, 2016 at 20:26
  • caseMap.get(c.createdbyId).add(c); logic is not clear Commented Sep 25, 2016 at 20:29
  • The value of CreatedById is null in a before insert context.
    – Adrian Larson
    Commented Sep 25, 2016 at 20:40
  • 2
    The one key can be null...
    – Adrian Larson
    Commented Sep 25, 2016 at 21:05

1 Answer 1

5

The "audit fields" (CreatedDate, LastModifiedDate, CreatedById, and LastModifiedById) are all null during a BeforeInsert trigger. Use UserInfo.getUserId() to find out who's creating the record in a BeforeInsert trigger, or use AfterInsert instead (depends on what you're trying to do).

1
  • 3
    @Bennie to support this, and Adrian's comments, you can do a System.debug(caseMap) instead of System.debug(caseMap.size()). It will clearly show you what is the value in the map's keyset.
    – Lance Shi
    Commented Sep 25, 2016 at 23:21

You must log in to answer this question.

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