I have an object where exist an Account and user lookup fields. Now records of the object will have always user and Account combination as unique. It means U1 -A1 combination can not exist in multiple records of the object. I have to create a map of User Id and List of it's corresponding Account Ids. I tried below but it somehow not even prints in debug log. So I am unable to understand where is my mistake. Can someone please help here?
Apex Code
Map<Id,Set<Id>> MapOfTierObj = new Map<Id,Set<Id>>();
// Change in map assign
for(Tier_abv__c Tr: scope)
{
if(!MapOfTierObj.containsKey(Tr.Core_Panel_User_abv__c))
{
MapOfTierObj.put(Tr.Core_Panel_User_abv__c, new Set<ID>{Tr.Account_abv__c});
}else{
MapOfTierObj.get(Tr.Core_Panel_User_abv__c).add(Tr.Account_abv__c);
system.debug('Add Accounts to existing@'+MapOfTierObj.get(Tr.Core_Panel_User_abv__c)); }
}
system.debug('MapOfTierObj@'+MapOfTierObj);
// Change in Map assign : Completed
Here the scope comes from here:
global Database.QueryLocator start(Database.BatchableContext BC) {
Query = 'SELECT id,Account_abv__c,Account_abv__r.Commercial_Core_Panel_Target_abv__c,Core_Panel_User_abv__c '+
'from Tier_abv__c Where Core_Panel_User_abv__c != NULL AND Account_abv__r.Commercial_Core_Panel_Target_abv__c = True '+
'order by Core_Panel_User_abv__c asc';
Abbvie_Custom_Settings__c AbvSetngs = Abbvie_Custom_Settings__c.getInstance('Main');
if(AbvSetngs !=null)
{
ListName = AbvSetngs.Commercial_Core_Panel_Target_List_Name__c;
}
else
{
ListName = 'My Core Panel with Recent Comm Visit';
}
return Database.getQueryLocator(query);
}
Scope
coming from if its a list then add a debug and check whether it holds any value before iteration of for loop – RCS Jan 19 '17 at 7:37