You need to sign in to do that
Don't have an account?
Garrett Miller
Update Field Between Account and Opportunity
Hi All,
I am trying to get used to writing triggers doing a fairly simple trigger but I am stuck.
I am trying to write a trigger where te scenario is:
- Two objects Account and Opportunity
- Account field Renewal Term (Renewal_Term__c)that is picklist of years annual through sexennial
- Opportunity Field Contract Length ( Contract_Length__c ) that is a picklist of values 1-6
- I believe that there is a master-detail relationship between Account and Opportunity
- When an Opportunity is Closed Won, Renewal Term should update to the corresponding contract length from the Opportunity, provided that Renewal Term isn't already populated.
Embarrassingly, all I have so far is:
trigger updatelength on Account (after update) { Opportunity__c[] opptoupdate = new Opporunity__c[]{}; for(account c: trigger.new){ for(Opportunity__c cp: [select id,name,Contract_Length__c from Opportunity__c where Account = :c.id]{ } } }
Would anyone mind helping me out?
Thanks!
You could probably solve this without a trigger, using the Process Builder. But regardless of that, keep the following in mind (some were mentioned before):
You should end up with something similar to the simplified example below.
Trigger
ClassAll Answers
As per description it loooks like you want to udpate opportunity on the basis of opportunity update (clsed or not).
1. When opportunity is closed renewal term should update hence you need to write a trigger on Opportunity not on account. Before udpate should work here. 2. Other thing you should keep in mind that you need to run this trigger only when Opportunity is closed.
3. Next you need to define what value should get udpated on the basis of renewal term like fi renewal term is A then length is 1. and then put conditions accordingly.
note that opportunity and account are standard objects. Hence no need to use "__c"
You could probably solve this without a trigger, using the Process Builder. But regardless of that, keep the following in mind (some were mentioned before):
You should end up with something similar to the simplified example below.
Trigger
ClassThanks @Harmpie! I was able to use your trigger and class successfully once I added in my Case conditions. To @NRD 4 U I appreciate the help as I can see how yours would work too. Do either of you have any suggestions on the Test Class?
What I have so far is:
But I am unsure how to actually perform the check against the Account.Renewal_Term__c. Any suggestions would be appreciated!
Didn't test compilation, so might get some syntax errors, but this should get you on the right track
Here is my actual working class;
Your trigger seems to be called on update but the test class doesnt have any update calls.
In test class, close the opportuntities means change the status to closed and update the contract term. It should call your trigger.
Ah makes sense thanks @NRD 4 U
I have an error: Update Failed. First Exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
I have changed the code so that it is:
So that it updates allOpps instead of insert allOpps, but this error is throwing me for a loop as the Id appears to be specified.
Try this.
You should first insert and then update.
For unit testing you will have to insert the data first. Means you need to create your own data on which you will perform unit testing. hece you will have to first insert and then update. Both are requried for creating unit test.
Enjoy.