You need to sign in to do that
Don't have an account?
Alex Merwin
APEX Trigger Advice: Cross Populate Fields between Opportunity & OpportunityLineItem standard objects
Hello -
I'm new to APEX and struggling with what should be a pretty easy ask I think.
When a user updates the StageName field on the Opportunity standard object to 'Notify Demand', I need all OpportunityLineItems (standard object) associated with this Opportunity to have a picklist custom field named 'Campaign Status' updated to 'Buyer Approval Needed'.
It is my intent to schedule a workflow rule to trigger based off the update of the OpportunityLineItem object, with a lot of merged fields from that object. So, you update the StageName at the Opportunity level, and a series of customized emails deploys, one per OpportunityLineItem.
Here's what I have so far for the Trigger:
I'm new to APEX and struggling with what should be a pretty easy ask I think.
When a user updates the StageName field on the Opportunity standard object to 'Notify Demand', I need all OpportunityLineItems (standard object) associated with this Opportunity to have a picklist custom field named 'Campaign Status' updated to 'Buyer Approval Needed'.
It is my intent to schedule a workflow rule to trigger based off the update of the OpportunityLineItem object, with a lot of merged fields from that object. So, you update the StageName at the Opportunity level, and a series of customized emails deploys, one per OpportunityLineItem.
Here's what I have so far for the Trigger:
trigger update on Opportunity (before insert, before update){ List<ID> OppIds = New List<ID>(); for(Opportunity o : Trigger.new){ if(o.StageName == 'Notify Demand' && o.OpportunityLineItem__c != null){ OppIds.add(o.OpportunityLineItem__c); } } List<OpportunityLineItem__c> oppList = [SELECT id, Campaign_Status__c FROM OpportunityLineItem WHERE id in :OppIds]; for(integer i = 0 ; i < oppList.size(); i++){ oppList[i].Campaign_Status__c = ‘Buyer Approval Needed’; } update oppList; }
All Answers
NOTE: This code has not been tested and may contain typographical or logical errors
I changed this to be on update only (since you cannot have an Opportunity with Line Items on create. Then we get all of the Opportunities that the StageName changed to Notify Demand. The way you had it before it would fire everytime any Opportunity was update with that and reset the status of all your line items. Then we query all of your line items and then update their status.
Error: Compile Error: expecting a semi-colon, found '}' at line 23 column 8
I then added a semi-colon:
And got the following additional compile error:
Error: Compile Error: Variable does not exist: olList at line 25 column 16
Got any advice on how I can remedy?
This is a huge help! I have a few other use cases I think we'll use this snippet for as well once we get it working.
Thanks a lot.
- Is there a way to restrict the Trigger to only fire when Opportunity.RecordTypeID has a certain value?
- Is there a way to populate the Campaign_Status__c field with 2 potential values, determined by a checkbox field on the OpportunityLineItem object called INTL_Demand_Buyer_Approved__c ?
So, something like:Error: Compile Error: AND operator can only be applied to Boolean expressions at line 13 column 13
Also, the logic for whether to go to 'Buyer Approved, Ready to Launch' or 'Buyer Approved, Pending Local Demand Approval' is based on a text field on the Opportunity Product object called INTL_Demand_Approval__c. My bad for missing that on my original inquiry! I tweaked the code as follows, but still getting the above compile error.
The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.OLIUpdate_SAP_BuyerApproval
OLIUpdate_SAP_SendLaunchTickets
OLIUpdate
It's strange because I'm only including the OLIUpdate trigger in the Outbound Change set, yet the error references the other two triggers associated with the Opportunity object. I tried a second change set including all three triggers from Sandbox but it's throwing the same error.
Here's the code for the three triggers:
OLIUpdate
OLIUpdate_SAP_SendLaunchTickets
OLIUpdate_SAP_BuyerApproval
Each test should follow the following structure:
- Setup of test data. This includes creation of any data needed by your class. Account, Contacts etc
- Starting the test. This is calling Test.startTest() to reset the governor limits
- Calling your class / method
- Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
- Asserting that your changes have worked
- If you have inserted/updated/deleted data, you need to query for the updates
- Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/
https://developer.salesforce.com/forums?state=id#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=OPENQUESTIONS&id=906F0000000MHLdIAO