0% found this document useful (0 votes)
9 views13 pages

New 3

The class UpdateAccountToMatchBilling is designed to update account information based on changes to the NCC number, ensuring that accounts reflect the correct billing entity details. It includes methods for retrieving billing entities, creating status values, processing account updates, and handling debtor updates. Additionally, it manages the relationship between billing codes and accounts, ensuring data consistency and integrity during updates.

Uploaded by

abhishek kale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

New 3

The class UpdateAccountToMatchBilling is designed to update account information based on changes to the NCC number, ensuring that accounts reflect the correct billing entity details. It includes methods for retrieving billing entities, creating status values, processing account updates, and handling debtor updates. Additionally, it manages the relationship between billing codes and accounts, ensuring data consistency and integrity during updates.

Uploaded by

abhishek kale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

/***********************************************************************

* Name of class: UpdateAccountToMatchBilling


* Purpose: the purpose of this class is to update an account if the account's NCC
number has been changed. The account will
* be updated to match the billing entity.
*********************
*********************

Author || Date || Change || Description of change


C.Charlton July 2014 Initial Development

************************************************************************/

public with sharing class UpdateAccountToMatchBilling{

private class updateAccountToMatchBillingException extends Exception {}

/*****************************************
* Purpose: to retrieve a list of billing entities
* Parameters: set of account ids
* Returns: list of billing entities
* Exceptions: N/A
*******************************************/

private List<Legal_entities__c> getBillingEntities (Set<String> accountIds) {

List<Legal_entities__c> billingObjects = new List<Legal_entities__c>();

if (accountIds != null) {

billingObjects = [Select id, Name, NCC__c, RegAddrCity__c,


RegAddrCountry__c, RegAddrCounty__c, RegAddrLine_1__c,
RegAddrLine_2__c, RegAddrLine_3__c,
RegAddrLine_4__c,

RegAddrLine_5__c,RegAddrState__c,RegAddrZip__c,
Parent__c,
Status_Miro__c, Airline_Tier__c,
Cust_Alpha_code__c, Membership__c,
RegAddrCountry__r.ISO_Country_Code__c,
Frozen_SITA_Membership__c
FROM Legal_entities__c
WHERE Parent__c In :accountIds
LIMIT 20000];
}

return billingObjects;
}

/*****************************************
* Purpose:
* Parameters: list of billing entities
* Returns: N/A (void)
* Exceptions: N/A
*******************************************/

private Map<string, Legal_entities__c> createBillingCodeMap


(List<Legal_entities__c> billingCodes) {
Map<string, Legal_entities__c> billingObjectsMap = new Map<string,
Legal_entities__c>();

if (billingCodes != null) {

billingObjectsMap = new Map<string, Legal_entities__c>();

for(Legal_entities__c billingCode : billingCodes) {


billingObjectsMap.put(billingCode.NCC__c, billingCode);
}

return billingObjectsMap;
}

/*****************************************
* Purpose:
* Parameters:
* Returns: N/A (void)
* Exceptions: N/A
*******************************************/

public Map<string, string> createStatusValues (List<Legal_entities__c>


billingCodes) {

Map<string, string> accountBillingStatus = new Map<string, string>();

if (billingCodes != null) {

accountBillingStatus = new Map<string, string>();

for(Legal_entities__c billingCode : billingCodes) {

String currentStatus;

if(billingCode.Status_Miro__c == 'Active') {
accountBillingStatus.put(billingCode.parent__c,
billingCode.Status_Miro__c);
}

if(billingCode.Status_Miro__c == 'Suspended') {

currentStatus =
accountBillingStatus.get(billingCode.parent__c);

if(currentStatus != 'Active') {
accountBillingStatus.put(billingCode.parent__c,
billingCode.Status_Miro__c);
}

}
if(billingCode.Status_Miro__c == 'Dead') {

currentStatus =
accountBillingStatus.get(billingCode.parent__c);

if(currentStatus != 'Active' && currentStatus != 'Suspended') {


accountBillingStatus.put(billingCode.parent__c,
billingCode.Status_Miro__c);
}
}
}
}

return accountBillingStatus;
}

/*****************************************
* Purpose: to retrieve a map of accounts
* Parameters: set of account ids
* Returns: map of accounts
* Exceptions: N/A
*********************************************/

private Map<Id, Account> getAccounts (Set<String> accountIds) {

Map<Id, Account> accountMap = new Map<Id, Account>();

if(accountIds != null) {

// ADO-953334 -Technical debt - Destroy Account field TBD - Country


Code(RegAddrCountry_Code__c)--July 2023-Abhishek Kale
accountMap = new Map<Id, Account>([SELECT id, RegAddrCity__c, NCC__c,
RegAddrCountry__c, HDQ_County__c,
RegAddrLine_1__c, RegAddrLine_2__c,
RegAddrLine_3__c, RegAddrLine_4__c,
Reg_addr_line_5_del__c, RegAddrState__c,
RegAddrZip__c,
Account_status__c, SITA_Member__c,
Cust_Alpha_code__c,
Country__c, Frozen_SITA_Membership__c
FROM Account
WHERE id IN :accountIds
LIMIT 20000]);

return accountMap;
}

/*****************************************
* Purpose: to process the accounts field updates to match the new NCC code.
* Parameters: A list of accounts and a map of the old account provided by a
trigger. i.e. Trigger.new and Trigger.oldmap
* Returns: N/A
* Exceptions: N/A
*******************************************/

public void processAccountBillingUpdates(List<Account> accounts, Map<id,


Account> beforeUpdateAccountsData) {

//create initial fields


Set<String> nccIds = new Set<String>();
Set<String> accountIds = new Set<String>();
List<Account> changedAccounts = new List<Account>();
id prospectRecordType;

try {
prospectRecordType = [Select Id
From RecordType
where DeveloperName = 'Prospect' AND
sObjectType = 'Account'].id;
}
catch (exception e) {
return;
}

if(accounts != null) {

//add the account NCCs or Account id to a list if the NCC on the


account has changed.
for(Account acc : accounts) {
//retrieve old account
Account preAccData = beforeUpdateAccountsData.get(acc.id);
//check account ncc has changed?
if(acc.NCC__c != preAccData.NCC__c) {
if(acc.NCC__c != null && acc.recordTypeId !=
prospectRecordType) {
changedAccounts.add(acc);
nccIds.add(acc.NCC__c);
accountIds.add(acc.id);
}
}
}
//retrieve all of the billing entities required to update the accounts.
if(nccIds != null) {

List<Legal_entities__c> billingCodesListForValues = new


List<Legal_entities__c>();
Map<string, Legal_entities__c> billingObjectsMap = new Map<string,
Legal_entities__c>();
Map<string, string> accountBillingStatus = new Map<string,
string>();

billingCodesListForValues = getBillingEntities(accountIds);
billingObjectsMap =
createBillingCodeMap(billingCodesListForValues);
accountBillingStatus =
createStatusValues(billingCodesListForValues);

//make the account changes to the accounts which have changed.


for(Account acc : changedAccounts) {

//retrieves the billing entity related to the account


Legal_entities__c billObj = billingObjectsMap.get(acc.NCC__c);

//checks if the billing object is null or checks the billing


parent matches the account id,
//if either criteria is met an error is triggered.
if(billObj == null || billObj.Parent__c != acc.id) {

acc.addError(system.label.UpdateAccountToMatchBilling_processAccountBillingUpdates_
NCCError);
}

//if the above criteria is not met, perform the account update.
if (billObj != null && billObj.Parent__c == acc.id) {
// acc = updateAccountWithBillingData(acc, billObj);

// acc.RegAddrCountry_Code__c =
billObj.RegAddrCountry__r.ISO_Country_Code__c;

if(accountBillingStatus.get(acc.id) != null) {
acc.Account_status__c =
accountBillingStatus.get(acc.id);

}
else{acc.Account_status__c = null;}
}
}
}
}
}

/*****************************************
* Purpose: to update the account status value
* Parameters: A list of billing codes and a map of the old billing codes
provided by a trigger. i.e. Trigger.new and Trigger.oldmap
* Returns: N/A
* Exceptions: N/A
*******************************************/

public void processAccountStatusUpdate (List<Legal_entities__c>


billingEntities, Map<id, Legal_entities__c> beforeUpdatebillingEntityData) {

if(billingEntities!= null) {

List<Legal_entities__c> billingCodeStatusUpdateList = new


List<Legal_entities__c>();
Set<String> billingCodeParentAccIds = new Set<String>();
List<Account> updatedAccountsList = new List<Account>();
Map<Id, Account> accountMap;

//loop the billing entities to find the account to update.


for(Legal_entities__c billCode : billingEntities) {

Legal_entities__c preBillEntData =
beforeUpdatebillingEntityData.get(billCode.id);

if(billCode.Parent__c != null) {

if (billCode.Status_Miro__c != preBillEntData.Status_Miro__c) {
billingCodeParentAccIds.add(billCode.Parent__c);
billingCodeStatusUpdateList.add(billCode);
}
}
}

if(billingCodeParentAccIds != null) {

accountMap = getAccounts(billingCodeParentAccIds);

Map<string, string> accountBillingStatus = new Map<string,


string>();
accountBillingStatus =
createStatusValues(getBillingEntities(billingCodeParentAccIds));

for(Account acc: accountMap.values()) {

//check to ensure the account is not null


if(acc != null) {

if(accountBillingStatus.get(acc.id) != null) {

acc.Account_status__c =
accountBillingStatus.get(acc.id);

//add the accounts to a list to be updated in the DML


operation below.
updatedAccountsList.add(acc);
}

//update the accounts.


if(updatedAccountsList != null) {
Savepoint sp = Database.setSavepoint();

try {
update updatedAccountsList;
}
catch (exception e) {
Database.rollback(sp);
system.debug('Account Status failed to update');

}
}
}
}
}

/*****************************************
* Purpose: to process the accounts field updates to match the new NCC code.
* Parameters: A list of accounts and a map of the old account provided by a
trigger. i.e. Trigger.new and Trigger.oldmap
* Returns: N/A
* Exceptions: N/A
*******************************************/

/*****************************************
* Purpose: to retrieve the debtor values from a CSV file and return those
values in a map.
* Parameters: N/A
* Returns: Map of String, Strings
* Exceptions: custom exception which triggers if the code fails to retrieve the
debtor document.
*******************************************/

private Map<String, String> retrieveDebtorValues() {

Document debtors;
Map<String, String> debtorMap;

try {
String debtorFileName =
InternalApplicationSettings__c.getValues('DebtorFileName').Application_Setting__c;
debtors = [Select id, body From Document Where
DeveloperName=:debtorFileName limit 1];
}
catch (exception e) {
throw new updateAccountToMatchBillingException('Your batch job -
retrieveDebtorValues failed : ' + e.getMessage());
}

if (debtors != null) {
List<String> debtorString = debtors.Body.toString().split('\n');
debtorMap = new Map<String, String>();

for (String str : debtorString) {


debtorMap.put(str.split(',')[0].trim(), str.split(',')[1].trim());
}
}

return debtorMap;

/*****************************************
* Purpose: to process the debtor updates in the billing codes and accounts.
* Parameters: N/A
* Returns: N/A
* Exceptions: custom exception which triggers if the code fails to update the
accounts or biiling codes.
*******************************************/

public void processDebtorUpdate() {

Map<String, String> debtorMap = retrieveDebtorValues();

List<Legal_entities__c> parentCodesToUpdate = [Select id, NCC__c,


Debtor__c, Parent__c
From Legal_entities__c
Where NCC__c IN :
debtorMap.KeySet() Limit 10000];

List<Legal_entities__c> childCodesToUpdate = [Select id,


Parent_NCC_code__c, Debtor__c
From Legal_entities__c
Where Parent_NCC_code__c IN :
debtorMap.KeySet() Limit 10000];
Set<Legal_entities__c> bCodeupdatesSet = new Set<Legal_entities__c>();

Set<Account> accUpdatesSet = new Set<Account>();

for (Legal_entities__c bCode : parentCodesToUpdate) {

bCode.Debtor__c = boolean.valueOf(debtorMap.get(bCode.NCC__c));
bCodeupdatesSet.add(bCode);
//fix here - duplicated account due to looping bcodes which share the same
account!!
Account acc = new Account(id=bCode.Parent__c);
acc.Debt_Current_Debt_Status__c =
boolean.valueOf(debtorMap.get(bCode.NCC__c));
accUpdatesSet.add(acc);
}

for (Legal_entities__c bCode : childCodesToUpdate) {

bCode.Debtor__c =
boolean.valueOf(debtorMap.get(bCode.Parent_NCC_code__c));
bCodeupdatesSet.add(bCode);
}

List<Legal_entities__c> bCodeupdatesList = new


List<Legal_entities__c>(bCodeupdatesSet);
List<Account> accUpdatesList = new List<Account>(accUpdatesSet);

Savepoint sp = Database.setSavepoint();

try {
update bCodeupdatesList;
update accUpdatesList;
}
catch (exception e) {
Database.rollback(sp);
throw new updateAccountToMatchBillingException('Your batch job-
processDebtorUpdate failed : ' + e.getMessage());
}

/*****************************************
* Purpose: set the billing code GEO / territory values
* Parameters: list of billing codes
* Returns: N/A
* Exceptions:
*******************************************/

public void processChildBillingCodeGEOTerrValues(List<Legal_entities__c>


billingEntities) {

if(billingEntities!= null) {

List<Legal_entities__c> childBcList = new List<Legal_entities__c>();


Set<String> childParentNCCs = new Set<String>();

for (Legal_entities__c bc : billingEntities) {


if (bc.Parent_NCC_code__c != null && bc.Parent_NCC_code__c != '') {

childParentNCCs.add(bc.Parent_NCC_code__c);
childBcList.add(bc);

if ((childBcList != null && !childBcList.isEmpty()) && (childParentNCCs


!= null && !childParentNCCs.isEmpty()) ) {

List<Legal_entities__c> parentBcList = [Select id, NCC__c,


RegAddrCountry__c From Legal_entities__c Where NCC__c IN :childParentNCCs limit
10000];
Map<String, Legal_entities__c> parentBcMap = new Map<String,
Legal_entities__c>();

for (Legal_entities__c parent : parentBcList) {


parentBcMap.put(parent.NCC__c, parent);
}

for (Legal_entities__c child : childBcList) {

if (parentBcMap.get(child.Parent_NCC_code__c) != null) {

if
(parentBcMap.get(child.Parent_NCC_code__c).RegAddrCountry__c != null) {

child.Parent_Code_Country__c =
parentBcMap.get(child.Parent_NCC_code__c).RegAddrCountry__c;
}
}
}

/*****************************************
* Purpose: overloads the initial method for an update version which compares
the parent NCC code values, if the parent NCC code changes it updates the child
with the new parent ncc code country.
* Parameters: list of billing codes and Map of the old version of the billing
codes.
* Returns: N/A
* Exceptions:
*******************************************/

public void processChildBillingCodeGEOTerrValues(List<Legal_entities__c>


billingEntities, Map<id, Legal_entities__c> beforeUpdatebillingEntityData) {

if (billingEntities != null && beforeUpdatebillingEntityData != null) {

List<Legal_entities__c> parentNCCChangeList = new


List<Legal_entities__c>();
for (Legal_entities__c bc : billingEntities) {

Legal_entities__c oldBc = beforeUpdatebillingEntityData.get(bc.id);

if (bc.Parent_NCC_Code__c != oldBc.Parent_NCC_Code__c) {

parentNCCChangeList.add(bc);

if(parentNCCChangeList != null && !parentNCCChangeList.isEmpty()) {


processChildBillingCodeGEOTerrValues(parentNCCChangeList);
}

/*****************************************
* Purpose: set the child billing code GEO / territory values when the parent
billing codes country changes.
* Parameters: list of billing codes and Map of the old version of the billing
codes.
* Returns: N/A
* Exceptions:
*******************************************/

public void pushChildBillingCodeGEOTerrValues(Map<id, Legal_entities__c>


billingEntities, Map<id, Legal_entities__c> beforeUpdatebillingEntityData) {

if(billingEntities!= null && beforeUpdatebillingEntityData != null) {

Map<String, Legal_entities__c> parentBcMap = new Map<String,


Legal_entities__c>();

for (Legal_entities__c bc : billingEntities.values()) {

Legal_entities__c oldBc = beforeUpdatebillingEntityData.get(bc.id);

if (bc.RegAddrCountry__c != oldBc.RegAddrCountry__c) {

parentBcMap.put(bc.NCC__c, bc);

if (parentBcMap != null && !parentBcMap.isEmpty()) {

List<Legal_entities__c> childBcList = [Select id,


Parent_NCC_Code__c, NCC__c, RegAddrCountry__c From Legal_entities__c Where
Parent_NCC_Code__c IN :parentBcMap.keySet() limit 10000];

for (Legal_entities__c child : childBcList) {

if (parentBcMap.get(child.Parent_NCC_code__c) != null) {
if
(parentBcMap.get(child.Parent_NCC_code__c).RegAddrCountry__c != null) {
child.Parent_Code_Country__c =
parentBcMap.get(child.Parent_NCC_code__c).RegAddrCountry__c;
}
}

update childBcList;

public void caseAssign(List<Id> caseIds)


{
Database.DMLOptions dmopt = new Database.DMLOptions();
dmopt.assignmentRuleHeader.useDefaultRule= true;
Case caseObj=[select id from Case where id in :caseIds];
caseObj.setOptions(dmopt);
update caseObj;
}

public void createAndUpdateRecords() {


List<Account> accounts = new List<Account>();
List<Legal_Entities__c> legalEntities = new List<Legal_Entities__c>();
List<Contact> contacts = new List<Contact>();

// Create Account records


for (Integer i = 0; i < 5; i++) {
Account acc = new Account(Name = 'Test Account ' + i,RegAddrCountry__c
='a0E2600000kwM3kEAE');
accounts.add(acc);
}

// Create Legal Entity records


for (Integer i = 0; i < 3; i++) {
Legal_Entities__c legalEntity = new Legal_Entities__c(Name = 'Test
Legal Entity ' + i);
legalEntities.add(legalEntity);
}

// Create Contact records associated with Accounts


for (Account acc : accounts) {
Contact con = new Contact(AccountId = acc.Id, FirstName = 'Test',
LastName = 'Contact');
contacts.add(con);
}

// Insert records
insert accounts;
insert legalEntities;
insert contacts;

// Update Account records


for (Account acc : accounts) {
acc.Description = 'Updated Description';
}
update accounts;

// Update Legal Entity records


for (Legal_Entities__c legalEntity : legalEntities) {
legalEntity.Name = 'Updated Description';
}
update legalEntities;

// Update Contact records


for (Contact con : contacts) {
con.Email = 'updated@example.com';
}
update contacts;
}
public void createContact(String firstName, String lastName, String email) {
Contact newContact = new Contact();
newContact.FirstName = firstName;
newContact.LastName = lastName;
newContact.Email = email;

try {
insert newContact;
System.debug('Contact created successfully. Contact ID: ' +
newContact.Id);
} catch (Exception e) {
System.debug('Error creating contact: ' + e.getMessage());
}
}

public void updateContact(Id contactId, String newFirstName, String


newLastName, String newEmail) {
Contact contactToUpdate = [SELECT Id, FirstName, LastName, Email FROM
Contact WHERE Id = :contactId LIMIT 1];

if (contactToUpdate != null) {
contactToUpdate.FirstName = newFirstName;
contactToUpdate.LastName = newLastName;
contactToUpdate.Email = newEmail;

try {
update contactToUpdate;
System.debug('Contact updated successfully. Contact ID: ' +
contactToUpdate.Id);
} catch (Exception e) {
System.debug('Error updating contact: ' + e.getMessage());
}
} else {
System.debug('Contact not found with ID: ' + contactId);
}
}
public void createSampleData() {
List<Account> accounts = new List<Account>();
List<Legal_Entities__c> legalEntities = new List<Legal_Entities__c>();

for (Integer i = 0; i < 20; i++) {


Account acc = new Account(Name = 'Account ' + i,RegAddrCountry__c
='a0E2600000kwM3kEAE');
accounts.add(acc);
Legal_Entities__c legalEntity = new Legal_Entities__c(Name = 'Legal
Entity ' + i);
legalEntities.add(legalEntity);
}

insert accounts;
insert legalEntities;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy