You need to sign in to do that
Don't have an account?
NHameed
Prevent Edit on Opportunity Team need help with Trigger and Test Class Please
Hello
New requirement needs to be added to Trigger and Test Class.
We want to prevent any edits to Opportunity Team 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.
Currently, this trigger will prevent addition and deletion of Team Members but now we need to prevents any edits to eexiting Team Members as well.
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:
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')
);
}
}
}
New requirement needs to be added to Trigger and Test Class.
We want to prevent any edits to Opportunity Team 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.
Currently, this trigger will prevent addition and deletion of Team Members but now we need to prevents any edits to eexiting Team Members as well.
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:
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')
);
}
}
}
Hey,
I would suggest, if the requirement is to prevent the editing of record then kinldy use validation rule.
Regards,
Priya Ranjan
I had a validation rule to prevent edit but the validation on Opportunity Team cannot have Opportunity level Check Box "Opportunity_Team_Locked__c" as the criteria. I would like users to only check one box on Opportunity to lock the Team. Looks like if I create Validation Rule on Opportunity Team they need to check a box on the Team it self.
Thanks