Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @description Location (CommercientSF8__SAGEX3_Address__c object) Trigger Handler class
- * @author John Lawrence Catan
- * @Company Simplus
- * @date 07.MAR.2018
- */
- public class LocationTriggerHandler {
- public void onAfterInsert(List<CommercientSF8__SAGEX3_Address__c> LocList){
- List<CommercientSF8__SAGEX3_Address__c> ShipIsTrueList = new List<CommercientSF8__SAGEX3_Address__c>();
- //Loop through the Location to check if ShipToAddress__c == TRUE
- for(CommercientSF8__SAGEX3_Address__c loc : LocList){
- if(loc.ShipToAddress__c == TRUE){
- ShipIsTrueList.add(loc);
- }
- }
- if(!ShipIsTrueList.isEmpty()){
- CreateShipping(ShipIsTrueList);
- }
- }
- public void onAfterUpdate(List<CommercientSF8__SAGEX3_Address__c> LocList, Map<Id,CommercientSF8__SAGEX3_Address__c> oldLocationMap){
- List<CommercientSF8__SAGEX3_Address__c> ShipIsTrueList = new List<CommercientSF8__SAGEX3_Address__c>();
- //Loop through the Location to check if ShipToAddress__c == TRUE
- for(CommercientSF8__SAGEX3_Address__c loc : LocList){
- if(loc.ShipToAddress__c == TRUE){
- ShipIsTrueList.add(loc);
- }
- }
- if(!ShipIsTrueList.isEmpty()){
- UpdateShipping(ShipIsTrueList, oldLocationMap);
- }
- }
- /*
- Method to create an Account of record type "Shipping" whenever a Location record is created
- */
- public static void CreateShipping(List<CommercientSF8__SAGEX3_Address__c> LocList){
- //Get the Shipping Account record type ID
- Id ShippingTypeID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Shipping Account').getRecordTypeId();
- Set<Id> LocAcctIds = new Set<Id>();
- //List to hold created accounts to be inserted after
- List<Account> toInsertAccounts = new List<Account>();
- //Get the Location's Accounts
- for(CommercientSF8__SAGEX3_Address__c loc : LocList){
- LocAcctIds.add(loc.CommercientSF8__Account__c);
- }
- Map<Id, Account> LocAcctNameMap = new Map<Id, Account>([select Id, Name from Account where Id in :LocAcctIds]);
- //System_ID__c SysId = System_ID__c.getValues('System Id');
- Location_Setting__mdt SysId = [Select DeveloperName, MasterLabel, System_User_Id__c from Location_Setting__mdt];
- //Loop through the Location record
- for(CommercientSF8__SAGEX3_Address__c loc : LocList){
- //Create the Account with record type Shipping
- Account acc = new Account();
- acc.RecordTypeId = ShippingTypeID;
- //acc.Name = loc.CommercientSF8__Title__c + '-' + loc.CommercientSF8__Address__c;
- //Modified 03/22/2018 by Lawrence Catan
- //Changed Account name to use Location's account + address
- //Added Carrier, Delivery Mode and Freight Method/Incoterms/FOB fields
- //acc.Name = LocAcctNameMap.get(loc.CommercientSF8__Account__c).Name + ' ' + loc.CommercientSF8__Address__c;
- acc.Name = LocAcctNameMap.get(loc.CommercientSF8__Account__c).Name + ' - ' + loc.CommercientSF8__Address__c;
- acc.Carrier__c = loc.Carrier__c;
- acc.Delivery_Mode__c = loc.Delivery_Mode__c;
- acc.Freight_Method_Incoterms_FOB__c = loc.Freight_Method_Incoterms_FOB__c;
- acc.ParentId = loc.CommercientSF8__Account__c;
- //Modified 03/23/2018 by Lawrence Catan
- //Changed Owner ID to use ID from Custom Settings : System ID
- acc.OwnerID = SysId.System_User_Id__c;
- acc.Billing_Account__c = loc.CommercientSF8__Account__c;
- //Assign Location's Address fields to the Account Shipping Address
- //acc.ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + loc.CommercientSF8__AdrLin2__c + ' ' + loc.CommercientSF8__AdrLin3__c;
- if(loc.CommercientSF8__AdrLin1__c != NULL){
- acc.ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ';
- } else if(loc.CommercientSF8__AdrLin1__c == NULL){
- acc.ShippingStreet = '';
- }
- if(loc.CommercientSF8__AdrLin2__c != NULL){
- acc.ShippingStreet += loc.CommercientSF8__AdrLin2__c + ' ';
- } else if (loc.CommercientSF8__AdrLin2__c == NULL){
- acc.ShippingStreet += '';
- }
- if(loc.CommercientSF8__AdrLin3__c != NULL){
- acc.ShippingStreet += loc.CommercientSF8__AdrLin3__c;
- } else if (loc.CommercientSF8__AdrLin3__c == NULL){
- acc.ShippingStreet += '';
- }
- acc.ShippingCity = loc.CommercientSF8__City__c;
- acc.ShippingPostalCode = loc.CommercientSF8__PostalCode__c;
- acc.ShippingState = loc.CommercientSF8__County__c;
- acc.ShippingCountry = loc.CommercientSF8__Country__c;
- acc.SAGEX3_Address__c = loc.id;
- System.debug('Street value = ' + acc.ShippingStreet);
- //Checking to input empty string values if the value is NULL
- if(acc.ShippingStreet.contains('null')){
- System.debug('CONTAINS NULL');
- acc.ShippingStreet.replace('null', '');
- System.debug('Street value AFTER = ' + acc.ShippingStreet);
- }
- if(acc.ShippingCity == NULL){
- acc.ShippingCity = '';
- }
- if(acc.ShippingPostalCode == NULL){
- acc.ShippingPostalCode = '';
- }
- if(acc.ShippingState == NULL){
- acc.ShippingState = '';
- }
- if(acc.ShippingCountry == NULL){
- acc.ShippingCountry = '';
- }
- acc.Phone = loc.CommercientSF8__TelePhone1__c;
- toInsertAccounts.add(acc);
- }
- //Check the List if not empty, then insert
- if(!toInsertAccounts.isEmpty()){
- insert toInsertAccounts;
- }
- }
- /*
- Method to update the Shipping Account if any changes are made to the Location records Address
- */
- public static void UpdateShipping(List<CommercientSF8__SAGEX3_Address__c> LocList, Map<Id,CommercientSF8__SAGEX3_Address__c> oldLocationMap){
- Set<Id> AccIDs = new Set<Id>();
- //List to hold updated accounts to be updated after
- List<Account> toUpdateAccounts = new List<Account>();
- //Query the corresponding Shipping Account record from Location
- Map<Id,Account> ShipAcctMap = new Map<Id,Account>();
- //JZ 05-10-18 Need to update this part
- //for(CommercientSF8__SAGEX3_Address__c loc : LocList){
- //Account acct = [SELECT Id, Shipping_Account__c, ShippingStreet, ShippingCity,ShippingPostalCode,ShippingCountry FROM Account WHERE Id =: loc.Shipping_Account__c];
- //ShipAcctMap.put(acct.id, acct);
- //}
- //JZ 05-10-18 Updated
- Set<Id> idLocAccount = new Set<Id>();
- for(CommercientSF8__SAGEX3_Address__c loc : LocList){
- idLocAccount.add(loc.Shipping_Account__c);
- }
- List<Account> accountListLocation = [SELECT Id, Shipping_Account__c, ShippingStreet, ShippingCity,ShippingPostalCode,ShippingCountry FROM Account WHERE Id in:idLocAccount];
- for(Account a:accountListLocation){
- ShipAcctMap.put(a.id, a);
- }
- //Loop through the Location record Address fields via old Map if there are any changes
- for(CommercientSF8__SAGEX3_Address__c loc : LocList) {
- if(oldLocationMap.get(loc.Id).CommercientSF8__AdrLin1__c != loc.CommercientSF8__AdrLin1__c) {
- if(loc.CommercientSF8__AdrLin1__c != NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c;
- } else if(loc.CommercientSF8__AdrLin1__c == NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = '';
- }
- //ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c;
- }
- if(oldLocationMap.get(loc.Id).CommercientSF8__AdrLin2__c != loc.CommercientSF8__AdrLin2__c) {
- if(loc.CommercientSF8__AdrLin2__c != NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + loc.CommercientSF8__AdrLin2__c;
- } else if (loc.CommercientSF8__AdrLin2__c == NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + '';
- }
- //ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + loc.CommercientSF8__AdrLin2__c;
- }
- if(oldLocationMap.get(loc.Id).CommercientSF8__AdrLin3__c != loc.CommercientSF8__AdrLin3__c) {
- if(loc.CommercientSF8__AdrLin3__c != NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + loc.CommercientSF8__AdrLin2__c + ' ' + loc.CommercientSF8__AdrLin3__c;
- } else if (loc.CommercientSF8__AdrLin3__c == NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + loc.CommercientSF8__AdrLin2__c + ' ' + '';
- }
- //ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + loc.CommercientSF8__AdrLin2__c + ' ' + loc.CommercientSF8__AdrLin3__c;
- }
- if(oldLocationMap.get(loc.Id).CommercientSF8__City__c != loc.CommercientSF8__City__c) {
- if(loc.CommercientSF8__City__c != NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingCity = loc.CommercientSF8__City__c;
- } else if(loc.CommercientSF8__City__c == NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingCity = '';
- }
- //ShipAcctMap.get(loc.Shipping_Account__c).ShippingCity = loc.CommercientSF8__City__c;
- }
- if(oldLocationMap.get(loc.Id).CommercientSF8__PostalCode__c != loc.CommercientSF8__PostalCode__c) {
- if(loc.CommercientSF8__PostalCode__c != NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingPostalCode = loc.CommercientSF8__PostalCode__c;
- } else if(loc.CommercientSF8__PostalCode__c == NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingPostalCode = '';
- }
- //ShipAcctMap.get(loc.Shipping_Account__c).ShippingPostalCode = loc.CommercientSF8__PostalCode__c;
- }
- if(oldLocationMap.get(loc.Id).CommercientSF8__County__c != loc.CommercientSF8__County__c) {
- if(loc.CommercientSF8__County__c != NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingState = loc.CommercientSF8__County__c;
- } else if(loc.CommercientSF8__County__c == NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingState = '';
- }
- //ShipAcctMap.get(loc.Shipping_Account__c).ShippingState = loc.CommercientSF8__County__c;
- }
- if(oldLocationMap.get(loc.Id).CommercientSF8__Country__c != loc.CommercientSF8__Country__c) {
- if(loc.CommercientSF8__Country__c != NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingCountry = loc.CommercientSF8__Country__c;
- } else if(loc.CommercientSF8__Country__c == NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).ShippingCountry = '';
- }
- //ShipAcctMap.get(loc.Shipping_Account__c).ShippingCountry = loc.CommercientSF8__Country__c;
- }
- if(oldLocationMap.get(loc.Id).CommercientSF8__TelePhone1__c != loc.CommercientSF8__TelePhone1__c) {
- if(loc.CommercientSF8__TelePhone1__c != NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).Phone = loc.CommercientSF8__TelePhone1__c;
- } else if(loc.CommercientSF8__TelePhone1__c == NULL){
- ShipAcctMap.get(loc.Shipping_Account__c).Phone = '';
- }
- //ShipAcctMap.get(loc.Shipping_Account__c).Phone = loc.CommercientSF8__TelePhone1__c;
- }
- }
- if(!ShipAcctMap.isEmpty()){
- update ShipAcctMap.values();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement