function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chaitra GVChaitra GV 

Sharing a record using Apex code

Hi,
I had a query related to sharing a record

There are two fields in a record and in that you want that if the total of field is 100 or more then share the record with the user else if it less then keep it in private only

Can you please tell me how to do this using apex?
Best Answer chosen by Chaitra GV
mritzimritzi
For custom object, sample code is:
trigger MyCustomObjectTrigger on MyCustomObject__c(after Insert){
	if(Trigger.isInsert && Trigger.isAfter){
		//you can get required User Id by changing filter conditions
		User user = [Select Id From User Where Name = '<Desired Name>' Limit 1];
		List<MyCustomObject__Share> objShareList = new List<MyCustomObject__Share>();
		for(MyCustomObject__c obj:Trigger.new){
			if(obj.Field1__c + obj.Field2__c >= 100){
				MyCustomObject__Share objShare = new MyCustomObject__Share();
				objShare.ParentId = obj.Id;
				objShare.UserOrGroupId = user.Id;
                //you change access level as per requirement
                objShare.AccessLevel = 'Read';
				objShare.RowCause = 'Value more than 100';
				objShareList.add(objShare);
			}
		}
		if(objShareList.size() > 0){
			try{
				insert objShareList;
			}catch(Exception ex){
				System.debug(ex);
			}
		}
	}
}
For Standard Object, APIs are slightly different
Share object is: <ObjectName>Share -> AccountShare
ParentId is replace by <ObjectName>Id -> AccountId
Details: https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_opportunityshare.htm
Custom Object: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_bulk_sharing_creating_with_apex.htm

Please mark this as Best Answer, if this helps solve your problem.
 

All Answers

mritzimritzi
For custom object, sample code is:
trigger MyCustomObjectTrigger on MyCustomObject__c(after Insert){
	if(Trigger.isInsert && Trigger.isAfter){
		//you can get required User Id by changing filter conditions
		User user = [Select Id From User Where Name = '<Desired Name>' Limit 1];
		List<MyCustomObject__Share> objShareList = new List<MyCustomObject__Share>();
		for(MyCustomObject__c obj:Trigger.new){
			if(obj.Field1__c + obj.Field2__c >= 100){
				MyCustomObject__Share objShare = new MyCustomObject__Share();
				objShare.ParentId = obj.Id;
				objShare.UserOrGroupId = user.Id;
                //you change access level as per requirement
                objShare.AccessLevel = 'Read';
				objShare.RowCause = 'Value more than 100';
				objShareList.add(objShare);
			}
		}
		if(objShareList.size() > 0){
			try{
				insert objShareList;
			}catch(Exception ex){
				System.debug(ex);
			}
		}
	}
}
For Standard Object, APIs are slightly different
Share object is: <ObjectName>Share -> AccountShare
ParentId is replace by <ObjectName>Id -> AccountId
Details: https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_opportunityshare.htm
Custom Object: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_bulk_sharing_creating_with_apex.htm

Please mark this as Best Answer, if this helps solve your problem.
 
This was selected as the best answer
raj_sfdccraj_sfdcc
Hi chaitra,
we can achieve this using Apex trigger in the required object.

Please find the below post which might help you in understanding with full end to end coding for the similar scenioros.

Apex sharing based on criteria (http://salessforcehacks.blogspot.com/2020/01/apex-managed-sharing-with-real-time.html)