+ Start a Discussion
Shrey TyagiShrey Tyagi 

Unable to add values to Map<Id,List<Id>> -Please help!!!

Hi ,
 I have this code below. I need to add values to Map<Id,List<Id>>, it is giving an error , can anyone pleas ehelp?


trigger Project_Risk_Review_Trigger on Project_Risk_Review__c (after insert,after update) {
   
   
         // Create a new list of sharing objects for Project_Risk_Review
         List<Project_Risk_Review__Share> ProjectRiskReviewShrs  = new List<Project_Risk_Review__Share>();

         Map<Id,List<Id>> OldSharesMap=new Map<Id,List<Id>>();
         // Declare variables for super user sharing.
         Project_Risk_Review__Share SuperUserShrDvp;
         Project_Risk_Review__Share SuperUserShrDelegate;
         Project_Risk_Review__Share SuperUserShrProjectDirector;
         Project_Risk_Review__Share SuperUserShrChairReviewer;
         for(Project_Risk_Review__c NewAssessment : trigger.new){
                  
                  Project_Risk_Review__c oldReview = Trigger.oldMap.get(NewAssessment.ID);                  
                  if(NewAssessment.DVP__c != Null && ((Trigger.IsInsert && Trigger.IsAfter)||(Trigger.IsUpdate && Trigger.IsAfter && (NewAssessment.DVP__c !=oldReview.DVP__c)))){
                      // Instantiate the sharing objects
                      SuperUserShrDvp = new Project_Risk_Review__Share();
                      // Set the ID of record being shared
                      SuperUserShrDvp.ParentId = NewAssessment.Id;
                      // Set the ID of user or group being granted access
                      SuperUserShrDvp.UserOrGroupId =NewAssessment.DVP__c;
                      SuperUserShrDvp.AccessLevel = 'edit';
                      // Set the Apex sharing reason for DVP
                      SuperUserShrDvp.RowCause = Schema.Project_Risk_Review__Share.RowCause.PRAS_Super_Users_R_W_Access__c;
                      // Add objects to list for insert
                      ProjectRiskReviewShrs.add(SuperUserShrDvp);
                      if(Trigger.IsAfter && Trigger.IsUpdate){
                        OldSharesMap.put(oldReview.ID,oldReview.DVP__c);
                      }
                  }
Shrey TyagiShrey Tyagi
Forgot to Mention DVP__C is a lookup field on User Object
Amit Chaudhary 8Amit Chaudhary 8
Please to add code like below
trigger Project_Risk_Review_Trigger on Project_Risk_Review__c (after insert,after update) {
   
   
         // Create a new list of sharing objects for Project_Risk_Review
         List<Project_Risk_Review__Share> ProjectRiskReviewShrs  = new List<Project_Risk_Review__Share>();

         Map<Id,List<Id>> OldSharesMap=new Map<Id,List<Id>>();
         // Declare variables for super user sharing.
         Project_Risk_Review__Share SuperUserShrDvp;
         Project_Risk_Review__Share SuperUserShrDelegate;
         Project_Risk_Review__Share SuperUserShrProjectDirector;
         Project_Risk_Review__Share SuperUserShrChairReviewer;
         for(Project_Risk_Review__c NewAssessment : trigger.new)
		 {
                  
		  Project_Risk_Review__c oldReview = Trigger.oldMap.get(NewAssessment.ID);                  
		  if(NewAssessment.DVP__c != Null && ((Trigger.IsInsert && Trigger.IsAfter)||(Trigger.IsUpdate && Trigger.IsAfter && (NewAssessment.DVP__c !=oldReview.DVP__c))))
		  {
			  // Instantiate the sharing objects
			  SuperUserShrDvp = new Project_Risk_Review__Share();
			  // Set the ID of record being shared
			  SuperUserShrDvp.ParentId = NewAssessment.Id;
			  // Set the ID of user or group being granted access
			  SuperUserShrDvp.UserOrGroupId =NewAssessment.DVP__c;
			  SuperUserShrDvp.AccessLevel = 'edit';
			  // Set the Apex sharing reason for DVP
			  SuperUserShrDvp.RowCause = Schema.Project_Risk_Review__Share.RowCause.PRAS_Super_Users_R_W_Access__c;
			  // Add objects to list for insert
			  ProjectRiskReviewShrs.add(SuperUserShrDvp);
			  
			  if(Trigger.IsAfter && Trigger.IsUpdate)
			  {
					if(OldSharesMap.containsKey(oldReview.ID) )
					{
						List<Id> lstId = OldSharesMap.get(oldReview.ID);
						lstId.add(oldReview.DVP__c);
					}
					else
					{
						List<Id> lstId = new List<ID>();
						lstId.add(oldReview.ID);
						OldSharesMap.put(oldReview.ID,lstId);
					}
			  }
		  }

Let us know if this will help you