Skip to content

Instantly share code, notes, and snippets.

@mhamzas
Last active October 21, 2019 13:32
Show Gist options
  • Save mhamzas/4e1aab7ed80ed6f355d7f1d72a595213 to your computer and use it in GitHub Desktop.
Save mhamzas/4e1aab7ed80ed6f355d7f1d72a595213 to your computer and use it in GitHub Desktop.
Rollup Asset Product Names on Account Object in MultiPickList - See the Updated here : https://gist.github.com/mhamzas/bff2c2115a23350289e1e2bbfa2766c5
/*
Method # 2 : Invokable Class
*/
public with sharing class RollupassetsonAcc {
public class AssetRollupRequest {
@InvocableVariable(
label = 'Asset Id'
description = 'ID of the Asset to rollup'
required = true
)
public ID assetId;
@InvocableVariable(
label = 'Product Name'
description = 'Name of a Product to rollup'
required = true
)
public String ProductName;
@InvocableVariable(
label = 'AccountID'
description = 'AccountID of an asset to rollup'
required = true
)
public String accountID;
@InvocableVariable(
label = 'Add Asset?'
description = 'Add or Remove Asset? Default is true.'
)
public Boolean addAsset = true;
}
public class AssetRollupResult {
@InvocableVariable( label = 'Accounts' )
public List<Account> accounts;
}
// --------------------------------------------------------------------------------
@InvocableMethod(
label = 'Rollup Assets'
)
public static List<Account> rollupAssets( List<AssetRollupRequest> requests ) {
List<Id> ListofAssets = new List<Id>();
Map<ID,Boolean> MapofAssetAction= new Map<ID,Boolean>();
Map<ID,String> MapofAssetProducts= new Map<ID,String>();
Map<ID,ID> MapofAssetAccounts = new Map<ID,ID>();
for(AssetRollupRequest requestObj : requests){
//Accessing the values from process builder when record is inserted
ListofAssets.add(requestObj.assetId);
MapofAssetProducts.put(requestObj.assetId,requestObj.ProductName);
MapofAssetAction.put(requestObj.assetId,requestObj.addAsset);
MapofAssetAccounts.put(requestObj.assetId,requestObj.accountId);
}
List<Id> AccountIds = new List<Id>();
Map<Id,String> AccProductName = new Map<Id,String>();
Map<Id,String> AccProductNameRemove = new Map<Id,String>();
List<Account> listofAcc = new List<Account>();
//Looping over New Asset Records
for (Id assetId: ListofAssets) {
//Adding all AccountIds in a List
AccountIds.add(MapofAssetAccounts.get(assetId));
//If Asset is Purchased
if(MapofAssetAction.get(assetId) == TRUE){
// Making a map of ProductNames for for all Accounts to add
if(AccProductName.containsKey(MapofAssetAccounts.get(assetId))){
String Productnames = AccProductName.get(MapofAssetAccounts.get(assetId));
Productnames+=';'+MapofAssetProducts.get(assetId); //ProductName__c is a formula field
AccProductName.put(MapofAssetAccounts.get(assetId),Productnames);
} else {
AccProductName.put(MapofAssetAccounts.get(assetId),MapofAssetProducts.get(assetId));
}
} else {
// Making a map of ProductNames for for all Accounts to Remove
if(AccProductNameRemove.containsKey(MapofAssetAccounts.get(assetId))){
String Productnames = AccProductNameRemove.get(MapofAssetAccounts.get(assetId));
Productnames+=';'+MapofAssetProducts.get(assetId); //ProductName__c is a formula field
AccProductNameRemove.put(MapofAssetAccounts.get(assetId),MapofAssetProducts.get(assetId));
} else {
AccProductNameRemove.put(MapofAssetAccounts.get(assetId),MapofAssetProducts.get(assetId));
}
}
}
List<Account> accList = [Select Id,Name,Account_Assets__c from Account where id in :AccountIds];
//Getting Account records for newly created/updated assets
for(Account acc : accList){
Account AccRecord = new Account(Id = acc.Id);
//List of Products to Remove
String ListtoAdd = AccProductName.get(acc.Id);
String ListtoRemove = AccProductNameRemove.get(acc.Id);
// Adding all products
if(ListtoAdd !='' && ListtoAdd != null){
for(String s :ListtoAdd.split(';')){
if(s != null){
AccRecord.Account_Assets__c = MultipicklistUpdate(acc.Account_Assets__c,s,TRUE);
}
system.debug('New Field Value='+AccRecord.Account_Assets__c);
}
}
// Removing all products
if(ListtoRemove !='' && ListtoRemove != null){
for(String s :ListtoRemove.split(';')){
if(s != null){
AccRecord.Account_Assets__c = MultipicklistUpdate(acc.Account_Assets__c,s,FALSE);
}
system.debug('New Field Value='+AccRecord.Account_Assets__c);
}
}
//AccRecord.Account_Assets__c = MultipicklistUpdate(acc.Account_Assets__c,AccProductName.get(acc.Id),TRUE);
system.debug('Assets::'+ AccRecord.Account_Assets__c);
listofacc.add(AccRecord);
}
//Updating account records
if(listofacc != null && listofacc.size() !=0){
update listofacc;
}
return listofacc;
}
// --------------------------------------------------------------------------------
// This function returns an string with the values for a multipicklist after adding or removing an input Value.
Public Static String MultipicklistUpdate(String currentValues,String inputValue,boolean addValue){
if(!String.isBlank(currentValues)){
Map<String, String> currentValueMap = new Map<String, String>();
for(String s :currentValues.split(';')){
currentValueMap.put(s,s);
}
if(addValue && !currentValueMap.containsKey(inputValue)){
currentValueMap.put(inputValue, inputValue);
}else{
if(currentValueMap.containsKey(inputValue)){
currentValueMap.remove(inputValue);
}
}
String newValue = '';
for(String s :currentValueMap.values()){
newValue = newValue+ ';'+inputValue;
}
return newValue;
}
else if(addValue && !String.isBlank(inputValue)){
return inputValue;
}
return null;
}
}
public class RollupassetsonAcc_TrgHandler {
public void onAfterInsert_Asset(List <Asset> newObjects, Map <Id, Asset> newMap) {
RollupAssets(newObjects, newMap);
}
public void onAfterUpdate_Asset(List <Asset> newObjects, Map <Id, Asset> newMap) {
RollupAssets(newObjects, newMap);
}
// This function returns an string with the values for a multipicklist after adding or removing an input Value.
Public Static String MultipicklistUpdate(String currentValues,String inputValue,boolean addValue){
if(!String.isBlank(currentValues)){
Map<String, String> currentValueMap = new Map<String, String>();
for(String s :currentValues.split(';')){
currentValueMap.put(s,s);
}
if(addValue && !currentValueMap.containsKey(inputValue)){
currentValueMap.put(inputValue, inputValue);
}else{
if(currentValueMap.containsKey(inputValue)){
currentValueMap.remove(inputValue);
}
}
String newValue = '';
for(String s :currentValueMap.values()){
newValue = newValue+ ';'+inputValue;
}
return newValue;
}
else if(addValue && !String.isBlank(inputValue)){
return inputValue;
}
return null;
}
public void RollupAssets(List <Asset> ListofAssets, Map <Id, Asset> MapofAssets){
List<Id> AccountIds = new List<Id>();
Map<Id,String> AccProductName = new Map<Id,String>();
Map<Id,String> AccProductNameRemove = new Map<Id,String>();
List<Account> listofAcc = new List<Account>();
//Looping over New Asset Records
for (Asset asset: ListofAssets) {
//Adding all AccountIds in a List
AccountIds.add(asset.AccountId);
//If Asset is Purchased
if(asset.Status == 'Purchased'){
// Making a map of ProductNames for for all Accounts to add
if(AccProductName.containsKey(asset.accountId)){
String Productnames = AccProductName.get(asset.accountId);
Productnames+=';'+asset.ProductName__c; //ProductName__c is a formula field
AccProductName.put(asset.accountId,Productnames);
} else {
AccProductName.put(asset.accountId,asset.ProductName__c);
}
} else {
// Making a map of ProductNames for for all Accounts to Remove
if(AccProductNameRemove.containsKey(asset.accountId)){
String Productnames = AccProductNameRemove.get(asset.accountId);
Productnames+=';'+asset.ProductName__c; //ProductName__c is a formula field
AccProductNameRemove.put(asset.accountId,Productnames);
} else {
AccProductNameRemove.put(asset.accountId,asset.ProductName__c);
}
}
}
List<Account> accList = [Select Id,Name,Account_Assets__c from Account where id in :AccountIds];
//Getting Account records for newly created/updated assets
for(Account acc : accList){
Account AccRecord = new Account(Id = acc.Id);
//List of Products to Remove
String ListtoAdd = AccProductName.get(acc.Id);
String ListtoRemove = AccProductNameRemove.get(acc.Id);
// Adding all products
if(ListtoAdd !='' && ListtoAdd != null){
for(String s :ListtoAdd.split(';')){
acc.Account_Assets__c = MultipicklistUpdate(acc.Account_Assets__c,s,TRUE);
}
}
// Removing all products
if(ListtoRemove !='' && ListtoRemove != null){
for(String s :ListtoRemove.split(';')){
acc.Account_Assets__c = MultipicklistUpdate(acc.Account_Assets__c,s,FALSE);
}
}
AccRecord.Account_Assets__c = MultipicklistUpdate(acc.Account_Assets__c,AccProductName.get(acc.Id),TRUE);
system.debug('Assets::'+ AccRecord.Account_Assets__c);
listofacc.add(AccRecord);
}
//Updating account records
if(listofacc != null && listofacc.size() !=0){
update listofacc;
}
}
}
@isTest
public class RollupassetsonAcc_TrgHandler_Test {
public static testMethod void testAssets()
{
Id ProductID1 = createProduct('Activity Management - Class Scheduling - Annual S&E');
Id ProductID2 = createProduct('Activity Management - Appointment Booking');
Id AccountID = createAccount('test Account');
Id assetID1 = createAsset('test','Purchased',ProductId1,AccountId);
Id assetId2 = createAsset('test','Purchased',ProductId2,AccountId);
updateAsset(assetID1, 'test','Removed',ProductId2,AccountId);
}
public static id createAsset(String Name, String status,Id pId, Id AccId){
Asset ass = new Asset();
ass.Name=Name;
ass.Status=status;
ass.Product2Id = pId;
ass.AccountId = accId;
insert ass;
return ass.Id;
}
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;
}
}
trigger RollupAssetsonAccount on Asset (after insert, after update) {
RollupassetsonAcc_TrgHandler handler = new RollupassetsonAcc_TrgHandler();
// After Insert
if(Trigger.isInsert && Trigger.isAfter)
{
handler.onAfterInsert_Asset(Trigger.new, Trigger.newMap);
}
// After Update
if(Trigger.isUpdate && Trigger.isAfter)
{
handler.onAfterUpdate_Asset(Trigger.new, Trigger.newMap);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment