fork download
  1. public without sharing class ConflictOfInterestHelper {
  2. @AuraEnabled(cacheable=false)
  3. public static Boolean hasConflict(Id recordId) {
  4. // Get the object API name for the record in question.
  5. String objectAPIName = recordId.getSobjectType().getDescribe().getName();
  6. String parentObjectAPI;
  7.  
  8. // Get the current user.
  9. User currentUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
  10.  
  11. // Get the custom metadata record for the object.
  12. COI_Object_to_Individual_Field_Ref__mdt objectFieldRef = [SELECT Id, Individual_Field_API_Name__c, Parent_Object_API__c, Direct_Relation_with_Account__c
  13. FROM COI_Object_to_Individual_Field_Ref__mdt
  14. WHERE Object_API_Name__c = :objectAPIName LIMIT 1];
  15.  
  16. System.debug('DirectRelation:' + objectFieldRef.Direct_Relation_with_Account__c);
  17.  
  18. // Flag to track if a conflict is found
  19. Boolean hasConflict = false;
  20.  
  21. // If Direct_Relation_with_Account__c is true, get the IndividualId and check conflicts
  22. if (objectFieldRef.Direct_Relation_with_Account__c) {
  23. // Query the record to get the individual field value
  24. SObject record = Database.query('SELECT ' + objectFieldRef.Individual_Field_API_Name__c + ' FROM ' + objectAPIName + ' WHERE Id = :recordId LIMIT 1');
  25. System.debug('record' + record);
  26.  
  27. // Get the associated Individual ID (this should be the value of the field specified in Individual_Field_API_Name__c)
  28. String individualId = String.valueOf(record.get(objectFieldRef.Individual_Field_API_Name__c));
  29. System.debug('IndividualId: ' + individualId);
  30.  
  31. // Check for conflict for the individual
  32. List<Conflict_of_Interest__c> conflictOfInterest = [SELECT Id FROM Conflict_of_Interest__c WHERE User__c = :currentUser.Id AND Individual__c = :individualId
  33. AND Active__c = true];
  34.  
  35. // If conflict found, set hasConflict to true and create conflict violation async
  36. if (conflictOfInterest.size() > 0) {
  37. hasConflict = true;
  38. createConflictViolationAsync(recordId, conflictOfInterest[0].Id);
  39. }
  40. }
  41.  
  42. // If Direct_Relation_with_Account__c is false, move to the next metadata record and check parentRecordId
  43. else if (!objectFieldRef.Direct_Relation_with_Account__c) {
  44. parentObjectAPI = objectFieldRef.Parent_Object_API__c;
  45. System.debug('parentObjectAPI:' + parentObjectAPI);
  46.  
  47. // Query to get the parent record's individual field value
  48. SObject record = Database.query('SELECT ' + objectFieldRef.Individual_Field_API_Name__c + ' FROM ' + objectAPIName + ' WHERE Id = :recordId LIMIT 1');
  49. String parentRecordId = (String)record.get(objectFieldRef.Individual_Field_API_Name__c);
  50. System.debug('parentRecordId: ' + parentRecordId);
  51.  
  52. // Continue with the logic to check the parent record for conflict
  53. hasConflict = hasConflictForParent(parentRecordId, objectFieldRef.Parent_Object_API__c);
  54. }
  55.  
  56. return hasConflict; // Return true if conflict found, false otherwise
  57. }
  58.  
  59. // Recursive method to check the parent records for conflicts
  60. private static Boolean hasConflictForParent(Id parentRecordId, String parentObjectAPIName) {
  61. // Get the custom metadata for the parent object
  62. COI_Object_to_Individual_Field_Ref__mdt parentObjectFieldRef = [SELECT Id, Individual_Field_API_Name__c, Parent_Object_API__c, Direct_Relation_with_Account__c
  63. FROM COI_Object_to_Individual_Field_Ref__mdt
  64. WHERE Object_API_Name__c = :parentObjectAPIName LIMIT 1];
  65.  
  66. // Query to get the parent record's individual field value
  67. SObject parentRecord = Database.query('SELECT ' + parentObjectFieldRef.Individual_Field_API_Name__c + ' FROM ' + parentObjectAPIName + ' WHERE Id = :parentRecordId LIMIT 1');
  68. String individualId = (String)parentRecord.get(parentObjectFieldRef.Individual_Field_API_Name__c);
  69.  
  70. // Check for conflict for the parent record's individual ID
  71. List<Conflict_of_Interest__c> conflictOfInterest = [SELECT Id FROM Conflict_of_Interest__c WHERE Individual__c = :individualId AND Active__c = true];
  72.  
  73. // If conflict found, set hasConflict to true and create conflict violation async
  74. if (conflictOfInterest.size() > 0) {
  75. createConflictViolationAsync(parentRecordId, conflictOfInterest[0].Id);
  76. return true;
  77. }
  78.  
  79. // If no conflict is found, check if there's a further parent object to check
  80. if (parentObjectFieldRef.Parent_Object_API__c != null) {
  81. // Recursively check the next level parent
  82. return hasConflictForParent(individualId, parentObjectFieldRef.Parent_Object_API__c);
  83. }
  84.  
  85. // No conflict found, return false
  86. return false;
  87. }
  88.  
  89. @Future
  90. private static void createConflictViolationAsync(Id recordId, Id conflictOfInterestId) {
  91. try {
  92. Conflict_of_Interest_Violation__c violation = new Conflict_of_Interest_Violation__c(
  93. Conflict_of_Interest__c = conflictOfInterestId,
  94. Record_ID__c = recordId,
  95. Object_API_Name__c = recordId.getSobjectType().getDescribe().getName()
  96. );
  97. insert violation;
  98. } catch (Exception e) {
  99. System.debug('Error creating Conflict_of_Interest_Violation__c record: ' + e.getMessage());
  100. }
  101. }
  102. }
  103.  
Success #stdin #stdout #stderr 0.01s 10492KB
stdin
Standard input is empty
stdout
Object: nil error: did not understand #without
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #without (SysExcept.st:1448)
UndefinedObject>>executeStatements (prog:1)
Object: nil error: did not understand #associationAt:ifAbsent:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #associationAt:ifAbsent: (SysExcept.st:1448)
DeferredVariableBinding>>resolvePathFrom: (DeferBinding.st:115)
DeferredVariableBinding>>value (DeferBinding.st:69)
UndefinedObject>>executeStatements (prog:43)
stderr
./prog:2: parse error, expected '}'
./prog:43: expected expression
./prog:80: expected expression