You need to sign in to do that
Don't have an account?
Gobbledigook
Trigger to update Parent picklist from Child picklist
Hey all,
I'm having a problem with what is probably a very simple solution, but I just can't wrap my head around it.
I'm trying to update an Account.Type field with a set value when an Opportunity hits a certain Stage, but only if an Account is a specific type already.
Here's the code:
trigger SetAccountToP on Opportunity (before insert, before update) { for(Opportunity o : Trigger.New){ if((o.StageName == 'Proposal/Price Quote' || o.StageName == 'Negotiation/Review' || o.StageName == 'Closed Won') && o.Account.Type == 'User') { Account a = o.account; a.Type = 'Prospect'; update a; } } }
I know this code is pretty bad, but I've tried many things and this is the easiest to read through to illustrate my point.
Any help would be appreciated.
First off, thanks for everyone's replies. They really gave me a lot of direction as to thought processes and where to go.
I figured it out. I'm posting the solution I found here just in case anyone else is has the same problem.
Thanks again for everyone's assistance.
All Answers
Hi there,
You haven't explained the nature of your problem though.
What kind of error is this code throwing?
If you are hitting governor limits, it's obviously because of the update statement inside the loop.
Or is the code not working as expected?
Regards,
A J
Hi,
I guess the problem is due to the update in the for loop.
Use a List<Account> and store the accounts that you want to update in that list and fire the update statement at the end.
i.e.
trigger SetAccountToP on Opportunity (before insert, before update) {
List<Account> lstAccToBeUpdated = new List<Account();
for(Opportunity o : Trigger.New){
if((o.StageName == 'Proposal/Price Quote' || o.StageName == 'Negotiation/Review' || o.StageName == 'Closed Won') && o.Account.Type == 'User')
{ Account a = new Account(Id=o.AccountId);
a.Type = 'Prospect';
lstAccToBeUpdated.add(a);
}
}
if(lstAccToBeUpdated != null && !lstAccToBeUpdated.isEmpty())
{
update lstAccToBeUpdated;
}
}
I'm not sure how successful you'll be in accessing fields from the related account object. I've found that following the reference normally results in all fields being null. Have you tried pulling the related accounts into the trigger via a SOQL call?
Thanks for the replies.
No I'm not hitting governer limits just yet. I understand the code itself is very sloppy, but it just was to illustrate the nature of the problem. It's just not working at all.
I've also tried pulling in the related account information in a SOQL call itself, but the field still doesn't update. Is it because it's a picklist value by any chance? (Yeah, I'm grasping at straws here)
Here is the updated code:
Ok, I got it to work for single processing. Any ideas of how I could do this in bulk?
(Sorry about the code being drastically altered between posts)
Hi,
As adviced by ipsita.biswas@in.v2solutions.com. Something like bellow.
Not sure that one works but it should give you an idea on how to build it.
Cheers,
Fred
First off, thanks for everyone's replies. They really gave me a lot of direction as to thought processes and where to go.
I figured it out. I'm posting the solution I found here just in case anyone else is has the same problem.
Thanks again for everyone's assistance.
Hi,
I am new to salesforce and i have to write trigger on custom object"Prod" and they purpose of that trigger is that when user enter data in field then its should update two more fields upon saving the record.
here is the example:
custom object = Prod
field 1 = field (this is text field)
field 2 = pick list field(option that should be populate is App)
field 3 = field(this text field)
if user enter in field 1 then its should poulate the field 2 = app and update the same value in field 3 = field 1.
please help me
thanks
Hi there,
This is something easier to achieve and to mainaint with workflow field updates as it is quite basic.
1 workflow to check if the field1 has been filled in and 2 field updates to give the correct value on both other fields (one for each of the 2 other fields)
If you want to do it trigger based anyway best way to achieve this would be to use a before insert/update trigger, loop trough your trigger.new and based on the field 1 value populate field 2 and 3. Since you are in a before context no DML statement is used as it populates the fields on the fly before actualy commiting to the database.
Something like this (didn't try to but should compile fine)
Cheers,
Fred