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
vijaymindvijaymind 

Record Sharing Through Apex Trigger

Hi All ,
I have an issue . Let me explain the problem.
I am sharing A custom object Record through before trigger . And the Object is OWD Private.

I am changing record owner as System Administrator who ever insert the record .
User u = [select Id from User where Profile.Name='System Administrator' Limit 1];
for(Project__c p : trigger.new){
p.ownerId = U.Id;
}
Now let say any of the user insert the Project__c records trigger will change the owner.
Once owner is change as System Admin. This user can not share this recod with other users because OWD is private.

The User who insert this record do not have privelage to share the record.

Now suggest me how to share these records in sigle trigger script.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Actually you are incorrect here.

 

Regardless of if the owner was changed or not, the trigger still runs in system context and without sharing. A class also operates without sharing by default.

 

You had something else going on that was not related to the sharing of the record through apex.

All Answers

ngabraningabrani

You could use Apex sharing rules to give permission to access records to additional users/groups.

 

Take a look at the article below -

http://astreait.com/wordpress/?p=26

Starz26Starz26

You should first investigate if the use of sharing rules would work in your situation. They are the prefered way to go and are eaiser to maintain. you can share to roles and / or groups as well as include people below those roles.

 

But, if that does not work, here is a basic example of adding a record to the object share. Make sure you set up Apex Sharing Reasons on the object to clarify whay someone has rights.

 

trigger Sharing on Assignment_Request__c (After Delete, After Update) {


for(Assignment_Request__c ar : trigger.new){

    //Add new share if field has changed
    if(ar.POCS__c != trigger.oldMap.get(ar.id).POCS__c){
    
    FSA__c FSA = [Select Account_ID__c From FSA__c Where FSA_Number__c = :ar.FSA_Number__c Limit 1];
    AccountShare aShare = New AccountShare();
    aShare.UserOrGroupID = ar.POCS__c;
    aShare.AccountID = FSA.Account_ID__c;
    aShare.AccountaccessLevel = 'Read';
    aShare.OpportunityAccessLevel = 'Read';
    aShare.rowCause = '****ENTER REASON HERE****'    
    
    Insert aShare;
    
    //Delete old share
    for(Assignment_Request__c oar : trigger.old){
    AccountShare[] accShare = [Select ID, RowCause From AccountShare Where UserOrGroupID = :oar.POCS__c Limit 1];
    if(!accShare.isEmpty())
        if(accShare[0].RowCause == 'Manual')
        delete accShare;
    }
    }
    
    

}
}

 

vijaymindvijaymind

Hi Starz26 ,

Thanks for reply,

I have read your post but the problem is not which you trying to explain.

Once I change the record owner setting to System Admin. Then the user in which context the script is runnig lost the permission to share the record. So what I did ,

I create a class without sharing and migrate the whole trigger code in that class.

 

In this way I was handle to share the records.

Over all I was expecting that Once update the Record Owner then below this statement all script should run in context on System Admin because now only System Admin Can share that record.

 

 

Starz26Starz26

Actually you are incorrect here.

 

Regardless of if the owner was changed or not, the trigger still runs in system context and without sharing. A class also operates without sharing by default.

 

You had something else going on that was not related to the sharing of the record through apex.

This was selected as the best answer
jeniffer homesjeniffer homes
You should first investigate if the use of sharing rules would work in your situation. They are the prefered way to go and are eaiser to maintain. you can share to roles and / or groups as well as include people below those roles.

Thanks
Regards
Jeniffer (http://www.greatbasinindustrial.com/products/air-pollution-control/)
pranav dinakaran 11pranav dinakaran 11
https://salesforce-fundamentals-by-pranav.blogspot.in
We can achieve it without writing apex code