You need to sign in to do that
Don't have an account?
Heather Bazel
Help with Apex Class
Hi all,
I am an SFDC Admin and am VERY new to the developer side of SFDC. I am currently trying to learn Apex Classes and Triggers. I was wondering if anyone could write my psudeo code for me with actual apex code and syntax? This would REALLY help me understand how to tranlate it.
Thanks!!!!
Heather
Business need: Want to recognize revenue over time for Forecasting. Large deals are recognized by Quarter.
Technical need: To insert records on custom object "Revenue Recogntion" whenever specific fields on opportunity are populated.
If Opportunity fields Duration (picklist) is populated then records in Revenue Recognition custom object need to be inserted.
When new records are inserted, specific fields must be auto populated based on values populated in Opportunity:
Duration
Start Date
Example:
If Duration on Opportunity equals "3"
And Start Date equals 8/1/2015
And Amont equls "$60,000"
Then 3 records must be inserted in the Revenue Recognition custom object with the following fields auto populated:
Record 1:
Revenue: $20,000 (calculation is Amount / Duration)
Rev Rec Quarter: Q3 (Since August is in the third quarter)
Rev Rec Year: 2015 (copies the year from the Start Date)
Record 2:
Revenue: $20,000 (same calculation)
Rev Rec Quarter: Q4 (since 3 was chosen for number of quarters in Duration)
Rev Rec Year: 2015 (copies the year from the Start Date)
Record 3:
Revenue: $20,000 (same calculation)
Rev Rec Quarter: Q1
Rev Rec Year: 2016 (must be pushed out a year since after Q4)
Psudeo Code:
Get AmtPerDuration
numDuration = Opp.Duration
Amount / numDuration
AmountPerDuration
Find Quarter and Year (Start Date)
If Start month == Jan, Feb, March
(Quarter = Q1)
Else If Start month == April, May, June
(Quarter = Q2)
Else If Start month == July, Aug, September
(Quarter = Q3)
Else If Start month == Oct, Nov, Dec
(Quarter = Q4)
If Quarter = after Q4
Year + 1
So I need this piece in a class, and then I need a trigger which will call this class... the trigger would be:
If Duration is not equal to null, then insert Rev Rec Records and Populate fields based on Duration (quarters) and Start Date.
Really ANY help on this would be SO helpful!! I really want to learn how to do this... and I really understand the above concept as I came up with it... so seeing this put into code will really help me understand the syntax of it.
Thanks so much for any help provided!!
Heather
I am an SFDC Admin and am VERY new to the developer side of SFDC. I am currently trying to learn Apex Classes and Triggers. I was wondering if anyone could write my psudeo code for me with actual apex code and syntax? This would REALLY help me understand how to tranlate it.
Thanks!!!!
Heather
Business need: Want to recognize revenue over time for Forecasting. Large deals are recognized by Quarter.
Technical need: To insert records on custom object "Revenue Recogntion" whenever specific fields on opportunity are populated.
If Opportunity fields Duration (picklist) is populated then records in Revenue Recognition custom object need to be inserted.
When new records are inserted, specific fields must be auto populated based on values populated in Opportunity:
Duration
Start Date
Example:
If Duration on Opportunity equals "3"
And Start Date equals 8/1/2015
And Amont equls "$60,000"
Then 3 records must be inserted in the Revenue Recognition custom object with the following fields auto populated:
Record 1:
Revenue: $20,000 (calculation is Amount / Duration)
Rev Rec Quarter: Q3 (Since August is in the third quarter)
Rev Rec Year: 2015 (copies the year from the Start Date)
Record 2:
Revenue: $20,000 (same calculation)
Rev Rec Quarter: Q4 (since 3 was chosen for number of quarters in Duration)
Rev Rec Year: 2015 (copies the year from the Start Date)
Record 3:
Revenue: $20,000 (same calculation)
Rev Rec Quarter: Q1
Rev Rec Year: 2016 (must be pushed out a year since after Q4)
Psudeo Code:
Get AmtPerDuration
numDuration = Opp.Duration
Amount / numDuration
AmountPerDuration
Find Quarter and Year (Start Date)
If Start month == Jan, Feb, March
(Quarter = Q1)
Else If Start month == April, May, June
(Quarter = Q2)
Else If Start month == July, Aug, September
(Quarter = Q3)
Else If Start month == Oct, Nov, Dec
(Quarter = Q4)
If Quarter = after Q4
Year + 1
So I need this piece in a class, and then I need a trigger which will call this class... the trigger would be:
If Duration is not equal to null, then insert Rev Rec Records and Populate fields based on Duration (quarters) and Start Date.
Really ANY help on this would be SO helpful!! I really want to learn how to do this... and I really understand the above concept as I came up with it... so seeing this put into code will really help me understand the syntax of it.
Thanks so much for any help provided!!
Heather
you can just have kick start by going to salesforce articel -
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_HelloWorld.htm
Once you are done with basic of Apex here you need INSERT record , create list of record and Simple for loop.
All these are very much similar to java.
i believe this class and trigger logic would be real easy to put into the correct syntax.
Steps for Apex class logic
1. create an Apex class with a method that takes List<Opportunity> object record as argument.
2. Inside Method Iterate the list of opportunity using for loop.
3. inside loop create object record for custoom object and populate fields as per your logic.
4. add new record to List<Custom object>
5. outside for loop insert the list.
For trigger logic.
1. write trigger in after update/insert context
2. Pass Trigger.NEW as an argument to method above.
3. Call the class
4. class will process the logic
job done.
Let me know if you get the idea and are able to start on this.
Note: Flag answered as solved if that helps.
I have written a trigger with your above reqs.
below is the trigger
below is the helper class
Hope this help you to solce your business needs.
As Girish mentioned you can go through the documents to understand how apex works. Also you can go through the trailhead where you can take a challenge and try of your own
Thanks
Advanatge of pushing to formula field -
1. Above Logic would be more clean
2. The forlmual field can be used in reports that will show reports as per required Quarter results.
I tried plugging in the help class but I got this error:
Error: Compile Error: unexpected token: ')' at line 35 column 40
Instead of for(integer i = 1 ; i <= 12; i+=)
change with for(integer i = 1 ; i <= 12; i++)
Thanks