0

In Account Object, I have created a field salesperson and it has a lookup relation with the user object. If the user is selected as the salesperson for a record, that user able to see those records, but OWD is private for Account

Could someone please help to achieve the above scenario?

1

You can't achieve this with a Sharing Rule of any kind, because the target of the sharing is dynamic on a per-record basis.

Before embarking on the only available solution, which is Apex-managed manual sharing, you may want to evaluate whether simply using the out-of-the-box Account Teams functionality can meet your needs. The salesperson could simply be on the Account Team with the right level of access.

If you absolutely must implement this objective in this way, you'll want to read this article from Salesforce, which discusses a couple of different solutions build around using Apex to generate manual sharing records to achieve this type of dynamic sharing.

  • Thank you so much David for sharing the knowledge now as per my understanding we need a trigger to do the same, I can get the user Id from that lookup field but at the end of the trigger what should I write to provide the read write access to that ID? – gs650x Aug 1 '18 at 13:24
  • There are examples in the documentation I linked. I recommend you read it in detail. This is not a solution for the light-hearted and there are many edge cases to handle. – David Reed Aug 1 '18 at 13:38
0

I tried the below trigger on lead and it worked as expected.

trigger Leadshare12 on Lead (after insert) {
    // Map to hold the detail of each created record in form of Lead Id and User Id.
    map<id,id> MapLeadiduserid = new map<id,id>();
    List<LeadShare> leadShareList = new list<LeadShare>();
    for(lead L : trigger.new)
    {
        if(l.Sales_User__c != null)
        {
            MapLeadiduserid.put(l.id,l.Sales_User__c);
        }
    }
    // Iterate over map keyset as all new created record of lead . In map we store lead id as a key and Userid as the value of map. All newly record details are now in map.
    for(Id LID : MapLeadiduserid.keyset())
    {
        // Leadshare object need to be created to hold the share record .
        LeadShare jobShare  = new LeadShare();

        jobShare.LeadId = LID;
        // now providing as access to user to have the capabilty to see the lead .MapLeadiduserid.get(s):- here we are getting user id from the map.
        jobShare.UserOrGroupId = MapLeadiduserid.get(LID);
        jobShare.LeadAccessLevel = 'Edit';
        leadShareList.add(jobShare);

    }
    //Inserting Sharing record as per the user.
    if(!leadShareList.isempty())
    {
        insert leadShareList;
    }

}

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.