3

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);
    }
  • Could you please also post the start method? – Shailesh Patil Jan 19 '17 at 7:34
  • I'm sorry I didn't get the start method. Which one please tell me? – Testing_SFDC Jan 19 '17 at 7:36
  • Hi. If you add a basic system.debug in the beginning of the for loop, does it print something in the log? If not, the variable scope must be empty. – Martin Lezer Jan 19 '17 at 7:36
  • 1
    Where is the 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
  • Did you try to verify that your scope variable is not empty? – Martin Lezer Jan 19 '17 at 7:54
3

You are missing putting set value to map. I will recommend writing it in below form:

for(Tier_abv__c Tr: scope)
{
   Set<Id> idSet=new Set<Id> ();
   if(MapOfTierObj.containsKey(Tr.Core_Panel_User_abv__c))
      idSet=MapOfTierObj.get(Tr.Core_Panel_User_abv__c);
   idSet.add(Tr.Account_abv__c)
   MapOfTierObj.put(Tr.Core_Panel_User_abv__c,idSet);

}

Here is adiitional information to explain what was wrong with your code:

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

Above code snippet will definitely add a value to set but will not update your map with revised set. So you could have written this as:

MapOfTierObj.put(Tr.Core_Panel_User_abv__c,MapOfTierObj.get(Tr.Core_Panel_User_abv__c).add(Tr.Account_abv__c));
  • He doesn't need it. He's updating the set by adding one id in it. – Martin Lezer Jan 19 '17 at 7:54
  • @Mukesh. Thanks I am getting exact values in my debug now. But still I didn't get where I was wrong. But Thanks i was able to work with this. :) – Testing_SFDC Jan 19 '17 at 8:04
  • @SFDC_Learner : Improved answer to explain what was wrong. – Mukesh Verma Jan 19 '17 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.