public without sharing class ConflictOfInterestHelper {
@AuraEnabled(cacheable=false)
public static Boolean hasConflict(Id recordId) {
// Get the object API name for the record in question.
String objectAPIName = recordId.getSobjectType().getDescribe().getName();
String parentObjectAPI;
// Get the current user.
User currentUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
// Get the custom metadata record for the object.
COI_Object_to_Individual_Field_Ref__mdt objectFieldRef = [SELECT Id, Individual_Field_API_Name__c, Parent_Object_API__c, Direct_Relation_with_Account__c
FROM COI_Object_to_Individual_Field_Ref__mdt
WHERE Object_API_Name__c = :objectAPIName LIMIT 1];
System.debug('DirectRelation:' + objectFieldRef.Direct_Relation_with_Account__c);
// Flag to track if a conflict is found
Boolean hasConflict = false;
// If Direct_Relation_with_Account__c is true, get the IndividualId and check conflicts
if (objectFieldRef.Direct_Relation_with_Account__c) {
// Query the record to get the individual field value
SObject record = Database.query('SELECT ' + objectFieldRef.Individual_Field_API_Name__c + ' FROM ' + objectAPIName + ' WHERE Id = :recordId LIMIT 1');
System.debug('record' + record);
// Get the associated Individual ID (this should be the value of the field specified in Individual_Field_API_Name__c)
String individualId = String.valueOf(record.get(objectFieldRef.Individual_Field_API_Name__c));
System.debug('IndividualId: ' + individualId);
// Check for conflict for the individual
List<Conflict_of_Interest__c> conflictOfInterest = [SELECT Id FROM Conflict_of_Interest__c WHERE User__c = :currentUser.Id AND Individual__c = :individualId
AND Active__c = true];
// If conflict found, set hasConflict to true and create conflict violation async
if (conflictOfInterest.size() > 0) {
hasConflict = true;
createConflictViolationAsync(recordId, conflictOfInterest[0].Id);
}
}
// If Direct_Relation_with_Account__c is false, move to the next metadata record and check parentRecordId
else if (!objectFieldRef.Direct_Relation_with_Account__c) {
parentObjectAPI = objectFieldRef.Parent_Object_API__c;
System.debug('parentObjectAPI:' + parentObjectAPI);
// Query to get the parent record's individual field value
SObject record = Database.query('SELECT ' + objectFieldRef.Individual_Field_API_Name__c + ' FROM ' + objectAPIName + ' WHERE Id = :recordId LIMIT 1');
String parentRecordId = (String)record.get(objectFieldRef.Individual_Field_API_Name__c);
System.debug('parentRecordId: ' + parentRecordId);
// Continue with the logic to check the parent record for conflict
hasConflict = hasConflictForParent(parentRecordId, objectFieldRef.Parent_Object_API__c);
}
return hasConflict; // Return true if conflict found, false otherwise
}
// Recursive method to check the parent records for conflicts
private static Boolean hasConflictForParent(Id parentRecordId, String parentObjectAPIName) {
// Get the custom metadata for the parent object
COI_Object_to_Individual_Field_Ref__mdt parentObjectFieldRef = [SELECT Id, Individual_Field_API_Name__c, Parent_Object_API__c, Direct_Relation_with_Account__c
FROM COI_Object_to_Individual_Field_Ref__mdt
WHERE Object_API_Name__c = :parentObjectAPIName LIMIT 1];
// Query to get the parent record's individual field value
SObject parentRecord = Database.query('SELECT ' + parentObjectFieldRef.Individual_Field_API_Name__c + ' FROM ' + parentObjectAPIName + ' WHERE Id = :parentRecordId LIMIT 1');
String individualId = (String)parentRecord.get(parentObjectFieldRef.Individual_Field_API_Name__c);
// Check for conflict for the parent record's individual ID
List<Conflict_of_Interest__c> conflictOfInterest = [SELECT Id FROM Conflict_of_Interest__c WHERE Individual__c = :individualId AND Active__c = true];
// If conflict found, set hasConflict to true and create conflict violation async
if (conflictOfInterest.size() > 0) {
createConflictViolationAsync(parentRecordId, conflictOfInterest[0].Id);
return true;
}
// If no conflict is found, check if there's a further parent object to check
if (parentObjectFieldRef.Parent_Object_API__c != null) {
// Recursively check the next level parent
return hasConflictForParent(individualId, parentObjectFieldRef.Parent_Object_API__c);
}
// No conflict found, return false
return false;
}
@Future
private static void createConflictViolationAsync(Id recordId, Id conflictOfInterestId) {
try {
Conflict_of_Interest_Violation__c violation = new Conflict_of_Interest_Violation__c(
Conflict_of_Interest__c = conflictOfInterestId,
Record_ID__c = recordId,
Object_API_Name__c = recordId.getSobjectType().getDescribe().getName()
);
insert violation;
} catch (Exception e) {
System.debug('Error creating Conflict_of_Interest_Violation__c record: ' + e.getMessage());
}
}
}