function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jennifer HardyJennifer Hardy 

Apex Code Help - Developer Needed

Hello all! We need to create a reportable field that will count the number of days between opportunities in a single account. This field will need to be placed at the opportunity level and once the number is calculated, it cannot change. We are looking for some help from a developer to help create the code, as we no longer have developer support on payroll (and my knowledge of Apex is only in reading, not practice). Suggestions??

Here is an example of what we need:

Account A has 3 Opportunities, A, B, & C.
Opp A was ordered on 6/24/2014
Opp B was ordered on 7/1/2014
Opp C was ordered on 7/5/2014

Opp B needs a field that will report the number of days between it and Opp A. Opp C needs a field that reports the number of days between it and Opp B. I then need to be able to run a report on said field in the future.

Thanks for your all of your time!!!
Best Answer chosen by Jennifer Hardy
SKolakanSKolakan
Jennifer, You can do this without triggers. This works for all new opportunities.

1. On Account, create a rollup summary field with 
  • Label: Last Opportunity Date,Summarized object: opportunity,Summarize Type : MAX,Field to Aggregate: Created Date
2. Create an number field on Opportunity 
  • Label: Days since Last Opportunity,Length: 5
3. Create a workflow rule on Opportunity
  • Evaluation Criteria:  Created,Rule criteria : Account: Last Opportunity Date NOT EQUAL TO  (leave the value blank - it will consider as null)
4. Create Field update workflow action
  • Field to update : ​Opportunity: Days Since Last Opportunity,Formula: NOW() - Account.Last_Opportunity_Date__c
5. Activate Workflow rule

Replace field names with the names you created. For existing opportunities, you can do a data load or write a batch job.

Hope it helps!

 

All Answers

Terence_ChiuTerence_Chiu
Jennifer, instead of using Apex a possbile declaritve (non-code) solution could be the following.
  • Create a lookup field on the Opportunity object that will lookup another Opportunity
  • Create a formula field that calculates days between Opportunities via the lookup relationship.
    • Formula would look something like this: CreatedDate - Link_Opportunity__r.CreatedDate
For example, a Opportunity lookup field called Linked Opportunity (Linked_Opportunity__c) is created. 

Opp B would have the Linked Opportunity Field populated with Opp A
Opp C would have the Linked Opportunity Field populated with Opp B.

There are a few manual steps for this to work, but it would be a start. 

 
SKolakanSKolakan
Jennifer, You can do this without triggers. This works for all new opportunities.

1. On Account, create a rollup summary field with 
  • Label: Last Opportunity Date,Summarized object: opportunity,Summarize Type : MAX,Field to Aggregate: Created Date
2. Create an number field on Opportunity 
  • Label: Days since Last Opportunity,Length: 5
3. Create a workflow rule on Opportunity
  • Evaluation Criteria:  Created,Rule criteria : Account: Last Opportunity Date NOT EQUAL TO  (leave the value blank - it will consider as null)
4. Create Field update workflow action
  • Field to update : ​Opportunity: Days Since Last Opportunity,Formula: NOW() - Account.Last_Opportunity_Date__c
5. Activate Workflow rule

Replace field names with the names you created. For existing opportunities, you can do a data load or write a batch job.

Hope it helps!

 
This was selected as the best answer
Jennifer HardyJennifer Hardy
Thank you to both of you. I believe your suggestions worked!!!