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
Tatiana Cooke 9Tatiana Cooke 9 

Problem with trigger to give read write access to opportunity team member

Team, 

I thought this trigger was working and I dont know how it is not now. When the secondary owner lookup field is populated on the opportunity I need it to create an opportunity with read write access. 

it is only giving the users Read Only access. 
Appreciate any help
trigger OTOpportunityTrigger on Opportunity (before update) {
    List<OpportunityTeamMember> listOpptyTeamMem = new List<OpportunityTeamMember>();
    Set<Id> OpptyIdSet  =  new Set<Id>();
    
    for(Opportunity oppty : trigger.New)
    {
        //Checking Oppty SecondaryOwner 
        if(oppty.Secondary_Owner__c != null)
        {
            OpportunityTeamMember OTM = new OpportunityTeamMember();
            OTM.OpportunityId = oppty.Id;
            OTM.TeamMemberRole = 'Secondary Owner'; 
            OTM.UserId = oppty.Secondary_Owner__c;
            listOpptyTeamMem.add(OTM);
        } 
    }
        
       
        if(listOpptyTeamMem.size() > 0)
        {
        insert listOpptyTeamMem;
                
        // get all of the team members' sharing recordsPost to Community
        List<OpportunityShare> shares = [select Id, OpportunityAccessLevel, 
          RowCause from OpportunityShare where OpportunityId IN : OpptyIdSet
          and RowCause = 'Team'];
 
        // set all team members access to read/write
        for (OpportunityShare share : shares) 
          share.OpportunityAccessLevel = 'Edit';
 
        update shares;
        }

}
Shikha AgashiShikha Agashi
Hi Tatiana,

You are not adding any Id in OpptyIdSet. So, when you are query at OpportunityShare, you are getting 0 list. 
Shikha AgashiShikha Agashi
After line 14 add, OpptyIdSet.add(Oppty.Id);
Tatiana Cooke 9Tatiana Cooke 9
Thanks Shikha!

The following error is related to the test class associated. I am gettin INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY on insert O;

Please help!
 
@isTest(SeeAllData=true)
public with sharing class TestOTOpportunityTrigger {

    public static testmethod void test_1() {
             
            //create test account
            Account a  = new Account();
            a.name     = 'Teston';
            insert a;

            //create test opportunity 
            
        Opportunity o  = new Opportunity();
        o.OwnerID = '005i0000004LFZH';
        o.Name = 'Test';
        o.AccountID = '001i000001mCGvY';
        o.Unit_Number__c = 'a01i000000YWLGK';
        o.Financing_Type__c = 'Cash Buyer';
        o.Nature_of_Tenancy__c = 'Severalty';
        o.Sale_Type__c = 'Founder';
        o.CloseDate = Date.today();
        o.stageName = 'S1: Ready to Contract';
        o.Secondary_Owner__c = '005i0000004LFZH';
        
        
        insert o;
        
        OpportunityTeamMember OTM = new OpportunityTeamMember();
        OTM.OpportunityId = o.Id;
        OTM.TeamMemberRole = 'Secondary Owner'; 
        OTM.UserId = o.Secondary_Owner__c;   
        

            
            insert OTM;
             
             
            List<OpportunityTeamMember> OTMs = [SELECT id, UserId, OPPORTUNITYACCESSLEVEL
                                                FROM OpportunityTeamMember
                                                WHERE OpportunityId =: otm.Id
                                                ];
         
             
             
            //update O to execute the trigger
            update otm;
             
            //re-query
            OTMs = [SELECT id, UserId, OPPORTUNITYACCESSLEVEL
                    FROM OpportunityTeamMember
                    WHERE OpportunityId =: otm.Id
                    ];
             

        }

    }

 
Shikha AgashiShikha Agashi
Hi Tatiana,

First please do not use hardcoded record Id. if you move them to different region, this will always error out. Like o.Accountid=a.id; as you already created account. Same ways please create two user data one for each o.OwnerId & o.Secondary_Owner__c. Create data for Unit_Number__c and use those Id's from test data. 
Tatiana Cooke 9Tatiana Cooke 9
Team,

I created the following in order to mittigate that.  But now I get the error, "invalid u.id"!
 
public with sharing class TestOTOpportunityTrigger {

    public static testmethod void test_1() {
             
            //Create test space
            Space__C S = New Space__C();
            s.Name = 'Test';
             
             
            //create test account
            Account a  = new Account();
            a.name     = 'Teston';
            insert a;
 
            // Create test user
            Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
            User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standard@testorg.com');
            Insert u;
            //create test opportunity 
            
            Opportunity o  = new Opportunity();
            o.OwnerID = 'u.id';
            o.Name = 'Test';
            o.AccountID = 'a.id';
            o.Unit_Number__c = 's.id';
            o.Financing_Type__c = 'Cash Buyer';
            o.Nature_of_Tenancy__c = 'Severalty';
            o.Sale_Type__c = 'Founder';
            o.CloseDate = Date.today();
            o.stageName = 'S1: Ready to Contract';
            o.Secondary_Owner__c = 'u.id';
            
            
            insert o;
            
            OpportunityTeamMember OTM = new OpportunityTeamMember();
            OTM.OpportunityId = o.Id;
            OTM.TeamMemberRole = 'Secondary Owner'; 
            OTM.UserId = o.Secondary_Owner__c;   
            
    
            
            insert OTM;
             
             
            List<OpportunityTeamMember> OTMs = [SELECT id, UserId, OPPORTUNITYACCESSLEVEL
                                                FROM OpportunityTeamMember
                                                WHERE OpportunityId =: otm.Id
                                                ];
         
             
             
            //update O to execute the trigger
            update otm;
             
            //re-query
            OTMs = [SELECT id, UserId, OPPORTUNITYACCESSLEVEL
                    FROM OpportunityTeamMember
                    WHERE OpportunityId =: otm.Id
                    ];
             

        }

    }

 
Shikha AgashiShikha Agashi
you do use '' comma. it should be like:

o.ownerid=u.id;
similarly:

o.accountid=a.id;