You need to sign in to do that
Don't have an account?
Michael Covert
SOQL query for OpportunityTeamMember returning zero rows
Hello,
I'm having an issue with an Opportunity trigger that I'm writing, the trigger will change a User lookup field called ResponsibleParty to someone on the Opportunity Team when the stage changes. This trigger does work in the dev org, but I can't get it to work with a test class. Below is the Trigger and test class, it is failing when I perform the SOQL query on OpportunityTeamMember, it will return zero rows. Also I can take the test class code and put it right into the execute Annonymous Code windows and it all works.
FYI I'm fairly new to APEX and I'm working on the Bulkifing.
Trigger -----
trigger Responsible_Party_Change on Opportunity (after update) {
for (Opportunity opp : Trigger.new) {
Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
//Getting the UserID for Opp Team Member with "Partner" Role
OpportunityTeamMember partner = [SELECT UserId
FROM OpportunityTeamMember
WHERE OpportunityId = :opp.Id
AND TeamMemberRole = 'Partner'
LIMIT 1];
//Getting the UserID for Opp Team Member with "Setup" Role
OpportunityTeamMember setup = [SELECT UserId
FROM OpportunityTeamMember
WHERE OpportunityId = :opp.Id
AND TeamMemberRole = 'Setup'
LIMIT 1];
//Getting the UserID for Opp Team Member with "Processor" Role
OpportunityTeamMember processor = [SELECT UserId
FROM OpportunityTeamMember
WHERE OpportunityId = :opp.Id
AND TeamMemberRole = 'Processor'
LIMIT 1];
//IF the Stage is Partner then set Responsible Party to Partner
if (!oldOpp.StageName.equals('Partner') && opp.StageName.equals('Partner'))
{
opp.Responsible_Party__c = partner.UserId;
}
}
}
Test Class -----
@isTest
private class ResponsiblePartyChangeTest {
@isTest static void StageChangeTest() {
Opportunity opp = new Opportunity();
opp.CloseDate = Date.Today();
opp.OwnerId = UserInfo.getUserId();
opp.Name = 'Test Opp';
opp.StageName = 'Officer';
Insert(opp);
opp.StageName = 'Partner';
Update(opp);
}
}
Any help would be greatly Appreciated!!
I'm having an issue with an Opportunity trigger that I'm writing, the trigger will change a User lookup field called ResponsibleParty to someone on the Opportunity Team when the stage changes. This trigger does work in the dev org, but I can't get it to work with a test class. Below is the Trigger and test class, it is failing when I perform the SOQL query on OpportunityTeamMember, it will return zero rows. Also I can take the test class code and put it right into the execute Annonymous Code windows and it all works.
FYI I'm fairly new to APEX and I'm working on the Bulkifing.
Trigger -----
trigger Responsible_Party_Change on Opportunity (after update) {
for (Opportunity opp : Trigger.new) {
Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
//Getting the UserID for Opp Team Member with "Partner" Role
OpportunityTeamMember partner = [SELECT UserId
FROM OpportunityTeamMember
WHERE OpportunityId = :opp.Id
AND TeamMemberRole = 'Partner'
LIMIT 1];
//Getting the UserID for Opp Team Member with "Setup" Role
OpportunityTeamMember setup = [SELECT UserId
FROM OpportunityTeamMember
WHERE OpportunityId = :opp.Id
AND TeamMemberRole = 'Setup'
LIMIT 1];
//Getting the UserID for Opp Team Member with "Processor" Role
OpportunityTeamMember processor = [SELECT UserId
FROM OpportunityTeamMember
WHERE OpportunityId = :opp.Id
AND TeamMemberRole = 'Processor'
LIMIT 1];
//IF the Stage is Partner then set Responsible Party to Partner
if (!oldOpp.StageName.equals('Partner') && opp.StageName.equals('Partner'))
{
opp.Responsible_Party__c = partner.UserId;
}
}
}
Test Class -----
@isTest
private class ResponsiblePartyChangeTest {
@isTest static void StageChangeTest() {
Opportunity opp = new Opportunity();
opp.CloseDate = Date.Today();
opp.OwnerId = UserInfo.getUserId();
opp.Name = 'Test Opp';
opp.StageName = 'Officer';
Insert(opp);
opp.StageName = 'Partner';
Update(opp);
}
}
Any help would be greatly Appreciated!!
Please follow below Trigger Best Practice
1) NO SOQL inside the loop
2) NO DML inside the loop
3) Trigger should be on before update
Try to update your code like below
Your test class should be like below
Let us know if this will help you
All Answers
Please follow below Trigger Best Practice
1) NO SOQL inside the loop
2) NO DML inside the loop
3) Trigger should be on before update
Try to update your code like below
Your test class should be like below
Let us know if this will help you