• Ramprasad Varri
  • NEWBIE
  • 0 Points
  • Member since 2015

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

I want to capture the Territory Ids the user is assigned to. I wrote the trigger and the logs show the value that i am expecting, but the record is not getting updated. Could you suggest me please?

trigger preventTerritoryAssignmentDeletion on User (before update) {
    
    Set<Id> userIds = new Set<Id>();
    List<User> UT2List = new List<User>();
   
    for(User u : Trigger.New){
        if(u.Resource_ID__c == 'testresourceid'){
            //u.Territory_Backup_IDs__c = 'workinig';
            userIds.add(u.Id);
        }
    }
    
        UT2List = [SELECT ID,Territory_Backup_IDs__c, (SELECT ID,Territory2Id FROM UserTerritory2Associations) FROM User WHERE Id IN :userIds];
		
        for(User u : UT2List){
            string terrString =',';
            for(UserTerritory2Association ut2a : u.UserTerritory2Associations){
                terrString = terrString + ut2a.Territory2Id +',';
                system.debug('terrString inside inner for loop'+terrString);
               
            }
           
            system.debug('terrString in out for loop'+terrString);
            system.debug('u.Territory_Backup_IDs__c'+ u.Territory_Backup_IDs__c);
            u.Territory_Backup_IDs__c =u.Territory_Backup_IDs__c+ terrString;
            system.debug('u.Territory_Backup_IDs__c'+ u.Territory_Backup_IDs__c);
            }
          
    
}
Thank you in advance!
I have an apex batch which i tested in sandbox and it is working as expected but in production it is executing without error but not commiting the records.

I executed the same batch code in anonymous block in production that worked fine and did the commit.

Batch Code Below
global class AppointmentAvailabilityUpdateBatch  implements 
Database.Batchable<sObject>, Database.Stateful {
    
    
    global Integer recordsProcessed = 0;
    global Database.QueryLocator start(Database.BatchableContext bc) {
        Id recordTypeId =Schema.SObjectType.Appointment__c.getRecordTypeInfosByName()
            .get('Available').getRecordTypeId();
        
        DateTime dt = Date.Today().addDays(2);
        
        return Database.getQueryLocator(
            'Select id,Medical_Expert__c,Clinic__c from Appointment__c '+
            'where recordTypeid =: recordTypeId and Appointment_Date_Time__c >=: dt'
        );
    }
    
    global void execute(Database.BatchableContext bc, List<Appointment__c> scope){
        
        Set<id> clinicIdset = new Set<id>();
        Set<id> expertId = new Set<id>();
        Set<id> expertIdAvlApt = new Set<id>();
        
        
        Map<id,List<id>> clinicsWRTExpert = new Map<id,List<id>>();
        
        List<Expert_and_Clinic_Link__c>  ecUpdatenewList = new List<Expert_and_Clinic_Link__c>();
        
        List<Expert_and_Clinic_Link__c>  ecUpdateList = new List<Expert_and_Clinic_Link__c>();
                
          for(Appointment__c apt : scope){
            
            List<id> clinicidsList = new List<id>();
            
            if(clinicsWRTExpert.containsKey(apt.Medical_Expert__c)){
                clinicidsList = clinicsWRTExpert.get(apt.Medical_Expert__c);
                
                if(!clinicidsList.contains(apt.Clinic__c)){
                    clinicidsList.add(apt.Clinic__c); 
                }
               
                clinicsWRTExpert.put(apt.Medical_Expert__c,clinicidsList); 
            }else{
                clinicidsList.add(apt.Clinic__c);     
                clinicsWRTExpert.put(apt.Medical_Expert__c,clinicidsList);   
                
            }
            
            
        }
        
        List<Expert_and_Clinic_Link__c>  ecList = [Select id,Avalability_Flag__c,Clinic__c,Contact__c
                                                  from Expert_and_Clinic_Link__c];
        
        
        if(scope != null && scope.size() != 0){
            
            
            for(Expert_and_Clinic_Link__c ec : ecList){
                
                if(clinicsWRTExpert.containsKey(ec.Contact__c)){
                    
                    List<id> clinicIds = clinicsWRTExpert.get(ec.Contact__c);
                    
                    for(Id cId : clinicIds){
                        
                        if(ec.Clinic__c == cid){
                            
                            ec.Avalability_Flag__c = true;
                            
                            if(!ecUpdateList.contains(ec)){
                                ecUpdateList.add(ec);
                            }
                            
                            recordsProcessed++;
                        }
                        
                       
                    }
                    
                }
                
            }
            
          
            
        }else{
            

            for(Expert_and_Clinic_Link__c ec : ecList){
                
                ec.Avalability_Flag__c = false; 
                ecUpdateList.add(ec);
                recordsProcessed++;
                
            }
            
        }
        
        
        for(Expert_and_Clinic_Link__c ec : ecList){
            
            if(!ecUpdateList.contains(ec)){
                
                ec.Avalability_Flag__c = false; 
                ecUpdateList.add(ec);
                
            }
        }
        
        
        
        if(ecUpdateList.size() > 0){
            
           Database.SaveResult[] results = Database.update(ecUpdateList, false);
           system.debug('results::'+ results); 
        }
                

        
        
    }
    
    
        

global void finish(Database.BatchableContext bc){
    System.debug(recordsProcessed + ' records processed. Shazam!');
    AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
                        JobItemsProcessed,
                        TotalJobItems, CreatedBy.Email
                        FROM AsyncApexJob
                        WHERE Id = :bc.getJobId()];
    
    system.debug('Total Record Updated::' + job );
    
}    
}

