Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.37 KB | None | 0 0
  1. /**
  2. * @description Location (CommercientSF8__SAGEX3_Address__c object) Trigger Handler class
  3. * @author John Lawrence Catan
  4. * @Company Simplus
  5. * @date 07.MAR.2018
  6.  
  7. */
  8.  
  9. public class LocationTriggerHandler {
  10.  
  11. public void onAfterInsert(List<CommercientSF8__SAGEX3_Address__c> LocList){
  12.  
  13. List<CommercientSF8__SAGEX3_Address__c> ShipIsTrueList = new List<CommercientSF8__SAGEX3_Address__c>();
  14.  
  15. //Loop through the Location to check if ShipToAddress__c == TRUE
  16. for(CommercientSF8__SAGEX3_Address__c loc : LocList){
  17. if(loc.ShipToAddress__c == TRUE){
  18. ShipIsTrueList.add(loc);
  19. }
  20. }
  21.  
  22. if(!ShipIsTrueList.isEmpty()){
  23. CreateShipping(ShipIsTrueList);
  24. }
  25. }
  26.  
  27. public void onAfterUpdate(List<CommercientSF8__SAGEX3_Address__c> LocList, Map<Id,CommercientSF8__SAGEX3_Address__c> oldLocationMap){
  28.  
  29. List<CommercientSF8__SAGEX3_Address__c> ShipIsTrueList = new List<CommercientSF8__SAGEX3_Address__c>();
  30.  
  31. //Loop through the Location to check if ShipToAddress__c == TRUE
  32. for(CommercientSF8__SAGEX3_Address__c loc : LocList){
  33. if(loc.ShipToAddress__c == TRUE){
  34. ShipIsTrueList.add(loc);
  35. }
  36. }
  37.  
  38. if(!ShipIsTrueList.isEmpty()){
  39. UpdateShipping(ShipIsTrueList, oldLocationMap);
  40. }
  41. }
  42.  
  43. /*
  44. Method to create an Account of record type "Shipping" whenever a Location record is created
  45. */
  46. public static void CreateShipping(List<CommercientSF8__SAGEX3_Address__c> LocList){
  47.  
  48. //Get the Shipping Account record type ID
  49. Id ShippingTypeID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Shipping Account').getRecordTypeId();
  50.  
  51. Set<Id> LocAcctIds = new Set<Id>();
  52.  
  53. //List to hold created accounts to be inserted after
  54. List<Account> toInsertAccounts = new List<Account>();
  55.  
  56. //Get the Location's Accounts
  57. for(CommercientSF8__SAGEX3_Address__c loc : LocList){
  58. LocAcctIds.add(loc.CommercientSF8__Account__c);
  59. }
  60.  
  61. Map<Id, Account> LocAcctNameMap = new Map<Id, Account>([select Id, Name from Account where Id in :LocAcctIds]);
  62.  
  63. //System_ID__c SysId = System_ID__c.getValues('System Id');
  64. Location_Setting__mdt SysId = [Select DeveloperName, MasterLabel, System_User_Id__c from Location_Setting__mdt];
  65.  
  66. //Loop through the Location record
  67. for(CommercientSF8__SAGEX3_Address__c loc : LocList){
  68.  
  69. //Create the Account with record type Shipping
  70. Account acc = new Account();
  71. acc.RecordTypeId = ShippingTypeID;
  72. //acc.Name = loc.CommercientSF8__Title__c + '-' + loc.CommercientSF8__Address__c;
  73. //Modified 03/22/2018 by Lawrence Catan
  74. //Changed Account name to use Location's account + address
  75. //Added Carrier, Delivery Mode and Freight Method/Incoterms/FOB fields
  76. //acc.Name = LocAcctNameMap.get(loc.CommercientSF8__Account__c).Name + ' ' + loc.CommercientSF8__Address__c;
  77. acc.Name = LocAcctNameMap.get(loc.CommercientSF8__Account__c).Name + ' - ' + loc.CommercientSF8__Address__c;
  78. acc.Carrier__c = loc.Carrier__c;
  79. acc.Delivery_Mode__c = loc.Delivery_Mode__c;
  80. acc.Freight_Method_Incoterms_FOB__c = loc.Freight_Method_Incoterms_FOB__c;
  81. acc.ParentId = loc.CommercientSF8__Account__c;
  82. //Modified 03/23/2018 by Lawrence Catan
  83. //Changed Owner ID to use ID from Custom Settings : System ID
  84. acc.OwnerID = SysId.System_User_Id__c;
  85. acc.Billing_Account__c = loc.CommercientSF8__Account__c;
  86.  
  87. //Assign Location's Address fields to the Account Shipping Address
  88. //acc.ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + loc.CommercientSF8__AdrLin2__c + ' ' + loc.CommercientSF8__AdrLin3__c;
  89.  
  90. if(loc.CommercientSF8__AdrLin1__c != NULL){
  91. acc.ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ';
  92. } else if(loc.CommercientSF8__AdrLin1__c == NULL){
  93. acc.ShippingStreet = '';
  94. }
  95.  
  96. if(loc.CommercientSF8__AdrLin2__c != NULL){
  97. acc.ShippingStreet += loc.CommercientSF8__AdrLin2__c + ' ';
  98. } else if (loc.CommercientSF8__AdrLin2__c == NULL){
  99. acc.ShippingStreet += '';
  100. }
  101.  
  102. if(loc.CommercientSF8__AdrLin3__c != NULL){
  103. acc.ShippingStreet += loc.CommercientSF8__AdrLin3__c;
  104. } else if (loc.CommercientSF8__AdrLin3__c == NULL){
  105. acc.ShippingStreet += '';
  106. }
  107.  
  108. acc.ShippingCity = loc.CommercientSF8__City__c;
  109. acc.ShippingPostalCode = loc.CommercientSF8__PostalCode__c;
  110. acc.ShippingState = loc.CommercientSF8__County__c;
  111. acc.ShippingCountry = loc.CommercientSF8__Country__c;
  112. acc.SAGEX3_Address__c = loc.id;
  113.  
  114. System.debug('Street value = ' + acc.ShippingStreet);
  115.  
  116. //Checking to input empty string values if the value is NULL
  117. if(acc.ShippingStreet.contains('null')){
  118. System.debug('CONTAINS NULL');
  119. acc.ShippingStreet.replace('null', '');
  120.  
  121. System.debug('Street value AFTER = ' + acc.ShippingStreet);
  122. }
  123.  
  124. if(acc.ShippingCity == NULL){
  125. acc.ShippingCity = '';
  126. }
  127.  
  128. if(acc.ShippingPostalCode == NULL){
  129. acc.ShippingPostalCode = '';
  130. }
  131.  
  132. if(acc.ShippingState == NULL){
  133. acc.ShippingState = '';
  134. }
  135.  
  136. if(acc.ShippingCountry == NULL){
  137. acc.ShippingCountry = '';
  138. }
  139.  
  140.  
  141. acc.Phone = loc.CommercientSF8__TelePhone1__c;
  142. toInsertAccounts.add(acc);
  143.  
  144. }
  145.  
  146. //Check the List if not empty, then insert
  147. if(!toInsertAccounts.isEmpty()){
  148. insert toInsertAccounts;
  149. }
  150.  
  151. }
  152.  
  153. /*
  154. Method to update the Shipping Account if any changes are made to the Location records Address
  155. */
  156.  
  157. public static void UpdateShipping(List<CommercientSF8__SAGEX3_Address__c> LocList, Map<Id,CommercientSF8__SAGEX3_Address__c> oldLocationMap){
  158.  
  159. Set<Id> AccIDs = new Set<Id>();
  160.  
  161. //List to hold updated accounts to be updated after
  162. List<Account> toUpdateAccounts = new List<Account>();
  163.  
  164. //Query the corresponding Shipping Account record from Location
  165. Map<Id,Account> ShipAcctMap = new Map<Id,Account>();
  166.  
  167. //JZ 05-10-18 Need to update this part
  168. //for(CommercientSF8__SAGEX3_Address__c loc : LocList){
  169. //Account acct = [SELECT Id, Shipping_Account__c, ShippingStreet, ShippingCity,ShippingPostalCode,ShippingCountry FROM Account WHERE Id =: loc.Shipping_Account__c];
  170. //ShipAcctMap.put(acct.id, acct);
  171. //}
  172. //JZ 05-10-18 Updated
  173. Set<Id> idLocAccount = new Set<Id>();
  174. for(CommercientSF8__SAGEX3_Address__c loc : LocList){
  175. idLocAccount.add(loc.Shipping_Account__c);
  176. }
  177.  
  178. List<Account> accountListLocation = [SELECT Id, Shipping_Account__c, ShippingStreet, ShippingCity,ShippingPostalCode,ShippingCountry FROM Account WHERE Id in:idLocAccount];
  179. for(Account a:accountListLocation){
  180. ShipAcctMap.put(a.id, a);
  181. }
  182.  
  183. //Loop through the Location record Address fields via old Map if there are any changes
  184. for(CommercientSF8__SAGEX3_Address__c loc : LocList) {
  185.  
  186. if(oldLocationMap.get(loc.Id).CommercientSF8__AdrLin1__c != loc.CommercientSF8__AdrLin1__c) {
  187.  
  188. if(loc.CommercientSF8__AdrLin1__c != NULL){
  189. ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c;
  190. } else if(loc.CommercientSF8__AdrLin1__c == NULL){
  191. ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = '';
  192. }
  193.  
  194. //ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c;
  195. }
  196.  
  197. if(oldLocationMap.get(loc.Id).CommercientSF8__AdrLin2__c != loc.CommercientSF8__AdrLin2__c) {
  198.  
  199. if(loc.CommercientSF8__AdrLin2__c != NULL){
  200. ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + loc.CommercientSF8__AdrLin2__c;
  201. } else if (loc.CommercientSF8__AdrLin2__c == NULL){
  202. ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + '';
  203. }
  204.  
  205. //ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + loc.CommercientSF8__AdrLin2__c;
  206. }
  207.  
  208. if(oldLocationMap.get(loc.Id).CommercientSF8__AdrLin3__c != loc.CommercientSF8__AdrLin3__c) {
  209.  
  210. if(loc.CommercientSF8__AdrLin3__c != NULL){
  211. ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + loc.CommercientSF8__AdrLin2__c + ' ' + loc.CommercientSF8__AdrLin3__c;
  212. } else if (loc.CommercientSF8__AdrLin3__c == NULL){
  213. ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + loc.CommercientSF8__AdrLin2__c + ' ' + '';
  214. }
  215.  
  216. //ShipAcctMap.get(loc.Shipping_Account__c).ShippingStreet = loc.CommercientSF8__AdrLin1__c + ' ' + loc.CommercientSF8__AdrLin2__c + ' ' + loc.CommercientSF8__AdrLin3__c;
  217. }
  218.  
  219. if(oldLocationMap.get(loc.Id).CommercientSF8__City__c != loc.CommercientSF8__City__c) {
  220.  
  221. if(loc.CommercientSF8__City__c != NULL){
  222. ShipAcctMap.get(loc.Shipping_Account__c).ShippingCity = loc.CommercientSF8__City__c;
  223. } else if(loc.CommercientSF8__City__c == NULL){
  224. ShipAcctMap.get(loc.Shipping_Account__c).ShippingCity = '';
  225. }
  226.  
  227. //ShipAcctMap.get(loc.Shipping_Account__c).ShippingCity = loc.CommercientSF8__City__c;
  228. }
  229.  
  230. if(oldLocationMap.get(loc.Id).CommercientSF8__PostalCode__c != loc.CommercientSF8__PostalCode__c) {
  231.  
  232. if(loc.CommercientSF8__PostalCode__c != NULL){
  233. ShipAcctMap.get(loc.Shipping_Account__c).ShippingPostalCode = loc.CommercientSF8__PostalCode__c;
  234. } else if(loc.CommercientSF8__PostalCode__c == NULL){
  235. ShipAcctMap.get(loc.Shipping_Account__c).ShippingPostalCode = '';
  236. }
  237.  
  238. //ShipAcctMap.get(loc.Shipping_Account__c).ShippingPostalCode = loc.CommercientSF8__PostalCode__c;
  239. }
  240.  
  241. if(oldLocationMap.get(loc.Id).CommercientSF8__County__c != loc.CommercientSF8__County__c) {
  242.  
  243. if(loc.CommercientSF8__County__c != NULL){
  244. ShipAcctMap.get(loc.Shipping_Account__c).ShippingState = loc.CommercientSF8__County__c;
  245. } else if(loc.CommercientSF8__County__c == NULL){
  246. ShipAcctMap.get(loc.Shipping_Account__c).ShippingState = '';
  247. }
  248.  
  249. //ShipAcctMap.get(loc.Shipping_Account__c).ShippingState = loc.CommercientSF8__County__c;
  250. }
  251.  
  252. if(oldLocationMap.get(loc.Id).CommercientSF8__Country__c != loc.CommercientSF8__Country__c) {
  253.  
  254. if(loc.CommercientSF8__Country__c != NULL){
  255. ShipAcctMap.get(loc.Shipping_Account__c).ShippingCountry = loc.CommercientSF8__Country__c;
  256. } else if(loc.CommercientSF8__Country__c == NULL){
  257. ShipAcctMap.get(loc.Shipping_Account__c).ShippingCountry = '';
  258. }
  259.  
  260. //ShipAcctMap.get(loc.Shipping_Account__c).ShippingCountry = loc.CommercientSF8__Country__c;
  261. }
  262.  
  263. if(oldLocationMap.get(loc.Id).CommercientSF8__TelePhone1__c != loc.CommercientSF8__TelePhone1__c) {
  264.  
  265. if(loc.CommercientSF8__TelePhone1__c != NULL){
  266. ShipAcctMap.get(loc.Shipping_Account__c).Phone = loc.CommercientSF8__TelePhone1__c;
  267. } else if(loc.CommercientSF8__TelePhone1__c == NULL){
  268. ShipAcctMap.get(loc.Shipping_Account__c).Phone = '';
  269. }
  270.  
  271. //ShipAcctMap.get(loc.Shipping_Account__c).Phone = loc.CommercientSF8__TelePhone1__c;
  272. }
  273.  
  274. }
  275.  
  276. if(!ShipAcctMap.isEmpty()){
  277. update ShipAcctMap.values();
  278. }
  279. }
  280.  
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement