Create README.md


//We need this trigger to share the corresponding OLIs of the Opportunity visible

//this has a dependency to run the Dashboards.

trigger ShareOLIWithAccountSalesLead on Account (after insert, after update)

{

    set<Id> SalesUnitIdSet = new set<id>();

    Map<Id,Account> AccountsToProcess = new Map<Id,Account>();

    for(Account TempAccountObj : Trigger.new)

    {

        if(TempAccountObj.Sales_Unit__c != null)

        {

            SalesUnitIdSet.add(TempAccountObj.Sales_Unit__c);

            AccountsToProcess.put(TempAccountObj.Id,TempAccountObj);

        }

    }

    if(SalesUnitIdSet.size()>0)

    {

    Map<Id,Sales_Unit__c> SalesUnitMap = new Map<Id,Sales_Unit__c>([select id,name,Sales_Leader__c from Sales_Unit__c where Id in :SalesUnitIdSet]);

    list<Opportunity> LstOfAllOpportunityForAccount = [select id,name,AccountId,ownerId,(select id,name,ownerId from Opportunity_Products__r) from Opportunity where AccountId in : AccountsToProcess.keyset()];

    Map<Id,list<OpportunityProduct__c>> LstOfOLIOfAccountMap = new Map<Id,list<OpportunityProduct__c>>();

    if(LstOfAllOpportunityForAccount.size()>0)

    {

        for(Opportunity TempOppObj : LstOfAllOpportunityForAccount)

        {

               if(LstOfOLIOfAccountMap .containskey(TempOppObj.AccountId))

               {

                   list<OpportunityProduct__c> TempOpportunityProductLst = new list<OpportunityProduct__c>();

                   TempOpportunityProductLst.addall(LstOfOLIOfAccountMap.get(TempOppObj.AccountId));

                   TempOpportunityProductLst.addall(TempOppObj.Opportunity_Products__r);

                   LstOfOLIOfAccountMap.put(TempOppObj.AccountId,TempOpportunityProductLst);

               }

               else

               {

                   LstOfOLIOfAccountMap.put(TempOppObj.AccountId,TempOppObj.Opportunity_Products__r);

               }

        }

        if(LstOfOLIOfAccountMap.size()>0)

        {

            list<OpportunityProduct__Share> OpportunityProductShareToInsert = new list<OpportunityProduct__Share>();

            for(Id AccountIdKey : LstOfOLIOfAccountMap.keyset())

            {

                for(OpportunityProduct__c TempOppProductObj : LstOfOLIOfAccountMap.get(AccountIdKey))

                {

                    if(AccountsToProcess.containskey(AccountIdKey))

                    {

                        if(SalesUnitMap.get(AccountsToProcess.get(AccountIdKey).Sales_Unit__c).Sales_Leader__c != TempOppProductObj.ownerId)

                        {

                            OpportunityProduct__Share AccountSalesLeadOLIShr  = new OpportunityProduct__Share();

                            AccountSalesLeadOLIShr.ParentId = TempOppProductObj.id;

                            AccountSalesLeadOLIShr.UserOrGroupId = SalesUnitMap.get(AccountsToProcess.get(AccountIdKey ).Sales_Unit__c).Sales_Leader__c;

                            AccountSalesLeadOLIShr.AccessLevel = 'Read';

                            OpportunityProductShareToInsert.add(AccountSalesLeadOLIShr);

                        }

                    }

                }

            }

            if(OpportunityProductShareToInsert.size()>0)

                insert OpportunityProductShareToInsert;

        }

       

        

    }

    }

   

}