Code which i run in anonymous block.
 
Integer recordCount = 0;

Id recordTypeId =Schema.SObjectType.Appointment__c.getRecordTypeInfosByName()
            .get('Available').getRecordTypeId();
        
DateTime dt = Date.Today().addDays(2);


List<Appointment__c> scope = [Select id,Medical_Expert__c,Clinic__c from Appointment__c 
            where recordTypeid =: recordTypeId and Appointment_Date_Time__c >=: dt];


 	Set<id> clinicIdset = new Set<id>();
        Set<id> expertId = new Set<id>();
        Set<id> expertIdAvlApt = new Set<id>();
        
        
        Map<id,List<id>> clinicsWRTExpert = new Map<id,List<id>>();
        
        List<Expert_and_Clinic_Link__c>  ecUpdateList = new List<Expert_and_Clinic_Link__c>();
                
          for(Appointment__c apt : scope){
            
            List<id> clinicidsList = new List<id>();
            
            if(clinicsWRTExpert.containsKey(apt.Medical_Expert__c)){
                clinicidsList = clinicsWRTExpert.get(apt.Medical_Expert__c);
                
                
                if(!clinicidsList.contains(apt.Clinic__c)){
                    clinicidsList.add(apt.Clinic__c); 
                }
               
                clinicsWRTExpert.put(apt.Medical_Expert__c,clinicidsList); 
            }else{
                clinicidsList.add(apt.Clinic__c);     
                clinicsWRTExpert.put(apt.Medical_Expert__c,clinicidsList);   
                
            }
            
            
        }
        
        List<Expert_and_Clinic_Link__c>  ecList = [Select id,Avalability_Flag__c,Clinic__c,Contact__c
                                                  from Expert_and_Clinic_Link__c];
        
        
        if(scope != null && scope.size() != 0){
            
            
            for(Expert_and_Clinic_Link__c ec : ecList){
                
                if(clinicsWRTExpert.containsKey(ec.Contact__c)){

                    List<id> clinicIds = clinicsWRTExpert.get(ec.Contact__c);
                    
                    for(Id cId : clinicIds){
                        if(ec.Clinic__c == cid){
                            
                            ec.Avalability_Flag__c = true;
                            
                            if(!ecUpdateList.contains(ec)){
                                ecUpdateList.add(ec);
                            }
                            
                            recordCount++;
                        }
                        
                       
                    }
                    
                }
                
            }
            
            
        }else{
            

            for(Expert_and_Clinic_Link__c ec : ecList){
                
                ec.Avalability_Flag__c = false; 
                ecUpdateList.add(ec);
                recordCount++;
                
            }
            
        }
        
        
        for(Expert_and_Clinic_Link__c ec : ecList){
            
            if(!ecUpdateList.contains(ec)){
                
                ec.Avalability_Flag__c = false; 
                ecUpdateList.add(ec);
                
            }
        }
        
        
        
        if(ecUpdateList.size() > 0){
           system.debug('recordCount:'+recordCount); 
           update ecUpdateList;
        }

These line in the batch class are executing. i debugged it but seems like it rolls back after that. 
if(ecUpdateList.size() > 0){
            
           Database.SaveResult[] results = Database.update(ecUpdateList, false);
           system.debug('results::'+ results); 
        }

Same batch is working fine in sandbox without any issue. I already checked the profile permission fls and class access everything seems fine.

HELP!!!!
 
Hi All,
I have created one custom object and I want to add Master-Detail Relationship with Task object but I am not getting Task object in Related To dropdown. 
User-added image
Kindly advise me for the same.

Thanks.
 
Good Afternoon,

I am hoping soeone may be able to advise on best practise. Each day a number of new 'files' are added to our insance of salesforce  , these are related to specfic accounts. Is there a way using a trigger or flows of auotmaically adding these to the relevant account if it can be identidied via the file name.

Many Thanks
Can we get this functionality by creating lightning compnent? Note: The lightning component has to load immediately when the user enters the communty home page.