You need to sign in to do that
Don't have an account?
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
}
}
}}
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:
All Answers
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:
That was the issue, thanks so much!