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
Irish@accIrish@acc 

Apex Managed sharing: need help

Hi,

I have a situation like make the record as Read on a object (when <object>.<Technical Agency>.<Vendor Name> = <Dev_Lead>.<CompanyName> )..

there is a fied Technical Agency which is lookup to vendor and the Dev_Lead__c is lookup to user.

 

how to write the sharing for this situation please help

SimonJaiSimonJai

I think this is similar to what you are looking for.

 

trigger objectSharing on object__c (after insert) {
	
	// Create a list to create all the shares at once
	List<object__Share> listOfShares = new List<object__Share>();
	
	// Traverse through every new record
	for (object__c tempObject : trigger.new) {
	
		if (tempObject.Technical Agency.Vendor Name = Dev_Lead.CompanyName) {
	
			// Create a new sharing record
			object__Share objectShare = new object__Share();
 
			// Populate the sharing record to identify which record is being shared
			objectShare.ParentId = tempObject.Id;
			
			// Set the dev lead as to who this sharing applies to
			objectShare.UserOrGroupId = Dev_Lead__c;
			
			// Give the Dev_Lead__c read rights (None, Read, Edit, All)
			objectShare.AccessLevel = 'Read';
			
			// Define the reason for the share
			// This is created via Apex Sharing Reason on the custom object
			// Not sure if this is optional but it's good practice to include it
			objectShare.RowCause = Schema.object__Share.RowCause.<Apex_Sharing_Reason_Name>__c;

			listOfShares.add(objectShare);
		
		}
	
	}
	
	// Only create sharing rule if list of shares is not empty
	if (listOfShares.size() > 0) {
		insert listOfShares;
	}

}

 

Irish@accIrish@acc
How do i get the Technical_Agency__c.Name this Name is coming from another object Vendor object
Vinit_KumarVinit_Kumar

You need to use SOQL to query the Technical_Agency__c.Name based on relationship field.

 

select Technical_Agency__r.Name from Vendor__c where id =<some id>

Irish@accIrish@acc
Hi Vinit
this is not working not able to get the Technical_agncy name
on project__c object there are two fileds one is Dev_Specialist__c which is in lookp with user and one is Technical_Agency__c which is in lookup with Vendor__c obj and i need company name in first case and vendor name in 2nd case
SimonJaiSimonJai

Vinit_Kumar wrote:

You need to use SOQL to query the Technical_Agency__c.Name based on relationship field.

 

select Technical_Agency__r.Name from Vendor__c where id =<some id>


Like Vinit has said, you need to query the names via the relationship fields.

SELECT User__r.name FROM Dev_Specialist__c WHERE Id =: <some_id>

 

SELECT Technical_Agency__r.name FROM Vendor__c WHERE Id =: <some_id>

 

Not sure how all your objects are related to project, but it would be similar. The "__r" denotes a relationship.

Irish@accIrish@acc
but I think this __r doesn't work in production
Vinit_KumarVinit_Kumar

Irish,

 

You will have to check how your objects are related and based upon relationship field,you can query the name field.