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
SFDCDevQASFDCDevQA 

Trigger not limiting by Record Type

I'm trying to limit a previously working trigger by the RecordType Name.  I have tried adding my limiter to both the beginning For and the latter For and it doesn't seem to make a difference.  I've tried using both 'High School' and High_School' but that makes no difference.  If I say != then the trigger always fires and if I say == then the trigger never fires.  I must be doing something simple wrong but I just can't figure out what it is.  Any help would be greatly appreciated.

 

Thanks

Amanda

 

trigger OpportunityOwnerUpdate on Opportunity (before insert,before update) {
// gather all the Account Id's in a set

Set<Id> setAccountIds = new Set<Id>();

Opportunity[] opp = Trigger.new;
for (Opportunity o:opp)
{
if(o.Recordtype.Name != 'High School')

{
setAccountIds.add(o.AccountId);

}}

if( setAccountIds.size() > 0 ) // if there are no eligible Quote records, end
{
// store each Account Id with it's owner Id in a map, such that AccountId => Account.OwnerId

Map<Id, Id> mapAccountToOwner = new Map<Id, Id>();

for(Account a : [Select Id, OwnerId From Account Where Id IN : setAccountIds])

{

mapAccountToOwner.put(a.Id, a.OwnerId);
}
for (Opportunity o:opp)
{
if(o.AccountId !=null)
{
o.OwnerID = mapAccountToOwner.get(o.AccountId); // here you are avoiding the query
}
}
}}

Best Answer chosen by Admin (Salesforce Developers) 
vbsvbs

Amanda - I think the reason for your trigger not working is the use of dot notation to access relationship field in the before  context. The line 'o.RecordType.Name' will not have any value in the before context hence you will have to get the Id of the recordtype initially and then use this in the comparison. Try this and see if this helps:

trigger OpportunityOwnerUpdate on Opportunity (before insert,before update) {
// gather all the Account Id's in a set
Set<Id> setAccountIds = new Set<Id>();
Opportunity[] opp = Trigger.new;
Id recType = [select Id from RecordType where Name = 'High School' Limit 1].Id;
for (Opportunity o : opp)
{
if(o.Recordtype != recType)

{
setAccountIds.add(o.AccountId);
}}
if( setAccountIds.size() > 0 ) // if there are no eligible Quote records, end
{
// store each Account Id with it's owner Id in a map, such that AccountId => Account.OwnerId
Map<Id, Id> mapAccountToOwner = new Map<Id, Id>();
for(Account a : [Select Id, OwnerId From Account Where Id IN : setAccountIds])
{
mapAccountToOwner.put(a.Id, a.OwnerId);
}
for (Opportunity o:opp)
{
if(o.AccountId !=null)
{
o.OwnerID = mapAccountToOwner.get(o.AccountId); // here you are avoiding the query
}
}
}}

 

 

All Answers

vbsvbs

Amanda - I think the reason for your trigger not working is the use of dot notation to access relationship field in the before  context. The line 'o.RecordType.Name' will not have any value in the before context hence you will have to get the Id of the recordtype initially and then use this in the comparison. Try this and see if this helps:

trigger OpportunityOwnerUpdate on Opportunity (before insert,before update) {
// gather all the Account Id's in a set
Set<Id> setAccountIds = new Set<Id>();
Opportunity[] opp = Trigger.new;
Id recType = [select Id from RecordType where Name = 'High School' Limit 1].Id;
for (Opportunity o : opp)
{
if(o.Recordtype != recType)

{
setAccountIds.add(o.AccountId);
}}
if( setAccountIds.size() > 0 ) // if there are no eligible Quote records, end
{
// store each Account Id with it's owner Id in a map, such that AccountId => Account.OwnerId
Map<Id, Id> mapAccountToOwner = new Map<Id, Id>();
for(Account a : [Select Id, OwnerId From Account Where Id IN : setAccountIds])
{
mapAccountToOwner.put(a.Id, a.OwnerId);
}
for (Opportunity o:opp)
{
if(o.AccountId !=null)
{
o.OwnerID = mapAccountToOwner.get(o.AccountId); // here you are avoiding the query
}
}
}}

 

 

This was selected as the best answer
SFDCDevQASFDCDevQA

That was the issue, thanks so much!