SyncMadeEasy/force-app/main/default/triggers/SmeConfigurationTrigger.tri...

51 lines
3.4 KiB
Plaintext

trigger SmeConfigurationTrigger on HIC_ShopifySync__HIC_Configuration__c (before insert, before update, after insert) {
// ───────────────────────────────────────────────────────────────────────────────────────────
// Existing behavior: assign the Shopify permission set to the record creator on insert.
// ───────────────────────────────────────────────────────────────────────────────────────────
if (Trigger.isAfter && Trigger.isInsert) {
Set<Id> userIds = new Set<Id>();
for (HIC_ShopifySync__HIC_Configuration__c conf : Trigger.new) {
if (conf.Name != null && conf.CreatedById != null) {
userIds.add(conf.CreatedById);
}
}
if (!userIds.isEmpty()) {
SmePermissionSetAssignmentQueueable.assignPermissionSet(userIds);
}
}
// ───────────────────────────────────────────────────────────────────────────────────────────
// Professional-Edition token reset.
// A PE admin cannot reach the Configuration custom setting to clear an expired token (no Dev
// Console / limited setup, even via Grant Login Access). Instead they set
// HIC_ShopifySync__Shopify_Store_Details__c = 'Token' on the 'Default' HIC_Configuration record.
// This trigger then BLANKS Access_Token__c and Refresh_Token__c on the Configuration setting so
// the client can re-initialize, and consumes the flag in-place (sets it back to null) so it can
// never fire again and cannot wipe a freshly re-initialized token.
// ───────────────────────────────────────────────────────────────────────────────────────────
if (Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)) {
Boolean resetRequested = false;
for (HIC_ShopifySync__HIC_Configuration__c conf : Trigger.new) {
if (conf.Name != null && conf.Name.equalsIgnoreCase('Default')
&& conf.HIC_ShopifySync__Shopify_Store_Details__c != null
&& conf.HIC_ShopifySync__Shopify_Store_Details__c.equalsIgnoreCase('Token')) {
resetRequested = true;
conf.HIC_ShopifySync__Shopify_Store_Details__c = null; // consume the flag (in-place, no extra DML)
}
}
if (resetRequested) {
List<HIC_ShopifySync__Configuration__c> cfg = [
SELECT Id
FROM HIC_ShopifySync__Configuration__c
WHERE Name = 'Default'
LIMIT 1
];
if (!cfg.isEmpty()) {
cfg[0].HIC_ShopifySync__Access_Token__c = '';
cfg[0].HIC_ShopifySync__Refresh_Token__c = '';
update cfg;
}
}
}
}