You need to sign in to do that
Don't have an account?

Email with collated opportunities per owner after meeting certain criteria
Need some guidance or a piece of code for the below mentioned scenario,
everyday at certain time, lets say at 9 am I want to send an email to opportunity owners with all their opportunities where amount is updated to 20000. so if one owner has 20 opportunity and has 20000 amount in each, then it will collate all these opportunity with their name and amount field and frame one email and send to the owner.
Please help me with the code if possible..
everyday at certain time, lets say at 9 am I want to send an email to opportunity owners with all their opportunities where amount is updated to 20000. so if one owner has 20 opportunity and has 20000 amount in each, then it will collate all these opportunity with their name and amount field and frame one email and send to the owner.
Please help me with the code if possible..
With the code that I have written below, there are a few things to keep in mind:
- Depending on your org, there is a limit as to how many single e-mails can be sent per day. Thus, depending on how many people you think will need to receive an email, we may have to take a different approach.
- Even though the code works, in the test email, I noticed that it only displays a maximum of 10 records.I am trying to find a work around for this.
- Besides code, this may be actually possible through Reports, subscribe method but I have to research more on this.
- If you believe more than 50k records will need to be processed, then we will have to convert the code I wrote into a batch instead.
Feel free to tell me your thoughts.
There is more to it in the criteria.. so when amount is meeting certain criteria, it should collate emails and send to owners.. Also, it should fire if related object opportunityline item has amount 4000. the complete requirement will as follows:
if only opportunity is meeting criteria it should show only opportunity id,
if line item is meeting the criteria, it should show line item id only....
if an opportunity has amount 20000 and line item has 4000, email should show the opportunity id and the line item id.
anythin on this please.. I really appreciate your assistance in this...
Conditional #1: If an opportunity doesn't have an amount of 20000 (or more - depending on your answer below), but has line items with an amount of 4000, then show only the ids of those line items.
Quick Question(s):
1. When you say line items, are you referring to the products associated with an opportunity or is this a custom object within your sandbox?
2. Does an opportunity have to equal 20000 exactly or are we considering any opportunity that has an amount equal to or greater than 20000?
3. Does a line item have to equal 4000 exactly or are we considering any line item that has an amount equal to or greater than 4000?
Conditional #2: If an opportunity has an amount of 20000 (or more - depending on your answer above), and it also has line items with an amount of 4000 (or more - depending on your answer above), then show both the opportunity and line item ids.
Conditional #3: If an opportunity has an amount of 20000 (or more - depending on your answer above), and no line items with an amount of 4000 (or more - depending on your answer above), then show only the opportunity ids.
Please confirm and answer the questions above
1. When you say line items, are you referring to the products associated with an opportunity or is this a custom object within your sandbox?
Ans.. it’s a custom object related with opportunity which has master-detail relationship
2. Does an opportunity have to equal 20000 exactly or are we considering any opportunity that has an amount equal to or greater than 20000?
Ans. It is equal to 20000.
3. Does a line item have to equal 4000 exactly or are we considering any line item that has an amount equal to or greater than 4000?
Ans. Line items which has amount equal to 4000.
Anything on this request please...
Currently, I am investigating if there is a way to render a PDF with all this data and include it as an attachment within an e-mail. If this is possible, would this be something that you would like to try? Please note that this may take some time to develop since there would be multiple parts to it most likely.
Also, does your company use Lightning? If yes, there may be a possibility to achieve this through the use of a lightning component. Thankfully, I know flows support both classic and lightning so I plan on exploring as to what they can do in this situation also. I believe they also have the ability to send emails.
So I know you may be asking, is there a temporary workaround in the meantime. Well, thankfully, I think I found one. With reports, you can subscribe to receive an email copy of them if certain conditions are met. Thus, what you can do is create a report with Opportunities that have an Amount equal to $20,000 and another with Line Items that have an Amount equal to $4000. Save both of these records in a public folder and then subscribe to them. When you subscribe to a report, specify as to when you would like to receive an e-mail copy of them. Before receiving the email, the report would autorun to auto-update itself. You can specify to only receive an email when there is at least 1 record within the report to prevent spam. Then, you can specify who you would like to receive this email. It can be particular users, groups, roles or roles and their subordinates. Those who receive the e-mail will be able to view the report and the records the belong to them which meet the criteria that you mentioned. I was wondering if you were aware of this feature and if you think this would be beneficial?
I apologize again for the wait and I look forward to hearing back from you soon!
1 - Schedule an Apex class called "OppEmailScheduler" to send an email to only internal users who have at least one opportunity record with an amount equal to $20,000 or at least one Line Item record with amount equal to $4,000.
2 - When the user receives an email, it will include a message and a link to view a Visualforce page called, "OppReport". When the user logs in to the view the page, they will be presented with a report that showcases a list of all opportunities and line items that they own and meet the criteria specified above. Thus, each user would have their own individual report to work with. At the moment, the visualforce page can display up to 50k records.
Please provide me your thoughts on this approach. If you believe this would fulfill your requirement, then I will provide the code. Thank you in advance!
Step #1 - Save in a separate Apex Class: Opportunity Report Schedulable Email Code - This code is responsible for sending an email out to specific internal users that have an opportunity or line item that fulfills the requirement.
Step #2 - Save the following code into a separate Apex Class - This is the controller code for the Visualforce page to display information.
Step #3 - Save the following code into a new Visualforce Page. This is the page that will be presented to the user when they click on the link within the email.
Step #4 (Optional) - Save the following code into a separate Apex class. This code is a duplicate of the code stated within the schedule apex class you created earlier. It is used solely for testing purposes. When you want to execute it, navigate to Developer Console, click on "Open Execute Anonymous Block" under the "Debug" tab and execute the following a code: oppEmailScheduleTest.sendOppEmail(); - Please note all users with Line Items and Opportunities with the amount criteria will be sent an e-mail with a link to view the VF page where they will see all their records. In regards to your question for when additional conditions may be required, I can potentially have users filter the results on the VF page using the stage picklist. Would something like this or did you have something else in mind?
Feel free to inform you if you have any issues within code above. Sorry again for the late reply!
Thank you for the great support in this.
It seems to be working fine. But I also want to show the opportunity and line item records in email with a hyperlink on their record IDs, and the overall report link so that if they dont want to go in salesforce they can see records in the email itself. anything on this please. thank you.