Skip to content

Instantly share code, notes, and snippets.

@olgaloza
Created October 22, 2018 02:34
Show Gist options
  • Save olgaloza/fcc728c5d2c162f0d3a354365cd9ccc9 to your computer and use it in GitHub Desktop.
Save olgaloza/fcc728c5d2c162f0d3a354365cd9ccc9 to your computer and use it in GitHub Desktop.
RAD 2 Homework 2 - OpportunityUtility class
//utility class for homework 2
public with sharing class OpportunityUtility {
//this is static a method that Takes an integer as an argument and returns a list of the top n Opportunities,
//ordered first by amount in descending order, then by Account Name.
//n is the integer that was passed in as an argument
//Include: Opportunity ID, Amount, Account Name, MainCompetitors__c, CloseDate, Stage
public static List<Opportunity> getTopOpportunities(Integer n) {
List<Opportunity> topOpportunities =
[ SELECT Id, Name, Amount, Account.Name, MainCompetitors__c, CloseDate, StageName
FROM Opportunity
ORDER BY Amount DESC, Account.Name ASC
LIMIT :n ];
System.debug('-----> Top ' + n + ' oppties: ');
For (Integer i=0; i < n; i++) {
System.debug('-----> Opportunity name: ' + topOpportunities[i].Name);
System.debug('-----> Amount: ' + topOpportunities[i].Amount);
System.debug('-----> Close Date: ' + topOpportunities[i].CloseDate);
System.debug('-----> Stage: ' + topOpportunities[i].StageName);
System.debug('-----> Account: ' + topOpportunities[i].Account.Name);
}
return topOpportunities;
}
/* Locates all opportunities in an open stage with a close date in the past and creates a task prompting the user
to update the close date. You can assign to yourself or create a user for this purpose
*/
public static void addTaskToOverdueOppties() {
//Find overdue opportunities
List<Opportunity> OverdueOpportunities =
[SELECT Id, Name, StageName, IsClosed, CloseDate
FROM Opportunity
WHERE CloseDate < Today AND IsClosed = FALSE];
//create a list to collect all tasks we will create (bulkification!)
List<Task> TasksList = new List<Task>();
//Loop through the Oppties we pulled up with the above query and create a Task for each
For (Opportunity o : OverdueOpportunities) {
Task t = new Task();
t.OwnerId = UserInfo.getUserId();
t.Description ='Opportunity \"' +o.Name+ '\" was due on ' + o.CloseDate +'. Please advance the Close Date or close this Oppty.';
t.Subject = 'Overdue opportunity';
t.WhatId = o.Id;
t.Status = 'Not Started';
t.ActivityDate = Date.Today();
System.debug('------> Added a Task');
TasksList.add(t);
}
//bulk insert the Tasks list
insert(TasksList);
System.debug('------> Created ' + TasksList.size() + ' Tasks.');
}
/* Takes a String as an argument representing State, and returns a map
of the open opportunities for accounts in that State (Map is Id:Opportunity)
*/
public static Map<Id, Opportunity> mapOfOpenOpportunitiesByState(String state) {
//Create a map of all opportunities in the specified state
Map<Id, Opportunity> opptyMap = new Map<Id, Opportunity>([SELECT ID, Name, Account.BillingState
FROM Opportunity
WHERE IsClosed = FALSE AND Account.BillingState =:state]);
//???convert the map size to an integer bcs for some reason it wouldn't work in the debug log string
Integer mSize = opptyMap.size();
System.debug('------> Map has '+mSize+' opportunities.');
System.debug(opptyMap);
return opptyMap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment