Reply
Visitor
RandyK
Posts: 1
Accepted Solution

Set Sharing Rule via field value

I'm using the Partner Portal and need to set the sharing on a custom object record such that a Partner can see it, but not records of other partners. The record has a "Partner" field, which is a lookup to the Accounts object.

 

The goal is to avoid partners from viewing records that are not marked for them via the Partner field. This is a custom app that does not use lead or opportunity sharing features.

 

Manual sharing achieves my goal, but I want to make this a non-user function. OWD on the object is set to Private. 

 

What is the best practice, and what are the options for getting this done?

 

  Thank you,

   --Randy

 

Visitor
wjmoxley
Posts: 3

Re: Set Sharing Rule via field value

Write a trigger on the object that creates a manual share of the record to the "Partner User" role of the partner.  And set the object to private.  Here is a sample trigger that does something like this for a custom object (the MDF Budget object).

 

Obviously you'll need to change the name of the object on the trigger and change the name of the share table for the object.  Also you'll need to update the "Account__c" field to the name of the account field you use.

 

---------------------------------------------- 

 

trigger manuallyShareToPartner on SFDC_Budget__c (afterinsert, after update) {

    Integer i;

    UserRole partnerUserRole;

    Account partnerAccount;

    Group partnerGroup;

    List<SFDC_Budget__share>oldBudgetShare = new SFDC_Budget__share[0];

    String roleName;

    Boolean share;

   

 

    //Now loop through each fund claim to setthe Partner Account Id on the claim

    for (i=0; i<Trigger.new.size(); i++) {

        //If the NewAccount field is not empty and it has changed then manually share to thepartner

       

        //Share if it's anew record with a Partner Account or an old record where the partner accountfield is now filled in

        if(Trigger.isUpdate) {

           if ((Trigger.new[i].Account__c != NULL) && (Trigger.new[i].Account__c!= Trigger.old[i].Account__c)) {

               share=TRUE;

           }

        } else if(Trigger.isInsert && Trigger.new[i].Account__c != NULL) {

           share=TRUE;

        } else {

           share=FALSE;

        }

       

        if (share==TRUE){

           partnerAccount = [Select IsPartner, Name from Account whereId=:Trigger.new[i].Account__c];

           //Only share if the account is a partner account

           if (partnerAccount.IsPartner == TRUE) {

               

               //if the account name is really long, truncate the role name

               if (partnerAccount.Name.length() > 62) {

                   roleName = partnerAccount.Name.substring(0,62)+' Partner User';

               } else {

                   roleName = partnerAccount.Name+' Partner User';

               }

               System.debug('Rolename='+rolename);

                   

               partnerUserRole = [Select Id, Name from UserRole where Name=:roleName];

               partnerGroup = [select Id from Groupwhere RelatedId=:smileytongue:artnerUserRole.Id and Type='Role'];

               SFDC_Budget__share bugetShare = newSFDC_Budget__share(UserOrGroupId=partnerGroup.Id,RowCause='manual',ParentId=Trigger.new[i].Id,AccessLevel='Read');

               Insert bugetShare;

           }

        }

       

        //if the Accoutfield is NULL and didn't use to be delete the manual share to the partner

        if(Trigger.isUpdate) {

           if ((Trigger.new[i].Account__c != Trigger.old[i].Account__c) &&(Trigger.old[i].Account__c != NULL)) {

               partnerAccount = [Select IsPartner, Name from Account whereId=:Trigger.old[i].Account__c];

               //Only unshare if the account is a partner account

               if (partnerAccount.IsPartner == TRUE) {

                   

                   //if the account name is really long, truncate the role name

                   if (partnerAccount.Name.length() > 62) {

                       roleName = partnerAccount.Name.substring(0,62)+' Partner User';

                   } else {

                       roleName = partnerAccount.Name+' Partner User';

                   }

                   

                   partnerUserRole = [Select Id, Name from UserRole where Name=:roleName];

                   partnerGroup = [select Id from Group where RelatedId=:smileytongue:artnerUserRole.Id andType='Role'];

                   oldBudgetShare = [select Id from SFDC_Budget__share whereUserOrGroupId=:smileytongue:artnerGroup.Id and RowCause='manual' andParentId=:Trigger.new[i].Id AND AccessLevel='Read'];

                   if (oldBudgetShare.size() > 0) {

                       Delete oldBudgetShare;

                   }

               }

           }

        }

    }