Apex Trigger That Creates Apex managed sharing records based on junction object’s relationships – Test Class – Time Complexity

Here's an example of an Apex trigger that creates Apex managed sharing records based on junction object's relationships:

trigger ShareJunctionObjectRecords on JunctionObject__c (after insert, after update) {
  // Create a set of the ID's of the related objects on the junction object
  Set<Id> relatedObjectIds = new Set<Id>();
  for (JunctionObject__c junctionObject : Trigger.new) {
    relatedObjectIds.add(junctionObject.RelatedObject1__c);
    relatedObjectIds.add(junctionObject.RelatedObject2__c);
  }

  // Query for the related objects
  Map<Id, RelatedObject__c> relatedObjects = new Map<Id, RelatedObject__c>([SELECT Id, OwnerId FROM RelatedObject__c WHERE Id IN :relatedObjectIds]);

  // Create a list of Apex sharing records to insert
  List<ApexManagedSharing> sharesToInsert = new List<ApexManagedSharing>();
  for (JunctionObject__c junctionObject : Trigger.new) {
    // Get the related objects
    RelatedObject__c relatedObject1 = relatedObjects.get(junctionObject.RelatedObject1__c);
    RelatedObject__c relatedObject2 = relatedObjects.get(junctionObject.RelatedObject2__c);

    // If both related objects are owned by the same user, add a sharing record
    // to allow the owner of both related objects to access both records
    if (relatedObject1.OwnerId == relatedObject2.OwnerId) {
      ApexManagedSharing share = new ApexManagedSharing();
      share.ParentId = relatedObject1.Id;
      share.UserOrGroupId = relatedObject1.OwnerId;
      share.AccessLevel = 'Edit';
      sharesToInsert.add(share);

      share = new ApexManagedSharing();
      share.ParentId = relatedObject2.Id;
      share.UserOrGroupId = relatedObject1.OwnerId;
      share.AccessLevel = 'Edit';
      sharesToInsert.add(share);
    }
  }

  // Insert the Apex managed sharing records
  if (!sharesToInsert.isEmpty()) {
    Database.SaveResult[] results = Database.insert(sharesToInsert, false);

    // Check the results for any errors
    for (Database.SaveResult result : results) {
      if (!result.isSuccess()) {
        System.debug('Error inserting Apex managed sharing record: ' + result.getErrors()[0].getMessage());
      }
    }
  }
}

This trigger will create Apex managed sharing records for any new or updated junction objects where the related objects are owned by the same user. It will give the owner of the related objects edit access to both records.

Please note List<ApexManagedSharing> is a list of Apex managed sharing records. Apex managed sharing allows you to programmatically create, update, and delete sharing records for an object.

Apex managed sharing is useful when you want to share records in a way that cannot be achieved using standard sharing rules or manual sharing. For example, you might want to create a custom sharing rule based on a field value, or you might want to share records with a group of users that is dynamically determined at runtime.

To create an Apex managed sharing record, you can use the ApexManagedSharing object. Each ApexManagedSharing object represents a sharing record for an object, and has the following fields:

  • ParentId: The ID of the object being shared.
  • UserOrGroupId: The ID of the user or group being granted access to the object.
  • AccessLevel: The level of access being granted to the user or group. Possible values are 'Read', 'Edit', and 'All'.

To create an Apex managed sharing record, you can instantiate an ApexManagedSharing object, set the values of the ParentId, UserOrGroupId, and AccessLevel fields, and then insert the object using the Database.insert method.

Lets write a test class for above trigger

@isTest
private class ShareJunctionObjectRecordsTest {
  @isTest
  static void testShareJunctionObjectRecords() {
    // Create two related objects owned by the same user
    User owner = [SELECT Id FROM User LIMIT 1];
    RelatedObject__c relatedObject1 = new RelatedObject__c(Name='Test Related Object 1', OwnerId=owner.Id);
    insert relatedObject1;
    RelatedObject__c relatedObject2 = new RelatedObject__c(Name='Test Related Object 2', OwnerId=owner.Id);
    insert relatedObject2;

    // Create a junction object linking the two related objects
    JunctionObject__c junctionObject = new JunctionObject__c(
      RelatedObject1__c=relatedObject1.Id,
      RelatedObject2__c=relatedObject2.Id
    );
    insert junctionObject;

    // Verify that Apex managed sharing records were created for both related objects
    relatedObject1 = [SELECT Id FROM RelatedObject__c WHERE Id = :relatedObject1.Id];
    relatedObject2 = [SELECT Id FROM RelatedObject__c WHERE Id = :relatedObject2.Id];
    System.assertEquals(1, [SELECT COUNT() FROM ApexManagedSharing WHERE ParentId = :relatedObject1.Id AND UserOrGroupId = :owner.Id AND AccessLevel = 'Edit'], 'Expected 1 Apex managed sharing record for related object 1');
    System.assertEquals(1, [SELECT COUNT() FROM ApexManagedSharing WHERE ParentId = :relatedObject2.Id AND UserOrGroupId = :owner.Id AND AccessLevel = 'Edit'], 'Expected 1 Apex managed sharing record for related object 2');
  }
}

This test class creates two related objects that are owned by the same user, then creates a junction object linking those two related objects. It then verifies that Apex managed sharing records were created for both related objects, allowing the owner of the related objects to access both records.

How much would be the time complexity of this Apex Trigger in Big O Notation

The time complexity of the Apex trigger will depend on the number of junction objects being inserted or updated and the number of related objects that are owned by the same user.

The trigger first loops through the list of junction objects being inserted or updated, and adds the ID's of the related objects to a set. This operation has a time complexity of O(n), where n is the number of junction objects being inserted or updated.

Next, the trigger queries for the related objects using the ID's in the set. The time complexity of this operation will depend on the number of related objects being queried, which will be the same as the number of unique ID's in the set. If all of the related objects are owned by different users, then the time complexity of this operation will be O(n). If some of the related objects are owned by the same user, then the time complexity will be less than O(n).

Finally, the trigger loops through the list of junction objects again, and for each junction object it checks if the related objects are owned by the same user. If they are, it creates two Apex managed sharing records. The time complexity of this operation will be O(n), where n is the number of junction objects being inserted or updated.

Overall, the time complexity of the Apex trigger will be O(n), where n is the number of junction objects being inserted or updated.

Leave a Reply

Your email address will not be published. Required fields are marked *