You need to sign in to do that
Don't have an account?
Harvey2016
Logic doesn't work for some reason.
I was working on a practice scenario when if Hello field on Opportunity is empty before inserting and updating. the field should filled with world. Below is my code. Can someone tell me where I did wrong
public class OppHelloWorld { public void addHelloWorld(List<opportunity> opps) { for (Opportunity o:opps) { if (o.Hello__c != 'World') { o.Hello__c = 'World'; } } } }Below is the Trigger Code:
trigger HelloWorld on Opportunity (Before Insert, Before update) { List <Opportunity> Opps = new List<Opportunity>(); OppHelloWorld OHW = New OppHelloWorld(); OHW.addHelloWorld(Opps); }
In your example, you create a new instance of a List of Opportunities and feed it into your helper. Since it is empty, your code doesn't really run on any Opp.
Trigger.new is a a special kind of Trigger Context Variable containing the list of newly inserted or updated lists. Please refer to https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
Also, there's a Trailhead on Trigger in Apex: https://trailhead.salesforce.com/en/modules/apex_triggers
All Answers
In your example, you create a new instance of a List of Opportunities and feed it into your helper. Since it is empty, your code doesn't really run on any Opp.
Trigger.new is a a special kind of Trigger Context Variable containing the list of newly inserted or updated lists. Please refer to https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
Also, there's a Trailhead on Trigger in Apex: https://trailhead.salesforce.com/en/modules/apex_triggers