function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC Noob 1SFDC Noob 1 

Sharing opportunities with Partner

I have a partner__c user lookup field on Opportunity. I created a trigger that gives View access to the opportunities to the User that is in the Partner__c field, but this only works on the new opportunities being created.

I have thousands of Opps that don't have a populated Partner__c yet and some that alread did. How can i give those users View access once i get the Parter__c populated and to the older opportunities?
ClintLeeClintLee
It sounds like your trigger is an insert trigger which is why it only creates the access on new opportunities.  Is that correct?

Creating an after-update trigger would allow the following:

1.  Give access to a new partner if the Partner__c field changes.
2.  Give you a way to manually invoke the process of granting access to the opportunity.

Create a new checkbox field on the Opportunity called Grant Partner Access (Grant_Partner_Access__c).  Then, in the update trigger check to see if either the Partner__c field was changed or if the Grant Partner Access field was just checked.  
 
trigger grantPartnerAccess on Opportunity (after update)
{
    for(Opportunity newOpp : Trigger.new.values())
    {
        Opportunity oldOpp = Trigger.old.get(newOpp.Id);

        if((newOpp.Partner__c != oldOpp.Partner__c) || 
            (opp.Grant_Partner_Access__c && !oldOpp.Grant_Partner_Access__c))
        {
            // add your code to grant visibility to the opportunity.
        }
    }
}

Now, when an opportunity is updated, visibility will be granted to the Partner if either the partner was just changed or if the Grant Partner Access field was just checked.  From here you can load the Partner__c field on all of your old opportunities and then do a mass update on all the opportunities to set the Grant_Partner_Access__c field to TRUE.

* On a side note - I don't suggest having multiple triggers on the same object but that's beyond the scope of this answer.  You can read more about that here https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices.  However, the logic is valid however you implement the trigger.

Hope that helps,

Clint