function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
grangran 

Getting null for the key of the Map

Got a piece of code wherein I'm trying to fetch the key value from the map. 

trigger SetContactOwnerAndASD on Contact (before insert) {
Map mapConIdAccId = new Map();
for(Contact c:trigger.new){
mapConIdAccId.put(c.Id,c.AccountId);
}
List consList = new List();
System.Debug('Size='+mapConIdAccId.keySet().size());
for(Id i:mapConIdAccId.keySet()){
  Contact co = new Contact();
  co.Id = i;
  System.Debug('co.Id='+i); // this one is showing me null, but I'm trying to display the key
  co.AccountId = mapConIdAccId.get(i);
  System.Debug('co.AccountId='+co.AccountId);
  consList.add(co);
}

//update consList ;
}

I want to get the value of the key in the map but I'm getting a null for the variable "i". The second one i.e. mapConIdAccId.get(i) works fine and this one brings the value. How do I display the key itself, not the value?

Thanks
V

Thanks
Satish_SFDCSatish_SFDC
This is a before insert trigger. Which means the record is not yet inserted, so an ID is not yet generated.
Hence the key contains null.
If its just displaying the data, use an after insert trigger.

Regards,
Satish Kumar