Sunday, September 20, 2015

Programmatically Creating Sharing Rules with Apex

Bussiness:

   When i Creating Contact,i selected check box true(Make_Public__c) the Record is share with Other group in Organization.

   if Make_Public__c it is False,delete the sharing record.

Contact is Private in OWD.



trigger ContactMakePublicTrigger on Contact (after insert, after update) {

  // get the id for the group for everyone in the org
  ID groupId = [select id from Group where Type = 'Organization'].id;

  // inserting new records
  if (Trigger.isInsert) {

    List<ContactShare> sharesToCreate = new List<ContactShare>();// sharing object of contact

    for (Contact contact : Trigger.new) {
      if (contact.Make_Public__c == true) {

        // create the new share for group
        ContactShare cs = new ContactShare();
        cs.ContactAccessLevel = 'Edit';
        cs.ContactId = contact.Id;
        cs.UserOrGroupId =  groupId;
        sharesToCreate.add(cs);

      }
    }

    // do the DML to create shares
    if (!sharesToCreate.isEmpty())
      insert sharesToCreate;

  // updating existing records
  } else if (Trigger.isUpdate) {

    List<ContactShare> sharesToCreate = new List<ContactShare>();
    List<ID> shareIdsToDelete = new List<ID>();

    for (Contact contact : Trigger.new) {

      // if the record was public but is now private -- delete the existing share
      if (Trigger.oldMap.get(contact.id).Make_Public__c == true &amp;&amp; contact.Make_Public__c == false) {
        shareIdsToDelete.add(contact.id);

      // if the record was private but now is public -- create the new share for the group
      } else if (Trigger.oldMap.get(contact.id).Make_Public__c == false &amp;&amp; contact.Make_Public__c == true) {

        // create the new share with read/write access
        ContactShare cs = new ContactShare();
        cs.ContactAccessLevel = 'Edit';
        cs.ContactId = contact.Id;
        cs.UserOrGroupId =  groupId;
        sharesToCreate.add(cs);

      }

    }

    // do the DML to delete shares
    if (!shareIdsToDelete.isEmpty())
      delete [select id from ContactShare where ContactId IN :shareIdsToDelete and RowCause = 'Manual'];

    // do the DML to create shares
    if (!sharesToCreate.isEmpty())
      insert sharesToCreate;

  }

}

============Test Class==============
@isTest
private class TestContactMakePublicTrigger {

    // test that newly inserted records marked as pubic=true have corresponding shares created
    static testMethod void testAddShares() {

    Set<ID> ids = new Set<ID>();
    List<Contact> contacts = new List<Contact>();

    for (Integer i=0;i<50;i++)
      contacts.add(new Contact(FirstName='First ',LastName='Name '+i,
        Email='email'+i+'@email.com',Make_Public__c=true));

    insert contacts;

    // get a set of all new created ids
    for (Contact c : contacts)
      ids.add(c.id);

    // assert that 50 shares were created
    List<ContactShare> shares = [select id from ContactShare where 
      ContactId IN :ids and RowCause = 'Manual'];
    System.assertEquals(shares.size(),50);

    }

    // insert records and switch them from public = true to public = false
    static testMethod void testUpdateContacts() {

    Set<ID> ids = new Set<ID>();
    List<Contact> contacts = new List<Contact>();

    for (Integer i=0;i<50;i++)
      contacts.add(new Contact(FirstName='First ',LastName='Name '+i,
        Email='email'+i+'@email.com',Make_Public__c=false));

    insert contacts;

    for (Contact c : contacts)
      ids.add(c.id);

    update contacts;

    // assert that 0 shares exist
    List<ContactShare> shares = [select id from ContactShare where 
      ContactId IN :ids and RowCause = 'Manual'];
    System.assertEquals(shares.size(),0);

    for (Contact c : contacts)
      c.Make_Public__c = true;

    update contacts;

    // assert that 50 shares were created
    shares = [select id from ContactShare where ContactId IN :ids and RowCause = 'Manual'];
    System.assertEquals(shares.size(),50);

    for (Contact c : contacts)
      c.Make_Public__c = false;

    update contacts;

    // assert that 0 shares exist
    shares = [select id from ContactShare where ContactId IN :ids and RowCause = 'Manual'];
    System.assertEquals(shares.size(),0);

    }
}

No comments:

Post a Comment