You need to sign in to do that
Don't have an account?
Pappu
Test class for Apex code
Hi Guys,
Anyone help me with test class for this trigger?
trigger TRG_Opportunity_AddAcctOwner on Opportunity (after insert, after update) {
List<OpportunityTeamMember> opptyTeam = new List<OpportunityTeamMember>();
List<Opportunity> opptyInfo = new List<Opportunity>();
List<Account> currAcctOwnerID = new List<Account>();
Integer teamFlag = 0;
Set<Id> setOpptyID = new Set<Id>();
Set<Id> setAcctID = new Set<Id>();
Set<Id> setOwnerUserID = new Set<Id>();
for (Opportunity opp: Trigger.new){
setOpptyID.add(opp.Id);
setAcctID.add(opp.AccountId);
}
currAcctOwnerID = [Select a.OwnerId From Account a where a.Id in: setAcctID];
opptyInfo = [Select opp.Id, opp.OwnerId, opp.StageName From Opportunity opp where opp.Id in: setOpptyID];
opptyTeam = [Select o.UserId, o.OpportunityId From OpportunityTeamMember o where o.OpportunityId in: setOpptyID];
for (OpportunityTeamMember otm: opptyTeam) {
Integer index = 0;
if (currAcctOwnerID[0].OwnerId == opptyTeam[index].UserId){
teamFlag += 1;
}
index +=1;
}
if (currAcctOwnerID.size() > 0 && opptyInfo[0].StageName != 'Closed Lost' && currAcctOwnerID[0].OwnerId != 'xxxxxxxxx')
{
User acctOwner = new User();
acctOwner = [Select IsActive From User where Id = :currAcctOwnerID[0].OwnerId];
if (acctOwner.IsActive == true){
OpportunityTeamMember otmAcctOwner = new OpportunityTeamMember();
if (teamFlag < 1 && opptyTeam.size() > 0 && opptyInfo[0].OwnerId != currAcctOwnerID[0].OwnerId) {
// Add the Account Owner to the Sales Team
otmAcctOwner.OpportunityId = opptyTeam[0].OpportunityId;
otmAcctOwner.UserId = currAcctOwnerID[0].OwnerId;
otmAcctOwner.TeamMemberRole = 'Account Owner';
insert otmAcctOwner;
}
if (opptyTeam.size() == 0 && opptyInfo[0].OwnerId != currAcctOwnerID[0].OwnerId) {
otmAcctOwner.OpportunityId = opptyInfo[0].Id;
otmAcctOwner.UserId = currAcctOwnerID[0].OwnerId;
otmAcctOwner.TeamMemberRole = 'Account Owner';
insert otmAcctOwner;
}
List<OpportunityShare> oppShares = new List<OpportunityShare>();
oppShares = [Select o.UserOrGroupId, o.OpportunityId, o.OpportunityAccessLevel From OpportunityShare o where o.OpportunityId = :otmAcctOwner.OpportunityId and RowCause = 'Team'];
for (OpportunityShare share : oppShares)
if (share.UserOrGroupID == otmAcctOwner.UserId) {
share.OpportunityAccessLevel = 'Edit';
}
update oppShares;
}
}
}
Anyone help me with test class for this trigger?
trigger TRG_Opportunity_AddAcctOwner on Opportunity (after insert, after update) {
List<OpportunityTeamMember> opptyTeam = new List<OpportunityTeamMember>();
List<Opportunity> opptyInfo = new List<Opportunity>();
List<Account> currAcctOwnerID = new List<Account>();
Integer teamFlag = 0;
Set<Id> setOpptyID = new Set<Id>();
Set<Id> setAcctID = new Set<Id>();
Set<Id> setOwnerUserID = new Set<Id>();
for (Opportunity opp: Trigger.new){
setOpptyID.add(opp.Id);
setAcctID.add(opp.AccountId);
}
currAcctOwnerID = [Select a.OwnerId From Account a where a.Id in: setAcctID];
opptyInfo = [Select opp.Id, opp.OwnerId, opp.StageName From Opportunity opp where opp.Id in: setOpptyID];
opptyTeam = [Select o.UserId, o.OpportunityId From OpportunityTeamMember o where o.OpportunityId in: setOpptyID];
for (OpportunityTeamMember otm: opptyTeam) {
Integer index = 0;
if (currAcctOwnerID[0].OwnerId == opptyTeam[index].UserId){
teamFlag += 1;
}
index +=1;
}
if (currAcctOwnerID.size() > 0 && opptyInfo[0].StageName != 'Closed Lost' && currAcctOwnerID[0].OwnerId != 'xxxxxxxxx')
{
User acctOwner = new User();
acctOwner = [Select IsActive From User where Id = :currAcctOwnerID[0].OwnerId];
if (acctOwner.IsActive == true){
OpportunityTeamMember otmAcctOwner = new OpportunityTeamMember();
if (teamFlag < 1 && opptyTeam.size() > 0 && opptyInfo[0].OwnerId != currAcctOwnerID[0].OwnerId) {
// Add the Account Owner to the Sales Team
otmAcctOwner.OpportunityId = opptyTeam[0].OpportunityId;
otmAcctOwner.UserId = currAcctOwnerID[0].OwnerId;
otmAcctOwner.TeamMemberRole = 'Account Owner';
insert otmAcctOwner;
}
if (opptyTeam.size() == 0 && opptyInfo[0].OwnerId != currAcctOwnerID[0].OwnerId) {
otmAcctOwner.OpportunityId = opptyInfo[0].Id;
otmAcctOwner.UserId = currAcctOwnerID[0].OwnerId;
otmAcctOwner.TeamMemberRole = 'Account Owner';
insert otmAcctOwner;
}
List<OpportunityShare> oppShares = new List<OpportunityShare>();
oppShares = [Select o.UserOrGroupId, o.OpportunityId, o.OpportunityAccessLevel From OpportunityShare o where o.OpportunityId = :otmAcctOwner.OpportunityId and RowCause = 'Team'];
for (OpportunityShare share : oppShares)
if (share.UserOrGroupID == otmAcctOwner.UserId) {
share.OpportunityAccessLevel = 'Edit';
}
update oppShares;
}
}
}