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
NHameedNHameed 

Prevent Edit on Opportunity Team

Hello
New requirement need to be added to Trigger and Test Class. We want to prevent any edits to Fields: "TeamMemberRole" and "OpportunityAccessLevel" when "Opportunity_Team_Locked__c" on Opportunity is checked.
The only User that should be able to modify these two fields should be Commission Admin with "commission_Admin__c" cheked on User record. 
TIA

Trigger: 
trigger OpptyTeamLock on OpportunityTeamMember(before insert, before delete) {
    set<Id> oppIdSet = new Set<id>();
    if (Trigger.IsBefore) {
        if (Trigger.IsInsert) {
            for (OpportunityTeamMember otm : Trigger.new) {
                oppIdSet.add(otm.OpportunityId);
            }
        }
        if (Trigger.IsDelete) {
            for (OpportunityTeamMember otm : Trigger.old) {
                oppIdSet.add(otm.OpportunityId);
            }
        }
    }
    // Id userid=UserInfo.getUserId();
    List<user> userinformation = [
        SELECT id, commission_Admin__c
        FROM user
        WHERE id = :UserInfo.getUserId()
    ];
    list<Opportunity> lstOpp = [
        SELECT Id, Name, Opportunity_Team_Locked__c
        FROM opportunity
        WHERE Id IN :oppIdSet AND Opportunity_Team_Locked__c = TRUE
    ];
    if (Trigger.IsBefore) {
        if (Trigger.IsInsert) {
            for (OpportunityTeamMember otm : Trigger.new) {
                if (lstOpp.Size() > 0 && userinformation[0].commission_Admin__c == false) {
                    otm.AddError('Opportunity Team is locked. Please contact Commissions Team');
                }
            }
        }
        if (Trigger.IsDelete) {
            for (OpportunityTeamMember otm : Trigger.old) {
                if (lstOpp.Size() > 0 && userinformation[0].commission_Admin__c == false) {
                    otm.AddError('Opportunity Team is locked. Please contact Commissions Team');
                }
            }
        }
    }
}

Test Class: 
@isTest
public class OpptyTeamLock_Test {
    public static testMethod void OpptyTeamTest() {
        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 = 'standardusedasdasdsaadr@testorg.com'
        );
        insert u;
        Opportunity Opp = new Opportunity(
            Name = 'TestOTMOpportunity',
            StageName = 'Prospecting',
            Type = 'New Customer',
            CloseDate = date.today(),
            Opportunity_Team_Locked__c = true
        );
        insert Opp;
        OpportunityTeamMember opm = new OpportunityTeamMember(
            TeamMemberRole = 'TSE Owner',
            OpportunityId = opp.id,
            UserId = u.id
        );
        try {
            insert opm;
        } catch (Exception e) {
            System.assert(
                e.getMessage().contains('Opportunity Team is locked. Please contact Commissions Team')
            );
        }
    }

    public static testMethod void OpptyTeamTest1() {
        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 = 'standardusedasdasdsaadr@testorg.com'
        );
        insert u;
        Opportunity Opp = new Opportunity(
            Name = 'TestOTMOpportunity',
            StageName = 'Prospecting',
            Type = 'New Customer',
            CloseDate = date.today()
        );
        insert Opp;
        OpportunityTeamMember opm = new OpportunityTeamMember(
            TeamMemberRole = 'TSE Owner',
            OpportunityId = opp.id,
            UserId = u.id
        );
        try {
            insert opm;
        } catch (Exception e) {
            System.assert(
                e.getMessage().contains('Opportunity Team is locked. Please contact Commissions Team')
            );
        }
        opp.Opportunity_Team_Locked__c = true;

        try {
            delete opm;
        } catch (Exception e) {
            System.assert(
                e.getMessage().contains('Opportunity Team is locked. Please contact Commission Team')
            );
        }
    }
}

 
CharuDuttCharuDutt
Hii
Try Belw Code
@isTest
public class OpptyTeamLock_Test {
    public static testMethod void OpptyTeamTest() {
        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 = 'standardusedasdasdsaadr@testorg.com',commission_Admin__c = true
        );
        insert u;
        Opportunity Opp = new Opportunity(
            Name = 'TestOTMOpportunity',
            StageName = 'Prospecting',
            Type = 'New Customer',
            CloseDate = date.today(),
            Opportunity_Team_Locked__c = true
        );
        insert Opp;
        OpportunityTeamMember opm = new OpportunityTeamMember(
            TeamMemberRole = 'TSE Owner',
            OpportunityId = opp.id,
            UserId = u.id
        );
        try {
            insert opm;
        } catch (Exception e) {
            System.assert(
                e.getMessage().contains('Opportunity Team is locked. Please contact Commissions Team')
            );
        }
    }

    public static testMethod void OpptyTeamTest1() {
        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 = 'standardusedasdasdsaadr@testorg.com',commission_Admin__c = true
        );
        insert u;
        Opportunity Opp = new Opportunity(
            Name = 'TestOTMOpportunity',
            StageName = 'Prospecting',
            Type = 'New Customer',
            CloseDate = date.today()
        );
        insert Opp;
        OpportunityTeamMember opm = new OpportunityTeamMember(
            TeamMemberRole = 'TSE Owner',
            OpportunityId = opp.id,
            UserId = u.id
        );
        try {
            insert opm;
        } catch (Exception e) {
            System.assert(
                e.getMessage().contains('Opportunity Team is locked. Please contact Commissions Team')
            );
        }
        opp.Opportunity_Team_Locked__c = true;

        try {
            delete opm;
        } catch (Exception e) {
            System.assert(
                e.getMessage().contains('Opportunity Team is locked. Please contact Commission Team')
            );
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
NHameedNHameed
Hi Charu I need help with Trigger as well not just test class. Thanks