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
arishi0arishi0 

Trigger to add sharing for custom object is not working! Sharing Rule not getting created.

I have a trigger that works on a custom object called Sales Credit Detail and is supposed to add a sharing to theOpportunity owner (which is a related object via lookup).

 

Our org wide default for this Sales Credit object is Private and not using hierarchies to grant access. The idea is that ONLY the sales credit detail owner AND the opportunity owner can edit the sales credit detail (from now on SCD) record. An Opportunity can have many SCDs and for each the owner of that record gets default FULL access, BUT also for each of these I want to add the Opportunity owner to be able to have full access to it as well.

 

I decided to add a trigger that would add a share to the SCD for the Opp owner BUT when I edit the SCD record, the rule is not created for the Opportunity owner. BElow is the code, can someone see if I am failing somewhere? I cannot figure it out. I have tested the list sizes and the opp owner etc and they all give the correct value, so I dont know why it is not working

 

 

trigger Opportunity_Owner_share on Sales_Credit_Detail__c (after insert, after update) {
    //Create list of sharing records to create for SCDs in api call. 
    List<Sales_Credit_Detail__Share> SCD_Shares = new List<Sales_Credit_Detail__Share>();
    List<Sales_Credit_Detail__c> SCDS;
   
   //get all Opportunities for all SCD that will be inserted.
   List<Opportunity> opps = [SELECT Id, OwnerId, Name From Opportunity Where Id IN(SELECT ParentOpportunity__c From Sales_Credit_Detail__c Where Id IN :Trigger.newMap.keyset())];
   System.Debug(opps.size());
   
   //For each SCD record being inserted/edited, create a sharing record to the Opportunity owner of that SCD
    for(Sales_Credit_Detail__c scd: Trigger.new){
	   Sales_Credit_Detail__Share scd_share = new Sales_Credit_Detail__Share();
	   scd_share.ParentId = scd.Id;
	   scd_share.AccessLevel = 'All';
	   scd_share.RowCause = Schema.Sales_Credit_Detail__share.RowCause.Opportunity_Owner_Access__c;
	   System.Debug(scd_share.RowCause);
	  
	   for(Opportunity o:opps){
	   	//Add share record to list of shares to upload IF not for Opp Owner same as SCD owner
	       if(o.Id == scd.ParentOpportunity__c && o.OwnerId != scd.OwnerId){
	           scd_share.UserOrGroupId = o.OwnerId;
	           }       	
	       }
	    // Add the new Share record to the list of new Share records.
	    SCD_Shares.add(scd_share);
	    System.Debug(SCD_Shares);
        }
        //Insert all shares to the Database.
        Try{
         insert(SCD_Shares);
        }catch (Exception e){
        	System.Debug(e);
        }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
hwelchhwelch

The issues are the picklist values you are picking.  You should verify the available values in your schema since they are different to the standard objects.

 

change

 

scd_share.AccessLevel = 'All';

to

 

scd_share.AccessLevel = 'Read/Write';

 

 

and change line

 

scd_share.RowCause = Schema.Sales_Credit_Detail__share.RowCause.Opportunity_Owner_Access__c;

 

to

 

scd_share.RowCause = 'Owner';

 

 

 

All Answers

hwelchhwelch

The issues are the picklist values you are picking.  You should verify the available values in your schema since they are different to the standard objects.

 

change

 

scd_share.AccessLevel = 'All';

to

 

scd_share.AccessLevel = 'Read/Write';

 

 

and change line

 

scd_share.RowCause = Schema.Sales_Credit_Detail__share.RowCause.Opportunity_Owner_Access__c;

 

to

 

scd_share.RowCause = 'Owner';

 

 

 

This was selected as the best answer
arishi0arishi0

YEs, you were right, the updates worked witha  little difference in the actual value s that I had to use. See details below:

 

 

 scd_share.AccessLevel = 'Edit';
  scd_share.RowCause = 'Opportunity_Owner_Access__c';
Other than that the solution was exact!!! 
THank you!!!!