Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Created April 3, 2019 08:44
Show Gist options
  • Save swapnilshrikhande/67a98e842d52a36a8e772cdeda36d150 to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/67a98e842d52a36a8e772cdeda36d150 to your computer and use it in GitHub Desktop.
Trigger Architecture
trigger Opportunity_AfterUpdate on Opportunity (after update) {
if (Settings.OPPORTUNITY_SETTING.Disable_All__c == true) {
return;
}
// Try to process transactions as needed
new Opportunity_ValidateTransactions(trigger.old, trigger.new).execute();
// ?
new Opportunity_RelateMatching(trigger.old, trigger.new).execute();
// to create default soft credits on giving
new Opportunity_UpdateSoftCredits(trigger.old, trigger.new).execute();
// update item delivery status on OpportunityLineItem
new Opportunity_UpdateItemDeliveryStatus(trigger.old, trigger.new).execute();
}
public abstract class OpportunityTrigger {
public static final Map<String, Schema.RecordTypeInfo> RT_RELATIONSHIP = rC_Bios__Relationship__c.SobjectType.getDescribe().getRecordTypeInfosByName();
public static final Schema.RecordTypeInfo RT_RELATIONSHIP_ACCOUNT_ACCOUNT = RT_RELATIONSHIP.get('Account - Account');
public static final Schema.RecordTypeInfo RT_RELATIONSHIP_ACCOUNT_CONTACT = RT_RELATIONSHIP.get('Account - Contact');
public static final Schema.RecordTypeInfo RT_RELATIONSHIP_ACCOUNT_GIVING = RT_RELATIONSHIP.get('Account - Giving');
public static final Schema.RecordTypeInfo RT_RELATIONSHIP_CONTACT_CONTACT = RT_RELATIONSHIP.get('Contact - Contact');
public static final Schema.RecordTypeInfo RT_RELATIONSHIP_CONTACT_GIVING = RT_RELATIONSHIP.get('Contact - Giving');
public static final Map<ID, Schema.RecordTypeInfo> RT_GIVING_BY_ID = Opportunity.SobjectType.getDescribe().getRecordTypeInfosById();
public static final Map<String, Schema.RecordTypeInfo> RT_GIVING = Opportunity.SobjectType.getDescribe().getRecordTypeInfosByName();
public static final Schema.RecordTypeInfo RT_GIVING_DONATION = RT_GIVING.get('Donation');
public static final Schema.RecordTypeInfo RT_GIVING_MEMBERSHIP = RT_GIVING.get('Membership');
public static final Schema.RecordTypeInfo RT_PROPOSAL = RT_GIVING.get('Proposal');
public static final Schema.RecordTypeInfo RT_TRANSACTION = RT_GIVING.get('Transaction');
public static final Schema.RecordTypeInfo RT_PLEDGEPAYMENT = RT_GIVING.get('Pledge Payment');
public static final Schema.RecordTypeInfo RT_PLEDGE = RT_GIVING.get('Pledge');
public static final Schema.RecordTypeInfo RT_OUTRIGHT = RT_GIVING.get('Outright Gift');
public static final Schema.RecordTypeInfo RT_PURCHASE = RT_GIVING.get('Purchase');
public static final String CURRENCY_CODE_CAD = 'CAD';
public static final String CURRENCY_CODE_EUR = 'EUR';
public static final String CURRENCY_CODE_USD = 'USD';
public static final String RT_DONATION = 'Donation';
public static final String RT_MEMBERSHIP = 'Membership';
public static final String RT_OUTRIGHT_GIFT = 'Outright Gift';
public static final Id RT_DONATION_ID = getRecordTypeId(RT_DONATION);
public static final Id RT_MEMBERSHIP_ID = getRecordTypeId(RT_MEMBERSHIP);
public static final Id RT_OUTRIGHT_GIFT_ID = getRecordTypeId(RT_OUTRIGHT_GIFT);
public static final Id RT_TRANSACTION_ID = getRecordTypeId('Transaction');
public static final Id RT_PLEDGE_PAYMENT_ID = getRecordTypeId('Pledge Payment');
public static final String STAGE_NAME_OPEN = 'Open';
public static final String STAGE_NAME_PENDING = 'Pending';
public static final String STAGE_NAME_COMPLETED = 'Completed';
public static final String STAGE_NAME_CANCELED = 'Canceled';
public static final String STAGE_NAME_UNCOLLECTIBLE = 'Uncollectible';
public static final String STAGE_NAME_SUSPENDED = 'Suspended';
public static final String STAGE_NAME_PENDING_RETRY = 'Pending - Retry';
public static final String STAGE_NAME_PENDING_FAILED = STAGE_NAME_PENDING + ' Failed';
public static final String STAGE_NAME_PARTIALLY_COMPLETE = 'Partially Complete';
public static final String STAGE_NAME_PARTIALLY_COLLECTED = 'Partially Collected';
public static final String PAYMENT_FREQUENCY_ANNUALLY = 'Annually';
public static final String PAYMENT_FREQUENCY_BIWEEKLY = 'Bi-Weekly';
public static final String PAYMENT_FREQUENCY_IRREGULAR = 'Irregular';
public static final String PAYMENT_FREQUENCY_MONTHLY = 'Monthly';
public static final String PAYMENT_FREQUENCY_ONE_PAYMENT = 'One Payment';
public static final String PAYMENT_FREQUENCY_ONE_TIME = 'One-Time';
public static final String PAYMENT_FREQUENCY_QUARTERLY = 'Quarterly';
public static final String PAYMENT_FREQUENCY_SEMIANNUALLY = 'Semi-Annually';
public static final String PAYMENT_FREQUENCY_SEMIMONTHLY = 'Semi-Monthly';
public static final String PAYMENT_FREQUENCY_WEEKLY = 'Weekly';
public static final String GIVING_FREQUENCY_ONE_PAYMENT = 'One Payment';
public static final String GIVING_FREQUENCY_ANNUALLY = 'Annually';
public static final String GIVING_FREQUENCY_SEMIANNUALLY = 'Semi-Annually';
public static final String GIVING_FREQUENCY_QUARTERLY = 'Quarterly';
public static final String GIVING_FREQUENCY_MONTHLY = 'Monthly';
public static final String GIVING_FREQUENCY_BIMONTHLY = 'Bi-Monthly';
public static final String GIVING_FREQUENCY_WEEKLY = 'Weekly';
public static final String GIVING_FREQUENCY_BIWEEKLY = 'Bi-Weekly';
public static final String GIVING_FREQUENCY_ONE_TIME = 'One-Time';
public static final String GIVING_FREQUENCY_SEMIMONTHLY = 'Semi-Monthly';
public static final String GIVING_FREQUENCY_IRREGULAR = 'Irregular';
public static final String GIVING_FREQUENCY_TOTAL = 'Total';
public static final String MATCHING_NOTICE_ABOVE_MAXIMUM_THRESHOLD = 'Above "Maximum Threshold" on the Matching Account';
public static final String MATCHING_NOTICE_BELOW_MINIMUM_THRESHOLD = 'Below "Minimum Threshold" on the Matching Account';
public static final String MATCHING_NOTICE_DEDUCTION_EXCEEDS_GIVING = 'Deduction from Items exceeds total "Matching Amount" value';
public static final String MATCHING_NOTICE_INSTALLMENTS_NOT_ALLOWED = 'Installments do not qualify for matching';
public static final String MATCHING_NOTICE_ITEMS_NOT_ALLOWED = 'Premium items do not qualify for matching';
public static final String MATCHING_NOTICE_MISSING_CURRENT_GIVING_AMOUNT = 'Missing value for "Current Giving Amount" field';
public static final String MATCHING_NOTICE_MISSING_MATCH_RATIO = 'Missing value for "Match Ratio" field on the Matching Account';
public static final String MATCHING_STATUS_APPROVED = 'Approved';
public static final String MATCHING_STATUS_INVALID = 'Invalid';
public static final String MATCHING_STATUS_NOT_SUBMITTED = 'Not Submitted';
public static final String MATCHING_STATUS_PENDING = 'Pending';
public static final String MATCHING_STATUS_SUBMITTED = 'Submitted';
public static final String PAYMENT_PROCESSOR_HEARTLAND = 'Heartland';
public static final String PAYMENT_PROCESSOR_SAGE = 'Sage';
public static final String PAYMENT_PROCESSOR_PAYPAL = 'PayPal';
public static final String TRANSACTION_TYPE_REFUND = 'Refund';
public static final String RELATIONSHIP_CATEGORY_EMPLOYMENT = 'Employment';
public static final String RELATIONSHIP_ROLE_EMPLOYEE = 'Employee';
public static final String RELATIONSHIP_ROLE_EMPLOYER = 'Employer';
public static final String RELATIONSHIP_CATEGORY_AUTO_CREDIT = 'Auto Credit';
public static final String RELATIONSHIP_ROLE_GIVER = 'Giver';
public static final String RELATIONSHIP_ROLE_RECIPIENT = 'Recipient';
public static final String SOFTCREDIT_TYPE_ACCOUNTONLY = 'Account Only';
public static final String SOFTCREDIT_TYPE_CONTACTONLY = 'Contact Only';
public static final String SOFTCREDIT_TYPE_ACCOUNTCONTACT = 'Account & Contact';
public Opportunity_Setting__c setting { get; set; }
public ReAttempt_Payment_Setting__c[] softDeclineList { get; set; }
public Map<String,Integer> softDeclineValuesMap { get;set;}
public Opportunity[] opportunityOldList { get; set; }
public Opportunity[] opportunityNewList { get; set; }
public Map<Id, Opportunity> opportunityOldListMap = new Map<Id, Opportunity>();
public Map<Id, Opportunity> opportunityNewListMap = new Map<Id, Opportunity>();
public OpportunityTrigger(Opportunity[] opportunityOldList, Opportunity[] opportunityNewList) {
this.opportunityOldList = opportunityOldList == null ? new Opportunity[] {} : opportunityOldList;
this.opportunityNewList = opportunityNewList == null ? new Opportunity[] {} : opportunityNewList;
// Build the old map
for(Opportunity opportunityOld : this.opportunityOldList) {
if (opportunityOld.Id != null) {
opportunityOldListMap.put(opportunityOld.Id, opportunityOld);
}
}
// Build the new map
for(Opportunity opportunityNew : this.opportunityNewList) {
if (opportunityNew.Id != null) {
opportunityNewListMap.put(opportunityNew.Id, opportunityNew);
}
}
// Fetch the common settings
this.setting = Opportunity_Setting__c.getInstance();
this.setting = this.setting == null ? new Opportunity_Setting__c() : this.setting;
setting.Max_Charge_Attempt__c = setting.Max_Charge_Attempt__c == null ? 1 : setting.Max_Charge_Attempt__c;
softDeclineList = ReAttempt_Payment_Setting__c.getAll().values();
this.softDeclineList = this.softDeclineList == null ? new ReAttempt_Payment_Setting__c[]{} : this.softDeclineList;
fillSoftDeclineValues();
}
public abstract Boolean executable(Opportunity opportunityOld, Opportunity opportunityNew);
public virtual void execute() {
if (setting.Disable_All__c == true) {
return;
}
Opportunity[] opportunityUpdateableList = new Opportunity[] {};
for(Opportunity opportunityNew : opportunityNewList) {
Opportunity opportunityOld = opportunityOldListMap.get(opportunityNew.Id);
opportunityOld = opportunityOld == null ? new Opportunity() : opportunityOld;
if (executable(opportunityOld, opportunityNew)) {
opportunityUpdateableList.add(opportunityNew);
}
}
if (opportunityUpdateableList.size() != 0) {
execute(opportunityUpdateableList, trigger.IsAfter);
}
}
public String blank(String value) {
return value == null ? '' : value;
}
public void fillSoftDeclineValues() {
SoftDeclineValuesMap = new Map<String,Integer>();
if (softDeclineList == null || softDeclineList.size() == 0) {
return;
}
for(ReAttempt_Payment_Setting__c softDecline:softDeclineList) {
if (softDecline.Soft_Decline_Values__c == null || softDecline.Soft_Decline_Wait_Period__c == null) {
continue;
}
if (softDecline.Soft_Decline_Values__c == ''|| softDecline.Soft_Decline_Wait_Period__c == 0) {
continue;
}
for (String softDeclineCode : softDecline.Soft_Decline_Values__c.split(',')) {
SoftDeclineValuesMap.put(softDeclineCode,softDecline.Soft_Decline_Wait_Period__c.intvalue());
}
}
}
public static boolean isHigherEDRecordType(Id recordTypeId) {
String recordTypeName = getRecordTypeName(recordTypeId);
rc_giving__Opportunity_Record_Type_Setting__c setting = rc_giving__Opportunity_Record_Type_Setting__c.getInstance(recordTypeName);
Boolean isHE = false;
if (setting != null && setting.rc_giving__Enable_HE__c == true) {
isHE = true;
}
return isHE;
}
public static Set<String> getHigherEdGivingRecordTypeNameSet(){
Set<String> higherEdGivingRecordTypesSet = new Set<String>();
for(Id recordTypeId : RT_GIVING_BY_ID.keySet()) {
If(isHigherEDRecordType(recordTypeId)) {
higherEdGivingRecordTypesSet.Add(getRecordTypeName(recordTypeId));
}
}
return higherEdGivingRecordTypesSet;
}
public static Id getRecordTypeId(String name) {
return RT_GIVING.get(name) != null ? RT_GIVING.get(name).getRecordTypeId() : null;
}
public static String getRecordTypeName(Id id) {
return RT_GIVING_BY_ID.get(id) != null ? RT_GIVING_BY_ID.get(id).getName() : null;
}
public virtual void execute(Opportunity[] opportunityList, Boolean forceUpdate) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment