Scenario
I have Community users that I need to share some account records to, where the Account that the Community user is linked to is referenced by a lookup field on the Account record to share.
I have set the OWD for external users on Account to Private, with it set to Read/Write for internal users. I have enabled manual sharing and created a trigger on Account which should create a sharing rule on insert or update of the account record, however when I have been testing by manually editing account records it is not creating a record in the AccountShare, but I get no error messages in the log.
It is throwing an error when I try and do the database create
09:52:04:031 VARIABLE_ASSIGNMENT [48]|result|[{"success":false,"errors":[{"status":"REQUIRED_FIELD_MISSI (2 more) ...","message":"missing required fie (28 more) ..."}]}]|0x12272438
Trigger Code
trigger WSC_Account_Trigger on Account (before insert, after insert, after update) {
/*
* Account trigger - mainly required for security settings for customer premises
*/
If(Trigger.isAfter&&trigger.isInsert){
WSC_Account_Trigger_Handler.accountShare(Trigger.New);
//call helper class to set Apex account sharing
}
If(trigger.isAfter&&Trigger.isUpdate){
WSC_Account_Trigger_Handler.accountShare(Trigger.New);
//call helper class to set Apex account sharing
}
}
Trigger Handler
public class WSC_Account_Trigger_Handler {
/*
* Account Trigger handler
* Main purpose to use Apex Sharing for Customer accounts to the relevant retailers using the
*
*/
Public static void accountShare(List<Account> shareAccounts){
List<AccountShare> toShareAccount = new List<AccountShare>();
AccountShare shAccounts = new AccountShare();
for(Account newAcc : shareAccounts){
//Water retailer shares
system.Debug('premises name '+newAcc.Name);
if(newAcc.Retailer__c !=Null){
Id waterRetailerId = newAcc.Retailer__c;
Id PremisesId = newAcc.Id;
List<User> accUsers = new List<User>();
accUsers = [SELECT Id FROM user WHERE User.AccountId = :waterRetailerId];
System.debug('accUsers '+ accUsers.size());
for(User shareUser : accUsers){
System.debug('Premises name '+ newAcc.Name+' '+PremisesId);
shAccounts.AccountId = PremisesId;
shAccounts.UserOrGroupId = shareUser.Id;
shAccounts.AccountAccessLevel = 'Read';
toShareAccount.add(shAccounts);
}
}
} //end water retailer shares
for(Account newAcc : shareAccounts){
//Sewerage retailer shares
if(newAcc.Sewerage_Retailer__c !=Null&&newAcc.Retailer__c!=newAcc.Sewerage_Retailer__c){
Id sewerageRetailerId = newAcc.Sewerage_Retailer__c;
List<User> accUsers = new List<User>();
accUsers = [SELECT Id FROM user WHERE User.AccountId = :sewerageRetailerId];
System.debug('accUsers '+ accUsers.size());
for(User shareUser : accUsers){
shAccounts.AccountId = newAcc.Id;
shAccounts.UserOrGroupId = shareUser.Id;
shAccounts.AccountAccessLevel = 'Read';
toShareAccount.add(shAccounts);
}
}
} //end sewerage retailer shares
System.debug('No of shares to create'+toShareAccount.size());
System.assert(toShareAccount.size()>0);
Database.SaveResult[] result = Database.insert(toShareAccount, false);
}
}