|
| 1 | +/** |
| 2 | + * @author Abraham David Lloyd |
| 3 | + * @date February 11th, 2021 |
| 4 | + * |
| 5 | + * @description This class is used to retrieve B2C Commerce customer data and details |
| 6 | + * from custom object definitions. Each customer should also have an associated |
| 7 | + * default customerList. |
| 8 | + */ |
| 9 | +public with sharing class B2CContactAccountManager extends B2CBaseMeta { |
| 10 | + |
| 11 | + /** |
| 12 | + * @description Attempts to retrieve a Contact configured via custom objects. |
| 13 | + * |
| 14 | + * @param contactId {String} Describes the Contact identifier used to retrieve a given definition |
| 15 | + * @param returnEmptyObject {Boolean} Describes if an empty sObject should be returned if no results are found |
| 16 | + * @param fieldMappings {List<B2C_Integration_Field_Mappings__mdt>} Represents the fieldMappings |
| 17 | + * @return {Account} Returns an instance of a Contact |
| 18 | + */ |
| 19 | + public static Account getAccountById( |
| 20 | + String accountId, Boolean returnEmptyObject, List<B2C_Integration_Field_Mappings__mdt> fieldMappings |
| 21 | + ) { |
| 22 | + |
| 23 | + // Initialize local variables |
| 24 | + List<Account> accounts; |
| 25 | + String errorMsg; |
| 26 | + Query accountQuery; |
| 27 | + Account output; |
| 28 | + |
| 29 | + // Default the error message |
| 30 | + errorMsg = B2CConstant.buildErrorMessage(B2CConstant.ERRORS_META_ACCOUNTNOTFOUND, accountId); |
| 31 | + |
| 32 | + // Seed the default query structure to leverage |
| 33 | + accountQuery = getDefaultQuery(fieldMappings); |
| 34 | + |
| 35 | + // Define the record limit for the query |
| 36 | + accountQuery.setLimit(1); |
| 37 | + |
| 38 | + // Define the default where-clause for the query |
| 39 | + accountQuery.addConditionEq('Id', accountId); |
| 40 | + |
| 41 | + // Execute the query and evaluate the results |
| 42 | + accounts = accountQuery.run(); |
| 43 | + |
| 44 | + // Process the return results in a consistent manner |
| 45 | + output = (Account)processReturnResult('Account', returnEmptyObject, accounts, errorMsg); |
| 46 | + |
| 47 | + // Return the customerList result |
| 48 | + return output; |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @description Helper function that takes an existing contact, and fieldMappings -- and creates an |
| 54 | + * object representation only containing mapped B2C Commerce properties that can be updated via the |
| 55 | + * OCAPI Data REST API. |
| 56 | + * |
| 57 | + * @param customerProfile {Account} Represents the account being processed for B2C Commerce updates |
| 58 | + * @param fieldMappings {List<B2C_Integration_Field_Mappings__mdt>} Represents the collection of |
| 59 | + * fieldMappings being evaluated |
| 60 | + * @return {Map<String, Object>} Returns an object representation of the properties to update |
| 61 | + */ |
| 62 | + public static Map<String, Object> getPublishProfile( |
| 63 | + Account customerProfile, List<B2C_Integration_Field_Mappings__mdt> fieldMappings, Map<String, Object> contactBasedMap |
| 64 | + ) { |
| 65 | + |
| 66 | + // Initialize local variables |
| 67 | + Map<String, Object> output; |
| 68 | + List<String> deleteNode; |
| 69 | + Object accountPropertyValue; |
| 70 | + String oCAPISubKey; |
| 71 | + |
| 72 | + // Initialize the output map |
| 73 | + output = contactBasedMap.clone(); |
| 74 | + deleteNode = new List<String>(); |
| 75 | + |
| 76 | + // Attach the contact and account Ids to the profile |
| 77 | + if (!contactBasedMap.containsKey('c_b2ccrm_contactId')) { |
| 78 | + output.put('c_b2ccrm_contactId', customerProfile.PersonContactId); |
| 79 | + } |
| 80 | + if (!contactBasedMap.containsKey('c_b2ccrm_accountId')) { |
| 81 | + output.put('c_b2ccrm_accountId', customerProfile.Id); |
| 82 | + } |
| 83 | + |
| 84 | + // Loop over the collection of field mappings |
| 85 | + for (B2C_Integration_Field_Mappings__mdt thisFieldMapping: fieldMappings) { |
| 86 | + // Ensure contact-based mapping has priority on account-based fields |
| 87 | + if (output.containsKey(thisFieldMapping.B2C_Commerce_OCAPI_Attribute__c)) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + // Create a reference to the property value for this contact |
| 92 | + accountPropertyValue = customerProfile.get(thisFieldMapping.Service_Cloud_Attribute__c); |
| 93 | + |
| 94 | + // Is this property empty and is this not a child node? If so, then add it to the delete node |
| 95 | + if (accountPropertyValue == null && !thisFieldMapping.B2C_Commerce_OCAPI_Attribute__c.contains('.')) { |
| 96 | + |
| 97 | + // If so, then add it to the delete node (fields to clear out) |
| 98 | + deleteNode.add(thisFieldMapping.B2C_Commerce_OCAPI_Attribute__c); |
| 99 | + |
| 100 | + } else { |
| 101 | + |
| 102 | + // Otherwise, attach the OCAPI property value to the object root |
| 103 | + output.put(thisFieldMapping.B2C_Commerce_OCAPI_Attribute__c, accountPropertyValue); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + // Do we have properties to delete? If so, then include it in the output |
| 110 | + if (deleteNode.size() > 0) { |
| 111 | + if (!output.containsKey('_delete')) { |
| 112 | + output.put('_delete', new List<String>()); |
| 113 | + } |
| 114 | + |
| 115 | + ((List<String>)output.get('_delete')).addAll(deleteNode); |
| 116 | + } |
| 117 | + |
| 118 | + // Returns the output collection |
| 119 | + return output; |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @description Helper method that provides a consistent set of columns to leverage |
| 125 | + * when selecting sObject data via SOQL |
| 126 | + * |
| 127 | + * @param fieldMappings {List<B2C_Integration_Field_Mappings__mdt>} Represents the fieldMappings |
| 128 | + * @return {Query} Returns the query template to leverage for customerLists |
| 129 | + */ |
| 130 | + private static Query getDefaultQuery(List<B2C_Integration_Field_Mappings__mdt> fieldMappings) { |
| 131 | + |
| 132 | + // Initialize local variables |
| 133 | + Query accountQuery; |
| 134 | + |
| 135 | + // Create the profile query that will be used to drive resolution |
| 136 | + accountQuery = new Query('Account'); |
| 137 | + |
| 138 | + // Add the base fields to retrieve (identifiers first) |
| 139 | + accountQuery.selectField('Id'); |
| 140 | + |
| 141 | + // Iterate over the field mappings and attach the mapped fields to the query |
| 142 | + for (B2C_Integration_Field_Mappings__mdt thisFieldMapping: fieldMappings) { |
| 143 | + |
| 144 | + // Add the Salesforce Platform attribute to the query |
| 145 | + accountQuery.selectField(thisFieldMapping.Service_Cloud_Attribute__c); |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + // Return the default query structure |
| 150 | + return accountQuery; |
| 151 | + } |
| 152 | +} |
0 commit comments