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
kevin.chileskevin.chiles 

Apex Sharing Rule

Hello!

 

I have 2 objects, a ship to and an account.  If a user has access to the ship tochild record they should also have access to the account.  Access is granted by the Saels Rep Code on the ship to record.  This will need to match the Rep ID on the user record.  So far this is what I have.  I continue to get User.Rep__ID__c at line 7 unexpected token.  Any thoughts or code correction are greatly appreciated.  Thanks!

trigger shareSPA on InForceEW__shipto__c (after insert, after update) {

  //SPA_Share is the share table that was created when the OWD was set to "Private"
  List <Ship_To_Sharing> shiptoShares = new List <Ship_To_Sharing>();
  
  for(User us:[select Id, Rep_ID__c from User]);
  for (InForceEW__shipto__c st : [select Id,InForceEW__Account__c,Sales_Rep_Code__c From InForceEW__shipto__c Where Sales_Rep_Code__c=User.Rep_ID__c];  {


    if (trigger.isInsert) {
      Ship_To_Sharing shiptoShares = new Ship_To_Sharing();
      shiptoShares.ParentId = st.InForceEW__Account__c;
      shiptoShares.UserOrGroupId = st.Sales_Rep_Code__c;
      shiptoShares.AccessLevel = 'edit';
      shiptoShares.RowCause = Schema.Ship_To_Sharing.RowCause.ShipToSharing__c;
        
      shiptoShares.add(shiptoShare);
    }
    if (trigger.isUpdate) {
      InForceEW__shipto__c oldShipTo = Trigger.oldMap.get(shp.Id);
      if(shp.Sales_Rep_Code__c != oldDiscount.Sales_Rep_Code__c) {
      Ship_To_Sharing shiptoShares = new Ship_To_Sharing();
      shiptoShares.ParentId = st.InForceEW__Account__c;
      shiptoShares.UserOrGroupId = st.Sales_Rep_Code__c;
      shiptoShares.AccessLevel = 'edit';
      shiptoShares.RowCause = Schema.Ship_To_Sharing.RowCause.ShipToSharing__c;
        
        shiptoShares.add(shiptoShare);      
      }
    }
  }

  Database.SaveResult[] shiptoShareInsertResult = Database.insert(shiptoShares, false);

}
}

 

 

AditiSFDCAditiSFDC

Hi Kevin,

 

Just at the first look, I think it need a small code fix.

 

for(User us:[select Id, Rep_ID__c from User]);
  for (InForceEW__shipto__c st : [select Id,InForceEW__Account__c,Sales_Rep_Code__c From InForceEW__shipto__c Where Sales_Rep_Code__c= :us.Rep_ID__c]) {
    // your code block
}

 

Please let me know if this fix your issue.