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
juhi dhiverjuhi dhiver 

Read Access for Opportunity!!!!!!

So my Opportunity are private and there is secondary owner field(lookup user) in Account. I want that my secondary owner can also view that particular Accounts  opportunities.

trigger Accountshare on Account (After insert, after update) {


List<AccountShare> sharesToCreate = new List<AccountShare>();
User us = new User();

    if (Trigger.isInsert) {
        set<ID> accwnerBranch=new set<Id>();
        For(Account acc:[SELECT Id,Owner.Branch__c FROM Account WHERE ID IN:Trigger.New]){
            accwnerBranch.add(acc.Owner.Branch__c);
            system.debug('The Branch owner Value is'+accwnerBranch);
        }
     
        for (Account ac : Trigger.new) {
            system.debug('The Owner Branch is'+ac.Owner.Branch__c);
                    IF(ac.Owner.Branch__c =='HYDERABAD'){ 
                        system.debug('The Owner Branch is'+ac.Owner.Branch__c);
                        AccountShare cs = new AccountShare();
                        cs.AccountAccessLevel = 'Read';
                        cs.OpportunityAccessLevel='Edit';                
                        cs.AccountId = ac.Id;
                        cs.UserOrGroupId = us.Id;
                        sharesToCreate.add(cs);    
                        
                    }
                    If(ac.Branch__c =='PUNE'){
                        AccountShare cs = new AccountShare();
                        cs.AccountAccessLevel = 'Read';
                        cs.OpportunityAccessLevel='Edit';                
                        cs.AccountId = ac.Id;
                        cs.UserOrGroupId = us.Id;
                        sharesToCreate.add(cs);         
                    }
                    
                    if (ac.Secondary_Owner__c != null) {
                        // create the new share for group
                        AccountShare cs = new AccountShare();
                        cs.AccountAccessLevel = 'Read';
                        cs.OpportunityAccessLevel='Edit';                
                        cs.AccountId = ac.Id;
                        cs.UserOrGroupId =  us.Id;
                        sharesToCreate.add(cs);
                    }
Need Suggestion...What can be done for this .
Here this trigger is not working .
Any other solution for this ???
Anupama SamantroyAnupama Samantroy
Hi 

Please try the below code. In your code I think Branch__c is a lookup to branch object from Account. Assuming that please find below code. Please let me know if it works for you.
List<AccountShare> sharesToCreate = new List<AccountShare>();
    if (Trigger.isInsert) {        
        for(Account acc:[SELECT Id,Owner.Branch__r.Name, Secondary_Owner__c FROM Account WHERE ID IN:Trigger.New]){
             system.debug('The Owner Branch is'+ac.Owner.Branch__c);
                    IF((ac.Owner.Branch__r.Name =='HYDERABAD' || ac.Branch__r.Name =='PUNE' )AND (Secondary_Owner__c !=null)){ 
                        system.debug('The Owner Branch is'+ac.Owner.Branch__c);
                        AccountShare cs = new AccountShare();
                        cs.AccountAccessLevel = 'Read';
                        cs.OpportunityAccessLevel='Edit';                
                        cs.AccountId = ac.Id;
                        cs.UserOrGroupId = ac.Secondary_Owner__c;
                        sharesToCreate.add(cs);    
                        
                    }
        }     
        
		if(!sharesToCreate.isEmpty()){
			insert sharesToCreate;
	}


Thanks
Anupama
Anupama SamantroyAnupama Samantroy
Please let me know if you need any further help.

Thanks
Anupama