6

In an existing instance, we have a territory hierarchy with no rules, just manual assignments. Since the Summer '13 release we are seeing AccountShare records with RowCause of 'Territory' which indicates a rule-based assignment. Additionally, we are unable to delete these records

The following code produces an INVALID_CROSS_REFERENCE_KEY error, even though all of the related records exist. While the error originates in a larger piece of code, I've extracted the following to reproduce the problem. The instance has Territory Management enabled and the code was working prior to the :Summer '13 update.

for (AccountShare s : [Select Id, UserOrGroupId, RowCause, IsDeleted From AccountShare Where AccountId = '001U0000006UFZE']) {
    if (s.RowCause == 'Territory') {
        System.debug(s.Id + ': ' + s.RowCause + ' - IsDelted: ' + s.IsDeleted);
        for (Group g : [Select RelatedId From Group Where Id = :s.UserOrGroupId]) {
            System.debug('Territory is ' + g.RelatedId);
            for (Territory t : [Select Name From Territory Where Id = :g.RelatedId]) {
                System.debug('Territory is ' + t.Name);
            }
        }
        try {
            delete s;
        } catch (Exception ex) {
            System.debug('Deletion failed: ' + ex.getMessage());
        }
    }
}

USER_DEBUG [13]|DEBUG|Deletion failed: Delete failed. First exception on row 0 with id 00rU0000004yvbMIAQ; first error: INVALID_CROSS_REFERENCE_KEY, id does not exist: []

1

Abrar, here (http://sforcecloud.blogspot.fr/2013/05/territory-management-in-apex.html) seems to assert that it is not possible to delete Territory AccountShares with APEX (DML).

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.