• ddgg
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

anyone can help me with this batch, the problem is that I'm not finding the records of the entity EventRelation to update the records of the other entity.

 

Thanks,

 

global void execute(Database.BatchableContext BC, List<sObject> scope){
// Create a map for the chunk of records passed into method.
Map<ID, Informe_icex__c> iMap = new Map<ID, Informe_icex__c>((List<Informe_icex__c>)scope);
System.Debug('iMap Records : ' + iMap);

List<EventRelation> eRList = [SELECT Id,response,Status,RelationId,EventId,RespondedDate FROM EventRelation WHERE Id IN
:iMap.keySet()];
System.Debug('EventRelation Records 1 : ' + eRList);

List<Informe_icex__c> listToBeUpdated = new List<Informe_icex__c>();
for(Informe_icex__c i : iMap.values()){
System.Debug('EventRelation Records 2 : ' + eRList);
System.Debug('Informe_Icex Records: ' + i);
if(!eRList.isEmpty()){
i.Comentarios_Invitados__c = eRList[0].response;
i.Estado_Invitado__c = eRList[0].status;
listToBeUpdated.add(i);

}
}
if(listToBeUpdated.size()>0){
update listToBeUpdated;

}

  • July 16, 2013
  • Like
  • 0

global class PopulateCorporateFields implements Schedulable
{

    public List<Corporate_Account_List_Assignment__c> calist { set; get; }
 
    
    //given a AccountId and List<AccountTeamMember >, it returns the record having AccountId=AccoundId
    public List<AccountTeamMember> findAccountTeamMember(Id accountId, Map<Id,List<AccountTeamMember>> acc_tmListMap, String teamMemberRole){
        if(acc_tmListMap== null || acc_tmListMap.size() == 0 || accountId==null ){
            return null;
       }
       
       integer count = 0;
       List<AccountTeamMember> atmList = acc_tmListMap.get(accountId);
       List<AccountTeamMember> atmRetList = new List<AccountTeamMember>();

       if(atmList  != null){
        for(AccountTeamMember am: atmList){
            if(am.TeamMemberRole == teamMemberRole){
                atmRetList.add(am);
                count++;
            }
        }
        }
        if(count > 0){
            return atmRetList;
        }else{
        return null;
        }
    }
    
    
     //given a Account_Name__c from Corporate_Account_List_Assignment__c object, find the corresponding Account record
     public Account findAccount(Id account_name,List<Account> accList ){
        if(accList == null || accList.size() == 0 || account_name==null ){
            return null;
        }
        for(Account am: accList){
            if(am.Id == account_name){
                return am;
            }
        }
        return null;
    }
    
    
   global void execute(SchedulableContext ctx) 
    {
     calist = [SELECT Account_Name__c,Corporate_Partnership_Director_1__c,Corporate_Partnership_Director_2__c,Corporate_Partnership_Director_3__c,Corporate_Partnership_Director_4__c,Corporate_Partnership_Director_5__c,Marketing_Solutions_1__c, Marketing_Solutions_2__c, Marketing_Solutions_3__c, Media_Services_1__c, Media_Services_2__c, Media_Services_3__c,Digital_Manager_1__c,Digital_Manager_2__c,Digital_Manager_3__c,Digital_Manager_4__c,Digital_Manager_5__c, Id,Name,Ultimate_Parent_Name__c FROM Corporate_Account_List_Assignment__c];     
         //prepare a map of ids and account_name__c
         Map<Id,Id> ca_acc_map = new Map<Id,Id>();
        
        for(Corporate_Account_List_Assignment__c ca : calist){
            
            ca_acc_map.put(ca.Id,ca.Account_Name__c);
            
         }
         
         
          Map<Id,Account> ac2 = new Map<Id,Account>([SELECT Name,ParentId,Id FROM Account where Id in :ca_acc_map.values()]);
           List<AccountTeamMember> atmcpd2 =[ SELECT Id,TeamMemberRole,UserId, IsDeleted,AccountId FROM AccountTeamMember WHERE AccountId in :ac2.keyset()  AND Isdeleted = FALSE  ];
      
      Map<Id,List<AccountTeamMember>> acc_tmListMap = new Map<Id,List<AccountTeamMember>>();
           for(AccountTeamMember atm: atmcpd2){
               List<AccountTeamMember> atmlist;
                if(acc_tmListMap.get(atm.AccountId) == null ){
                    atmlist = new List<AccountTeamMember>();
                   
                }else{
                     atmlist = acc_tmListMap.get(atm.AccountId);
                    
                
                }
                
                 atmList.add(atm);
                    acc_tmListMap.put(atm.AccountId,atmList);
           
           }
          
     for (Corporate_Account_List_Assignment__c ca : calist)
     {
     ca.Corporate_Partnership_Director_1__c = null;
     ca.Corporate_Partnership_Director_2__c = null;
     ca.Corporate_Partnership_Director_3__c = null;
     ca.Corporate_Partnership_Director_4__c = null; 
     ca.Corporate_Partnership_Director_5__c = null;
     
     ca.Digital_Manager_1__c = null;
     ca.Digital_Manager_2__c = null;
     ca.Digital_Manager_3__c = null; 
     ca.Digital_Manager_4__c = null;
     ca.Digital_Manager_5__c = null;
     
     ca.Marketing_Solutions_1__c = null;
     ca.Marketing_Solutions_2__c = null;
     ca.Marketing_Solutions_3__c = null;
     
     ca.Media_Services_1__c = null;
     ca.Media_Services_2__c = null; 
     ca.Media_Services_3__c = null;
      System.debug('******ID********'+ca.Id);
     
      
      //find atmcpd
      Account accX = ac2.get(ca.Account_Name__c);
      List<AccountTeamMember> atmcpd = findAccountTeamMember(accX.Id,acc_tmListMap,'Corporate Partnership Director' );
     List<AccountTeamMember> atmMS = findAccountTeamMember(accX.Id,acc_tmListMap,'IMS');
      List<AccountTeamMember> atmMDS = findAccountTeamMember(accX.Id,acc_tmListMap,'Media Services');
       List<AccountTeamMember> atmDM = findAccountTeamMember(accX.Id,acc_tmListMap,'Digital Manager');
         
      
        if(atmcpd!= null && atmcpd.size() > 0 )
        {       
         if(atmcpd.size() > 0)
         ca.Corporate_Partnership_Director_1__c = atmcpd.get(0).UserId;
         
          if(atmcpd.size()>1)
           ca.Corporate_Partnership_Director_2__c = atmcpd.get(1).UserId;
         
           if(atmcpd.size()>2)
           ca.Corporate_Partnership_Director_3__c = atmcpd.get(2).UserId;         
           
           if(atmcpd.size()>3)
           ca.Corporate_Partnership_Director_4__c = atmcpd.get(3).UserId;
           
          if(atmcpd.size()>4)
           ca.Corporate_Partnership_Director_5__c = atmcpd.get(4).UserId;
        }
        
        if(atmms != null && atmms.size() > 0)
        {
          if(atmms.size() > 0)
          ca.Marketing_Solutions_1__c  = atmms.get(0).UserId;
          
          if(atmms.size() > 1)
          ca.Marketing_Solutions_2__c = atmms.get(1).UserId;
          
          if(atmms.size() > 2)
          ca.Marketing_Solutions_3__c = atmms.get(2).UserId;

        }
      
      
        if(atmmds != null && atmmds.size() > 0)
        {
          if(atmmds.size() > 0)
          ca.Media_Services_1__c = atmmds.get(0).UserId;
          
          if(atmmds.size() > 1)
          ca.Media_Services_2__c = atmmds.get(1).UserId;
          
          if(atmmds.size() > 2) 
          ca.Media_Services_3__c = atmmds.get(2).UserId;
          
        }
        if(atmdm != null && atmdm.size() > 0)
        {
         if(atmdm.size() > 0)
         ca.Digital_Manager_1__c = atmdm.get(0).UserId;
         
         if(atmdm.size() > 1)
          ca.Digital_Manager_2__c = atmdm.get(1).UserId;
          
         if(atmdm.size() > 2)
          ca.Digital_Manager_3__c  = atmdm.get(2).UserId;
          
         if(atmdm.size() > 3)
          ca.Digital_Manager_4__c  = atmdm.get(3).UserId;
          
          if(atmdm.size() > 4)
          ca.Digital_Manager_5__c  = atmdm.get(4).UserId;
        }
      }               
      update calist;
    }

}