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
Bryan Revelant 7Bryan Revelant 7 

Trigger on Sharring custom object




Hello,

I have a cross object that looks up to a user and a parent. I have put a trigger on the parent, trying to give sharing access to all users that are on the child object user lookup.

I am getting the below error message:

Apex trigger ADRSharringTrigger caused an unexpected exception, contact your administrator: ADRSharringTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_ACCESS_LEVEL, : []: Trigger.ADRSharringTrigger: line 29, column 1

trigger ADRSharringTrigger on ADR__c (after update) {

      List<ADR__Share> ADR_Shares = new List<ADR__Share>();

   
    list<id> ADRSubmittedList = new list<id>();
    list<ADRUser__c> ADRUserList = new list<ADRUser__c>();
   
    for(ADR__c lstADRTrigger : trigger.new){
        if(lstADRTrigger.Next_Step__c == 'Submit'){
            ADRSubmittedList.add(lstADRTrigger.id);
        }

    for(ADRUser__c lstADRUser : [Select ID, Status__c, ADR__c, User__c from ADRUser__c where ADR__c in :ADRSubmittedList] ){
    
     ADR__Share  ADR_share = new ADR__Share();
    ADR_share.ParentId = lstADRUser.ADR__c;
    ADR_share.AccessLevel = 'All';
     ADR_share.UserOrGroupId = lstADRUser.User__c;

    
      SCD_Shares.add(scd_share);
    
    
     insert SCD_Shares;
    
    
   
    }
}
NehalNehal (Salesforce Developers) 

Hi,

Full Access can only be granted through Force.com managed sharing.
Force.com managed sharing involves sharing access granted by Force.com through based on record ownership, the role hierarchy, and sharing rules. Check:

http://www.clienthouse.com/images/21b6368951/salesforce_summer08_release_notes.pdf
Page 83

If you are writing Apex code to share the record you are not doing "Force.com Managed Sharing", but "User Managed Sharing" or "Apex Managed Sharing".

With manual sharing you can only grant Read/Write access.

With your code, I could figure out that you are granting "Full Access".  With "Read/Write" access, if you check, this will not return you the error.
 

Also please refer below links for your reference:

1.http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_bulk_sharing_understanding.htm#sharing_access_levels  
                                     2.https://developer.salesforce.com/page/Using_Apex_Managed_Sharing_to_Create_Custom_Record_Sharing_Logic

I hope this helps.

Bryan Revelant 7Bryan Revelant 7
I have the objects marked as private, and I will not know whom the users share to. it should be Dynamic. I had

ADR_share.AccessLevel = 'Read/Write' however this stated that it was an invalid pick list as well.
NehalNehal (Salesforce Developers) 
Hi,

Please refer to links below as that might helps:

1.https://developer.salesforce.com/forums/ForumsMain?id=906F00000008yiVIAQ
2.http://mindfiresfdcprofessionals.wordpress.com/2014/01/22/how-to-create-sharing-rules-via-apex/
3.http://salesforce.stackexchange.com/questions/3613/can-we-use-dynamic-apex-to-share-a-record
4.http://astreait.com/Creating_Apex_Sharing_rules/

I hope this helps.
Bryan Revelant 7Bryan Revelant 7
Ahhh nevermind.....  I got it.....
NehalNehal (Salesforce Developers) 


Hi,

Please mark this as a "Best Answer" if this has resolved your issue.
Bryan Revelant 7Bryan Revelant 7
Actually now I am getting a weird error. It seems like I am not able to enter in multiple users with my trigger. I can enter one user on my cross object however when I enter to it give me this error? Error:Apex trigger ADRSharringTrigger caused an unexpected exception, contact your administrator: ADRSharringTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0 with id 02cc0000020FKLQAA4; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]: Trigger.ADRSharringTrigger: line 34, column 1 CODE: trigger ADRSharringTrigger on ADR__c (after update) { List SCD_Shares = new List(); list ADRSubmittedList = new list(); list ADRUserList = new list(); for(ADR__c lstADRTrigger : trigger.new){ if(lstADRTrigger.Next_Step__c == 'Submit'){ if(trigger.oldmap.get(lstADRTrigger.id).Next_Step__c != 'Submit'){ ADRSubmittedList.add(lstADRTrigger.id); }//*** End of if(lstADRTrigger.Next_Step__c... }//*** End of for(ADR__c lstADRTrigger : trigger.new){ } for(ADRUser__c lstADRUser : [Select ID, Status__c, ADR__c, User__c from ADRUser__c where ADR__c in :ADRSubmittedList] ){ ADR__Share scd_share = new ADR__Share(); scd_share.ParentId = lstADRUser.ADR__c; scd_share.AccessLevel = 'edit'; scd_share.UserOrGroupId = lstADRUser.User__c; SCD_Shares.add(scd_share); system.debug(SCD_Shares); insert SCD_Shares; } }
Bryan Revelant 7Bryan Revelant 7
Sorry let me format the code a bit better.. It looks like it should suck in two users but bombs. Works with one

Error message

Error:Apex trigger ADRSharringTrigger caused an unexpected exception, contact your administrator: ADRSharringTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0 with id 02cc0000020FKLQAA4; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]: Trigger.ADRSharringTrigger: line 34, column 1 CODE:




trigger ADRSharringTrigger on ADR__c (after update) {

      List<ADR__Share> SCD_Shares = new List<ADR__Share>();

   
    list<id> ADRSubmittedList = new list<id>();
    list<ADRUser__c> ADRUserList = new list<ADRUser__c>();
   
    for(ADR__c lstADRTrigger : trigger.new){
        if(lstADRTrigger.Next_Step__c == 'Submit'){
       
         if(trigger.oldmap.get(lstADRTrigger.id).Next_Step__c != 'Submit'){
        
            ADRSubmittedList.add(lstADRTrigger.id);
        }//*** End of if(lstADRTrigger.Next_Step__c...
        
    }//*** End of for(ADR__c lstADRTrigger : trigger.new){
   
}
 
    for(ADRUser__c lstADRUser : [Select ID, Status__c, ADR__c, User__c from ADRUser__c where ADR__c in :ADRSubmittedList] ){
   
     ADR__Share scd_share = new ADR__Share();
       scd_share.ParentId = lstADRUser.ADR__c;
      scd_share.AccessLevel = 'edit';
       scd_share.UserOrGroupId = lstADRUser.User__c;
 
    
      SCD_Shares.add(scd_share);
    
     system.debug(SCD_Shares);
    
    
     insert SCD_Shares;
    
    }
}
vikrant Chauhan 5vikrant Chauhan 5
Hi,
It should be ADR_share.AccessLevel = 'Read'; then it will not give you the error "first error: INVALID_ACCESS_LEVEL, : []:"
Hope it helps.If you find it useful ,Please mark my answer as best answer.