+ Start a Discussion
DebjaniDebjani 

How to implement map between Case and case share in salesforce ?

How to implement map between Case and case share in salesforce ?
NOTE:if cases are shared to particular public group I don't want to re-share them to the groups, if not it should be share .Can any one please assist in validation of this using a map?
Best Answer chosen by Debjani
DebasisDebasis
Hi Lilly, in the above code, pass list of cases, it will calculate whihc are shared and whihc are not shared .
//pass list of case to this method to get its sharing record in map
		public void  getcaseAndCaseShareMap(list<case>  caseList){
		//Map to hold caseid and its shared records as a list
		map<id,list<caseShare>> caseAndSHareMap = new map<id,list<caseShare>>();
		
		list<CaseShare> caseShareList;
		set<id> allCaseId = new set<id>();
		//iterate on eachcase and take caseid in allCaseId set
		for(case eachCase :caseList){
			allCaseId.add(eachCase.id);
		}
		//if there is any id in set then query caseshaer object to get its associated cases
		if(allCaseId.size()>0){
			caseShareList = [SELECT Id, CaseId, UserOrGroupId, CaseAccessLevel, RowCause FROM CaseShare where caseid in:allCaseId];
		}
		//iterate on each case share and put the data in map as caseid as key and caseshare record as value
		for(CaseShare cs: caseShareList){
			if(caseAndSHareMap.get(cs.CaseId)!=null){
			caseAndSHareMap.get(cs.CaseId).add(cs);
			}
			else{
				caseAndSHareMap.put(cs.caseid,cs);
			}
		}
		 set<id> sharedCaseId = new case<id>();
		 if(caseAndSHareMap.size()>0){
			sharedCaseId=caseAndSHareMap.keyset();
		 }
		 
		 set<id> nonsharedcaseid = new case<id>();
		 if(sharedCaseId.size()>0){
		 nonsharedcaseid = allCaseId.removAll(sharedCaseId);
		 }
		 system.debug('The case id whihc are shared'+sharedCaseId);
		 system.debug('The case id whihc are Not shared yet'+nonsharedcaseid);
		}




 

All Answers

DebasisDebasis
Hi LILLY 11,

please check this below code for getting case and its sharing record in a map.
map will contain case id as key and list<caseshare> as value.
 

//pass list of case to this method to get its sharing record in map
		public map<id,list<caseShare>> getcaseAndCaseShareMap(list<case>  caseList){
		//Map to hold caseid and its shared records as a list
		map<id,list<caseShare>> caseAndSHareMap = new map<id,list<caseShare>>();
		
		list<CaseShare> caseShareList;
		set<id> allCaseId = new set<id>();
		//iterate on eachcase and take caseid in allCaseId set
		for(case eachCase :caseList){
			allCaseId.add(eachCase.id);
		}
		//if there is any id in set then query caseshaer object to get its associated cases
		if(allCaseId.size()>0){
			caseShareList = [SELECT Id, CaseId, UserOrGroupId, CaseAccessLevel, RowCause FROM CaseShare where caseid in:allCaseId];
		}
		//iterate on each case share and put the data in map as caseid as key and caseshare record as value
		for(CaseShare cs: caseShareList){
			if(caseAndSHareMap.get(cs.CaseId)!=null){
			caseAndSHareMap.get(cs.CaseId).add(cs);
			}
			else{
				caseAndSHareMap.put(cs.caseid,cs);
			}
		}
		}


please do let me know if it helps

Thanks,
Debasis
DebjaniDebjani
Hi Debasis,

In the code here we are sharing cases. How do i compare the cases which are shared and the cases which are not shared?
DebasisDebasis
Hi Lilly, in the above code, pass list of cases, it will calculate whihc are shared and whihc are not shared .
//pass list of case to this method to get its sharing record in map
		public void  getcaseAndCaseShareMap(list<case>  caseList){
		//Map to hold caseid and its shared records as a list
		map<id,list<caseShare>> caseAndSHareMap = new map<id,list<caseShare>>();
		
		list<CaseShare> caseShareList;
		set<id> allCaseId = new set<id>();
		//iterate on eachcase and take caseid in allCaseId set
		for(case eachCase :caseList){
			allCaseId.add(eachCase.id);
		}
		//if there is any id in set then query caseshaer object to get its associated cases
		if(allCaseId.size()>0){
			caseShareList = [SELECT Id, CaseId, UserOrGroupId, CaseAccessLevel, RowCause FROM CaseShare where caseid in:allCaseId];
		}
		//iterate on each case share and put the data in map as caseid as key and caseshare record as value
		for(CaseShare cs: caseShareList){
			if(caseAndSHareMap.get(cs.CaseId)!=null){
			caseAndSHareMap.get(cs.CaseId).add(cs);
			}
			else{
				caseAndSHareMap.put(cs.caseid,cs);
			}
		}
		 set<id> sharedCaseId = new case<id>();
		 if(caseAndSHareMap.size()>0){
			sharedCaseId=caseAndSHareMap.keyset();
		 }
		 
		 set<id> nonsharedcaseid = new case<id>();
		 if(sharedCaseId.size()>0){
		 nonsharedcaseid = allCaseId.removAll(sharedCaseId);
		 }
		 system.debug('The case id whihc are shared'+sharedCaseId);
		 system.debug('The case id whihc are Not shared yet'+nonsharedcaseid);
		}




 
This was selected as the best answer
DebjaniDebjani
I have public group name like Internal Users.

Can you please tell me where I have to check cases are shared to Internal Users public group?
If Internal Users public group is not shared to some cases ,I want to share that public group to non shared case.

Thanks!