0

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);
    }

}
  • It says required fields are missing, check if all the fields are being populated as expected for insertion. – Raul Jan 23 '17 at 10:17
0

You are missing setting OpportunityAccessLevel attribute while preparing AccountShare records to insert. OpportunityAccessLevel is required to insert AccountShare record. So Below correction can help you:

   for(User shareUser : accUsers){
        System.debug('Premises name '+ newAcc.Name+' '+PremisesId);
        shAccounts.AccountId = PremisesId;
        shAccounts.UserOrGroupId = shareUser.Id;
        shAccounts.AccountAccessLevel = 'Read';
        shAccounts.OpportunityAccessLevel='Read';
        toShareAccount.add(shAccounts);
        }
  • I have actually added all the other sharing, which in my scenario is OpportunityAccessLevel='None', CaseAccessLevel ='Read',ContactAccessLevel='None'. Thanks. – Dave Humm Jan 23 '17 at 10:46
1


You are missing OpportunityAccessLevel
please refer following for more details.
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_accountshare.htm

     for(Account objAccount: accountList){
        AccountShare objAccountShare = new AccountShare ();
        objAccountShare.AccountId = objAccount.Id;
        objAccountShare.UserOrGroupID  = 'PutuserOrGroupId';
        objAccountShare.AccountAccessLevel  = 'read';
        objAccountShare.OpportunityAccessLevel = 'read';
        accountSharesInsertList.add(objAccountShare);
    }//for

Thanks.
Nachiket

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.