+ Start a Discussion
CliffACliffA 

AccountAccessLevel errors on insert

I have a trigger that fires when accounts are merged so that the surviving account has a untion of territory assignments for the two accounts.  For example, if the master account is in territory A and the duplicate is in territory B then after the merge the master account is associated with both territories A and B.

 

The problem is that for certain records I'm getting an error on insert into AccountShare.

 

My original code was this:

 

List<AccountShare> acctSharesList = acctToShares.get(c.AccountId); // list of shares from "duplicate" account
for (AccountShare a : acctSharesList) {
AccountShare tmpAS = new AccountShare();
tmpAS.AccountId = masterContacts.get(c.MasterRecordID).AccountId; // get Account ID of Master account
tmpAS.UserOrGroupId = a.UserOrGroupId; // Get id from Duplicate account
// Don't add the share if the master already has it
if (!(masterAcctToShares.get(tmpAS.AccountId).containsKey(tmpAS.UserOrGroupId))) {
newAccountShares.add(tmpAS);
}
}

// add new entries for the territories
insert newAccountShares;

 

 

For some records when the trigger executes I get the following error:

 

First exception on row 0 with id 0034000000Xewa8AAB; first error: DELETE_FAILED, ContactMerge: execution of AfterDelete

caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, missing required field: AccountAccessLevel: [AccountAccessLevel]

 

 

So, in response I updated the trigger to set that value (and another value based on an interim error that I haven't included in this post):

 

List<AccountShare> acctSharesList = acctToShares.get(c.AccountId); // list of shares from "duplicate" account
for (AccountShare a : acctSharesList) {
AccountShare tmpAS = new AccountShare();
tmpAS.AccountId = masterContacts.get(c.MasterRecordID).AccountId; // get Account ID of Master account
tmpAS.UserOrGroupId = a.UserOrGroupId; // Get id from Duplicate account
tmpAS.AccountAccessLevel = 'Read';
tmpAs.OpportunityAccessLevel = 'None';
// Don't add the share if the master already has it
if (!(masterAcctToShares.get(tmpAS.AccountId).containsKey(tmpAS.UserOrGroupId))) {
newAccountShares.add(tmpAS);
}
}

// add new entries for the territories
insert newAccountShares;

 

 

Now I get the following error:

 

First exception on row 0 with id 0034000000Xewa8AAB; first error: DELETE_FAILED, ContactMerge: execution of AfterDelete

caused by: System.DmlException: Insert failed. First exception on row 1; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: AccountAccessLevel (Access level should not be specified on territory manual assignment): [AccountAccessLevel]

 

So the first error tells me the AccountAccessLevel is missing but when I add it to the trigger the error changes and it tells me it shouldn't be specified.

 

Not quite sure what to make of these seemingly contradictary results.

 

Has anyone encountered this before of have any suggestions?

 

Pradeep_NavatarPradeep_Navatar

In my opinion, the problem is related with the territory assignment rule.

 

go through the following documentaton :

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_bulk_sharing_understanding.htm                                  

http://community.salesforce.com/t5/Apex-Code-Development/How-to-insert-a-AccountSharing-record-linked-to-a-Territory/td-p/179545

 

Hope this helps.