Skip to content

Instantly share code, notes, and snippets.

@mhamzas
Last active October 21, 2019 13:44
Show Gist options
  • Save mhamzas/bff2c2115a23350289e1e2bbfa2766c5 to your computer and use it in GitHub Desktop.
Save mhamzas/bff2c2115a23350289e1e2bbfa2766c5 to your computer and use it in GitHub Desktop.
Rollup Asset Product Names on Account Object in MultiPickList - [NEW]
@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;
}
}
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;
}
}
}
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