+ Start a Discussion
Heather BazelHeather 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

 
GarryPGarryP
Hi 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.

 
Heather BazelHeather Bazel
Hi Girish, thanks so much for this! I will give it a go! :)
GarryPGarryP
Sure. Let me know if you face any challenges.
Note: Flag answered as solved if that helps.
SaranSaran
Hi Heather,

I have written a trigger with your above reqs.

below is the trigger 
trigger RevenueCreation on opportunity (after update)  
{
	list<opportunity> newList = new list<Oportunity>();

	for(Opportunity opp : trigger.new)
	{
		if(opp.Duration__c != null && opp.Duration__c != trigger.oldMap.get(opp.Id).Duration__c) // check weather the duration field is changed.
		{
		    newList.add(opp); // adding only particular oppty whose duration has changed.
		}
	}

	if(newList.size() > 0)
		RevenueCreationHelper.createRevenue(newList);  // passing particular records to class.
}

below is the helper class
 
public class RevenueCreationHelper
{
	public static void createRevenue(list<opportunity> opptyList)
	{
		list<Revenue_Recogntion__c> revRegList = new list<Revenue_Recogntion__c>();  // List holding new Revenue Recogntion records
		map<integer , string> quarterMap = createQuarterMap(); 

		for(opportunity opp : opptyList)
		{
			date oppStartDate = opp.Start_Date__C;
			integer duration = integer.valueOf(opp.Duration__c);   // since oppty's duration is a string converting to integer.
			decimal revAmount = opp.Amount / duration;

			for(integer i=0; i < duration; i++)
			{
				Integer monthOfStartDate = oppStartDate.month();

				Revenue_Recogntion__c revReg = new Revenue_Recogntion__c();
				revReg.Revenue__c = revAmount;
				revReg.Quarter__c = quarterMap.get(monthOfStartDate);
				revReg.Year__c = oppStartDate.year();
				revRegList.add(revReg);

				monthOfStartDate.addMonths(duration);
			}
		}

		insert revRegList;
	}

	public static map<integer , string> createQuarterMap() // return's a map such as (key 1,2,3 - value 'Q1' , key 4,5,6 - value is 'Q2') so on
	{
		map<integer , string> quarterMap = new map<integer , string>();

		for(integer i = 1 ; i <= 12; i+=)
		{
			if(i > 9)
				quarterMap.put(i , 'Q4');
			else if(i > 6)
				quarterMap.put(i , 'Q3');
			else if(i > 3)
				quarterMap.put(i , 'Q2');
			else if(i > 0)
				quarterMap.put(i , 'Q1');
		}
		return quarterMap;
	}
}

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
GarryPGarryP
The logic to get the quarter can be pushed to Formula field in Oppty object that will reduce the class logic.
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.
Heather BazelHeather Bazel
Hi Saran,
I tried plugging in the help class but I got this error:
Error: Compile Error: unexpected token: ')' at line 35 column 40
SaranSaran
Hi,

Instead of for(integer i = 1 ; i <= 12; i+=)

change with for(integer i = 1 ; i <= 12; i++)

Thanks