You need to sign in to do that
Don't have an account?
Alexander G Goodman
Creating an If statement where the critia comes from a Picklist
I have Custom code for a trigger which I am trying to get it to only activate if a pick list field is selected. The code needsto be Triggered from a If Statement.
This is the original Code:
trigger OpportunityAutoUpdate on Opportunity (before insert,before update)
{
// get the ids of all the accounts
Set<Id> accIds=new Set<Id>();
for (Opportunity opp : trigger.new)
{
accIds.add(opp.AccountId);
}
// retrieve the accounts and owners
List<Account> accs=[select id, OwnerId from Account where id in :accIds];
Map<Id, Account> accById=new Map<Id, Account>();
accById.putall(accs);
// iterate the opportunities, updating the opportunity owner to the account owner
for (Opportunity opp : trigger.new)
{
// any logic about when to do this - i.e. only if the current owner matches a
// particular id goes here
if (null!=opp.AccountId)
{
opp.OwnerId=accById.get(opp.AccountId).OwnerId;
} }
}
Any help would be apprecated.
Note: I can not give the field name of the picklist.
This is the original Code:
trigger OpportunityAutoUpdate on Opportunity (before insert,before update)
{
// get the ids of all the accounts
Set<Id> accIds=new Set<Id>();
for (Opportunity opp : trigger.new)
{
accIds.add(opp.AccountId);
}
// retrieve the accounts and owners
List<Account> accs=[select id, OwnerId from Account where id in :accIds];
Map<Id, Account> accById=new Map<Id, Account>();
accById.putall(accs);
// iterate the opportunities, updating the opportunity owner to the account owner
for (Opportunity opp : trigger.new)
{
// any logic about when to do this - i.e. only if the current owner matches a
// particular id goes here
if (null!=opp.AccountId)
{
opp.OwnerId=accById.get(opp.AccountId).OwnerId;
} }
}
Any help would be apprecated.
Note: I can not give the field name of the picklist.
What exactly you want to do in this trigger ?
if opportunity.AccountId != null then you want to updaet the opportunity ownerId with related account ownerId is it ?
so what is the use of picklist ..
Thanks
Assume that you want to fire that cod eif the opportunity stageName have some value .
You can try like if(opp.StageName != null)
or if(opp.StageName= ='Closed Own')
I think it will help any issue let me know .
Please try below code with your Opportunity field.
trigger OpportunityAutoUpdate on Opportunity (before insert,before update)
{
// get the ids of all the accounts
Set<Id> accIds=new Set<Id>();
for (Opportunity opp : trigger.new)
{
if(opp.stage =='Closed Won'){
accIds.add(opp.AccountId);
}
}
// retrieve the accounts and owners
List<Account> accs=[select id, OwnerId from Account where id in :accIds];
Map<Id, Account> accById=new Map<Id, Account>();
accById.putall(accs);
// iterate the opportunities, updating the opportunity owner to the account owner
for (Opportunity opp : trigger.new)
{
//I would like this trigger to only work when a certain picklist selection is selected and then when the account owner is changed, the opportunity owners are not chaged with it.
if(opp.stage =='Closed Won' && opp.OwnerId != accById.get(opp.AccountId).OwnerId){
opp.OwnerId=accById.get(opp.AccountId).OwnerId;
}
}
}
trigger OpportunityAutoUpdate on Opportunity (before insert,before update)
{
// get the ids of all the accounts
Set<Id> accIds=new Set<Id>();
for (Opportunity opp : trigger.new)
{
accIds.add(opp.AccountId);
}
// retrieve the accounts and owners
List<Account> accs=[select id, OwnerId from Account where id in :accIds];
Map<Id, Account> accById=new Map<Id, Account>();
accById.putall(accs);
// iterate the opportunities, updating the opportunity owner to the account owner
for (Opportunity opp : trigger.new)
{
// any logic about when to do this - i.e. only if the current owner matches a
// particular id goes here
if (opp.****_*****__c == '****')
{
opp.OwnerId=accById.get(opp.AccountId).OwnerId;
} }
}
What is exacxt error that you are getting ? Can you paste screenshot ?
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger OpportunityAutoUpdate caused an unexpected exception, contact your administrator: OpportunityAutoUpdate: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OpportunityAutoUpdate: line 24, column 1
// Filter opportunity here which meets your criteria on the very second block of your code.
So you will have all accounts in map where related opportunity field value has changed.
for (Opportunity opp : trigger.new)
{
if( opp.stage =='Closed Won'){
accIds.add(opp.AccountId);
}
}
And yes ,you can transfer trigger logic to a utility class which can be triggered from process builder.