Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglascayers/886162b8c3830fed2ab8 to your computer and use it in GitHub Desktop.
Save douglascayers/886162b8c3830fed2ab8 to your computer and use it in GitHub Desktop.
Apex code examples for blog post
/**
* Written by Doug Ayers, https://communities.bmc.com/people/douglas.ayers%40fotlinc.com
* Inspired by Paul Donders, https://communities.bmc.com/people/p.donders@infravision.com
* BMC Communities, https://communities.bmc.com/ideas/3039
*/
trigger BMCRF_IncidentTrigger on BMCServiceDesk__Incident__c ( before update ) {
if ( Trigger.isBefore && Trigger.isUpdate ) {
BMCRF_IncidentTriggerHelper.checkAndSetImpact( Trigger.new );
BMCRF_IncidentTriggerHelper.checkAndSetUrgency( Trigger.new );
}
}
/**
* Written by Doug Ayers, https://communities.bmc.com/people/douglas.ayers%40fotlinc.com
* Inspired by Paul Donders, https://communities.bmc.com/people/p.donders@infravision.com
* BMC Communities, https://communities.bmc.com/ideas/3039
*/
public class BMCRF_IncidentTriggerHelper
{
public static void checkAndSetImpact( List<BMCServiceDesk__Incident__c> incidents ) {
// get list of all active impacts
List<BMCServiceDesk__Impact__c> impactList = new List<BMCServiceDesk__Impact__c>([
SELECT id, name FROM BMCServiceDesk__Impact__c WHERE BMCServiceDesk__inactive__c = false
]);
// convert list of impacts into a map so we can do look ups by impact name (eg, 'MEDIUM')
Map<String, BMCServiceDesk__Impact__c> impactMap = new Map<String, BMCServiceDesk__Impact__c>();
for ( BMCServiceDesk__Impact__c impact : impactList ) {
impactMap.put( impact.name, impact );
}
// loop through each incident and check if its impact should be set
for ( BMCServiceDesk__Incident__c incident : incidents ) {
System.debug( 'Incident Impact: ' + incident.BMCServiceDesk__FKImpact__r );
System.debug( 'Change Impact Controller: ' + incident.Change_Impact_Controller__c );
if ( String.isNotBlank( incident.Change_Impact_Controller__c ) ) {
// get the impact reference from the map whose name matched the custom controller field's value
BMCServiceDesk__Impact__c impact = impactMap.get( incident.Change_Impact_Controller__c );
if ( impact != null ) {
System.debug( 'Changing impact to: ' + impact );
incident.BMCServiceDesk__FKImpact__c = impact.id;
} else {
System.debug( LoggingLevel.WARN, 'Unknown impact to set: ' + incident.Change_Impact_Controller__c );
}
// clear the controller value so this code doesn't run again
// unless the workflow rules reset the controller value
incident.Change_Impact_Controller__c = null;
}
}
}
public static void checkAndSetUrgency( List<BMCServiceDesk__Incident__c> incidents ) {
// get list of all active urgencies
List<BMCServiceDesk__Urgency__c> urgencyList = new List<BMCServiceDesk__Urgency__c>([
SELECT id, name FROM BMCServiceDesk__Urgency__c WHERE BMCServiceDesk__inactive__c = false
]);
// convert list of urgencies into a map so we can do look ups by urgency name (eg, 'MEDIUM')
Map<String, BMCServiceDesk__Urgency__c> urgencyMap = new Map<String, BMCServiceDesk__Urgency__c>();
for ( BMCServiceDesk__Urgency__c urgency : urgencyList ) {
urgencyMap.put( urgency.name, urgency );
}
// loop through each incident and check if its urgency should be set
for ( BMCServiceDesk__Incident__c incident : incidents ) {
System.debug( 'Incident Urgency: ' + incident.BMCServiceDesk__FKUrgency__r );
System.debug( 'Change Urgency Controller: ' + incident.Change_Urgency_Controller__c );
if ( String.isNotBlank( incident.Change_Urgency_Controller__c ) ) {
// get the urgency reference from the map whose name matched the custom controller field's value
BMCServiceDesk__Urgency__c urgency = urgencyMap.get( incident.Change_Urgency_Controller__c );
if ( urgency != null ) {
System.debug( 'Changing urgency to: ' + urgency );
incident.BMCServiceDesk__FKUrgency__c = urgency.id;
} else {
System.debug( LoggingLevel.WARN, 'Unknown urgency to set: ' + incident.Change_Urgency_Controller__c );
}
// clear the controller value so this code doesn't run again
// unless the workflow rules reset the controller value
incident.Change_Urgency_Controller__c = null;
}
}
}
}
@isTest
private class BMCRF_IncidentTriggerHelperTest
{
@isTest
static void test_set_incident_impact_and_urgency() {
BMCServiceDesk__Impact__c impact = new BMCServiceDesk__Impact__c();
impact.name = 'MEDIUM';
insert impact;
BMCServiceDesk__Urgency__c urgency = new BMCServiceDesk__Urgency__c();
urgency.name = 'MEDIUM';
insert urgency;
BMCServiceDesk__Category__c category = new BMCServiceDesk__Category__c();
category.name = 'Test Category';
category.BMCServiceDesk__AvailableForIncidents__c = true;
insert category;
BMCServiceDesk__Incident__c incident = new BMCServiceDesk__Incident__c();
incident.BMCServiceDesk__incidentDescription__c = 'Test';
incident.BMCServiceDesk__IncidentType__c = 'Incident';
incident.BMCServiceDesk__FKCategory__c = category.id;
incident.BMCServiceDesk__FKClient__c = UserInfo.getUserId();
System.assert( incident.BMCServiceDesk__FKImpact__c == null );
System.assert( incident.BMCServiceDesk__FKUrgency__c == null );
Test.startTest();
insert incident;
Test.stopTest();
incident = [ select id, BMCServiceDesk__Impact_Id__c, BMCServiceDesk__Urgency_Id__c from BMCServiceDesk__Incident__c where id = :incident.id ];
System.assert( incident.BMCServiceDesk__Impact_Id__c == 'MEDIUM' );
System.assert( incident.BMCServiceDesk__Urgency_Id__c == 'MEDIUM' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment