Last active
October 21, 2019 13:44
-
-
Save mhamzas/bff2c2115a23350289e1e2bbfa2766c5 to your computer and use it in GitHub Desktop.
Rollup Asset Product Names on Account Object in MultiPickList - [NEW]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @isTest | |
| public class RollupassetsonAcc_Test { | |
| public static testMethod void testAssets() | |
| { | |
| Id AccountID = createAccount('test Account'); | |
| Id ProductID1 = createProduct('Activity Management - Class Scheduling - Annual S&E'); | |
| Id ProductID2 = createProduct('Activity Management - Appointment Booking'); | |
| List<Id> ProductIds = new list<Id>(); | |
| ProductIds.add(ProductID1); | |
| ProductIds.add(ProductID2); | |
| List<asset> assetIds = createAssets('test','Purchased',ProductIds,AccountId); | |
| //Id assetId2 = createAsset('test','Purchased',ProductId2,AccountId); | |
| updateAsset(assetIDs[0].Id, 'test','Removed',ProductId2,AccountId); | |
| } | |
| public static List<Asset> createAssets(String Name, String status,List<Id> products, Id AccId){ | |
| List<Asset> ListtoInsert = new List<Asset>(); | |
| for(Id pId : products){ | |
| Asset ass = new Asset(); | |
| ass.Name=Name+pId; | |
| ass.Status=status; | |
| ass.Product2Id = pId; | |
| ass.AccountId = accId; | |
| ListtoInsert.add(ass); | |
| } | |
| system.debug('Assets::'+ListtoInsert); | |
| insert ListtoInsert; | |
| return ListtoInsert; | |
| } | |
| public static void updateAsset(Id assId ,String Name, String status,Id pId, Id AccId){ | |
| Asset ass = new Asset(id=assId); | |
| ass.Name=Name; | |
| ass.Status=status; | |
| ass.Product2Id = pId; | |
| ass.AccountId = accId; | |
| update ass; | |
| } | |
| public static id createAccount(String Name){ | |
| Account acc = new Account(); | |
| acc.Name = Name; | |
| acc.Regions__c = 'North America'; | |
| insert acc; | |
| return acc.Id; | |
| } | |
| public static id createProduct(String Name){ | |
| Product2 prod = new Product2(Name = Name); | |
| insert prod; | |
| Id pricebookId = Test.getStandardPricebookId(); | |
| PricebookEntry standardPrice = new PricebookEntry( | |
| Pricebook2Id = pricebookId, Product2Id = prod.Id, | |
| UnitPrice = 10000, IsActive = true); | |
| insert standardPrice; | |
| Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true); | |
| insert customPB; | |
| PricebookEntry customPrice = new PricebookEntry( | |
| Pricebook2Id = customPB.Id, Product2Id = prod.Id, | |
| UnitPrice = 12000, IsActive = true); | |
| insert customPrice; | |
| return prod.Id; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class RollupassetsonAcc_TrgHandler { | |
| public void RollupAssets(List <Asset> newObjects, Map <Id, Asset> newMap,List <Asset> oldObjects, Boolean isDelete) { | |
| Set<Id> AccountIds = new Set<Id>(); | |
| if(newObjects != null){ | |
| //Looping over New Asset Records | |
| for (Asset ass: newObjects) { | |
| //Adding all AccountIds in a List | |
| AccountIds.add(ass.AccountId); | |
| } | |
| } | |
| if(isDelete && oldObjects !=null){ | |
| //Looping over Old Asset Records | |
| for (Asset ass: oldObjects) { | |
| //Adding all AccountIds in a List | |
| AccountIds.add(ass.AccountId); | |
| } | |
| } | |
| List<String> Products = new List<String>(); | |
| Map<Id,String> AccProducts = new Map<Id,String>(); | |
| for (Asset ass: [Select Id,AccountId,ProductName__c,Status from Asset where AccountId in :AccountIds and Status='Purchased']) { | |
| if(AccProducts.containsKey(ass.AccountId)){ | |
| String Productnames = AccProducts.get(ass.AccountId); | |
| Productnames+=';'+ass.ProductName__c; //ProductName__c is a formula field | |
| AccProducts.put(ass.AccountId,Productnames); | |
| } else { | |
| AccProducts.put(ass.AccountId,ass.ProductName__c); | |
| } | |
| } | |
| List<Account> listofacctoUpdate = new List<Account>(); | |
| for (Id accId : AccProducts.keySet()){ | |
| Account AccRec = new Account(Id = accId,Account_Assets__c=AccProducts.get(accId)); | |
| listofacctoUpdate.add(AccRec); | |
| } | |
| //Updating account records | |
| if(listofacctoUpdate != null && listofacctoUpdate.size() !=0){ | |
| update listofacctoUpdate; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trigger RollupassetsonAccTrg on Asset (after insert,after update,after delete) { | |
| RollupassetsonAcc_TrgHandler handler = new RollupassetsonAcc_TrgHandler(); | |
| handler.RollupAssets(Trigger.new, Trigger.newMap, Trigger.old,Trigger.isDelete); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment