You need to sign in to do that
Don't have an account?
AL
Adding additional set elements to a string collection
Hello I am trying to add additional string elements to my set<string> collection variable. When I add a new string element (e.g. asset), it seems to be overriding the existing values in the assetCollection variable. Is there a way to add the additonal elements without overiding existing values?
I've tried the add(string element) method that doesn't seem to be working. Thank you.
Below are my class and trigger
Class
public class AccountActiveProducts {
public static void updateAccounts(List<Asset> assetLists){
Map<Id, Account> mapAccount = new Map<Id, Account>([Select Id, ProductsOwned__c, (Select Id, ProductFamily__c from Assets Where Id In: Trigger.new) From Account]);
Set<String> assetCollection = new Set<String>();
Set<Id> ParentIds = new Set<Id>();
List<Account> AccountParent = new List<Account>();
String a = '';
Boolean noMatch = false;
if(Trigger.isInsert || Trigger.isUpdate){
for(Asset asset: assetLists){
if(asset.Status!= null ||asset.Status !='Retired' || asset.Status!= 'Obsolete' && asset.AccountId != null )
ParentIds.add(asset.AccountId);
assetCollection.add(asset.ProductFamily__c);
}
}
if(Trigger.isDelete){
for(Asset assetOld: assetLists){
if(assetOld.Status!= null && assetOld.Status !='Retired' && assetOld.Status!= 'Obsolete' && assetOld.AccountId != null )
ParentIds.add(assetOld.AccountId);
}
}
for(Account account: [Select Id, ProductsOwned__c from Account where Id In: ParentIds]){
a =+ '' + string.valueOf(assetCollection);
mapAccount.put(account.id, account);
}
if(ParentIds.size() >0){
List<Asset> assetList = new List<Asset>([Select Id, AccountId, Status, ProductFamily__c from Asset where
AccountId IN: ParentIds]);
List<Account> accountList = new List<Account>([Select Id, ProductsOwned__c from Account where
Id IN: ParentIds]);
for(Account accountP: accountList){
for(Asset asstc: assetList){
if(asstc.AccountId == accountP.Id)
for(String s: assetCollection){
//If assetCollection doesn't contain the string element, then add that string element to the collection
if(!assetCollection.contains(s) && asstc.Status!= null && asstc.Status !='Retired' && asstc.Status!= 'Obsolete' && assetCollection.isEmpty()){
noMatch = false;
assetCollection.add(asstc.ProductFamily__c);
// assetCollection.add(s);
String outputString = String.join(new List<String>(assetCollection), ',');
a =+ string.valueOf(assetCollection).replaceAll('[{||}]','') + ', ' + asstc.ProductFamily__c;
// a =+ outputString.replaceAll('[{||}]','');
}
else if(assetCollection.contains(s) && asstc.Status!= null && asstc.Status !='Retired' && asstc.Status!= 'Obsolete' && assetCollection.contains(asstc.ProductFamily__c)){
noMatch = true;
String outputString = String.join(new List<String>(assetCollection), ',');
a = string.valueOf(assetCollection).replaceAll('[{||}]','');
}
else if(asstc.Status =='Retired' || asstc.Status== 'Obsolete' || asstc.Status== null && assetCollection.contains(asstc.ProductFamily__c)){
noMatch = false;
assetCollection.remove(asstc.ProductFamily__c);
}
}
}
accountP.ProductsOwned__c = a;
}
try{
upsert accountList;
}
catch(Exception ex){
System.debug('Exception is ' + ex);
}
}
}
}
Trigger
trigger AssetPrimaryTrigger on Asset (before insert, before update, after insert, after update, before delete, after delete) {
if(Trigger.isBefore){
if(Trigger.isInsert){
}
if(Trigger.isUpdate){
}
if(Trigger.isDelete){
}
}
if(Trigger.isAfter){
if(Trigger.isInsert){
}
if(Trigger.isUpdate){
AccountActiveProducts.updateAccounts(trigger.new);
}
if(Trigger.isDelete){
}
}
}
I've tried the add(string element) method that doesn't seem to be working. Thank you.
Below are my class and trigger
Class
public class AccountActiveProducts {
public static void updateAccounts(List<Asset> assetLists){
Map<Id, Account> mapAccount = new Map<Id, Account>([Select Id, ProductsOwned__c, (Select Id, ProductFamily__c from Assets Where Id In: Trigger.new) From Account]);
Set<String> assetCollection = new Set<String>();
Set<Id> ParentIds = new Set<Id>();
List<Account> AccountParent = new List<Account>();
String a = '';
Boolean noMatch = false;
if(Trigger.isInsert || Trigger.isUpdate){
for(Asset asset: assetLists){
if(asset.Status!= null ||asset.Status !='Retired' || asset.Status!= 'Obsolete' && asset.AccountId != null )
ParentIds.add(asset.AccountId);
assetCollection.add(asset.ProductFamily__c);
}
}
if(Trigger.isDelete){
for(Asset assetOld: assetLists){
if(assetOld.Status!= null && assetOld.Status !='Retired' && assetOld.Status!= 'Obsolete' && assetOld.AccountId != null )
ParentIds.add(assetOld.AccountId);
}
}
for(Account account: [Select Id, ProductsOwned__c from Account where Id In: ParentIds]){
a =+ '' + string.valueOf(assetCollection);
mapAccount.put(account.id, account);
}
if(ParentIds.size() >0){
List<Asset> assetList = new List<Asset>([Select Id, AccountId, Status, ProductFamily__c from Asset where
AccountId IN: ParentIds]);
List<Account> accountList = new List<Account>([Select Id, ProductsOwned__c from Account where
Id IN: ParentIds]);
for(Account accountP: accountList){
for(Asset asstc: assetList){
if(asstc.AccountId == accountP.Id)
for(String s: assetCollection){
//If assetCollection doesn't contain the string element, then add that string element to the collection
if(!assetCollection.contains(s) && asstc.Status!= null && asstc.Status !='Retired' && asstc.Status!= 'Obsolete' && assetCollection.isEmpty()){
noMatch = false;
assetCollection.add(asstc.ProductFamily__c);
// assetCollection.add(s);
String outputString = String.join(new List<String>(assetCollection), ',');
a =+ string.valueOf(assetCollection).replaceAll('[{||}]','') + ', ' + asstc.ProductFamily__c;
// a =+ outputString.replaceAll('[{||}]','');
}
else if(assetCollection.contains(s) && asstc.Status!= null && asstc.Status !='Retired' && asstc.Status!= 'Obsolete' && assetCollection.contains(asstc.ProductFamily__c)){
noMatch = true;
String outputString = String.join(new List<String>(assetCollection), ',');
a = string.valueOf(assetCollection).replaceAll('[{||}]','');
}
else if(asstc.Status =='Retired' || asstc.Status== 'Obsolete' || asstc.Status== null && assetCollection.contains(asstc.ProductFamily__c)){
noMatch = false;
assetCollection.remove(asstc.ProductFamily__c);
}
}
}
accountP.ProductsOwned__c = a;
}
try{
upsert accountList;
}
catch(Exception ex){
System.debug('Exception is ' + ex);
}
}
}
}
Trigger
trigger AssetPrimaryTrigger on Asset (before insert, before update, after insert, after update, before delete, after delete) {
if(Trigger.isBefore){
if(Trigger.isInsert){
}
if(Trigger.isUpdate){
}
if(Trigger.isDelete){
}
}
if(Trigger.isAfter){
if(Trigger.isInsert){
}
if(Trigger.isUpdate){
AccountActiveProducts.updateAccounts(trigger.new);
}
if(Trigger.isDelete){
}
}
}
Hi AL,
if you put same value multiple time in set it overwrite the value to a single value.
if you want duplicate value you should try List in the place od Set.
I've tried converting the set to a list and I am coming up with null values. Is there a way for me to iterate through all of the set values and as new set values are inserted or deleted, they get added to the existing set? I tried putting the values in a map (mapAccount.put(accountP.Id, new Account(Id = accountP.Id, ProductsOwned__c =+ setToList ));) and then updating the map but that isnt' working either. Thank you for your help.
for(Account accountP: accountList){
for(Asset asstc: assetList){
if(asstc.AccountId == accountP.Id)
for(String s: assetCollection){
//If assetCollection doesn't contain the string element, then add that string element to the collection
if(!assetCollection.contains(s) && asstc.Status!= null && asstc.Status !='Retired' && asstc.Status!= 'Obsolete' && assetCollection.isEmpty()){
noMatch = false;
assetCollection.add(asstc.ProductFamily__c);
setToList.add(asstc.ProductFamily__c);
extraValues = s;
// assetCollection.add(s);
// setToList.addAll(assetCollection);
String outputString = String.join(new List<String>(assetCollection), ',');
a =+ string.valueOf(setToList).replaceAll('[{||}]','') + ', ' + s;
// a =+ outputString.replaceAll('[{||}]','');
// setToList.addAll(assetCollection);
}
else if(assetCollection.contains(s) && asstc.Status!= null && asstc.Status !='Retired' && asstc.Status!= 'Obsolete' && assetCollection.contains(asstc.ProductFamily__c)){
noMatch = true;
String outputString = String.join(new List<String>(assetCollection), ',');
a = string.valueOf(assetCollection).replaceAll('[{||}]','');
}
else if(asstc.Status =='Retired' || asstc.Status== 'Obsolete' || asstc.Status== null){
// else if(asstc.Status =='Retired' || asstc.Status== 'Obsolete' || asstc.Status== null && assetCollection.contains(asstc.ProductFamily__c))
noMatch = false;
assetCollection.remove(asstc.ProductFamily__c);
}
}
}
if(assetCollection.isEmpty()){
accountP.ProductsOwned__c = a;
}
//Add the additional picklist values in a variable and append to
else if(!assetCollection.isEmpty()){
accountP.ProductsOwned__c =+ a;
mapAccount.put(accountP.Id, new Account(Id = accountP.Id, ProductsOwned__c =+ setToList ));
//assetCollection + extraValues
}
}
try{
// upsert accountList;
upsert mapAccount.values();
}
catch(Exception ex){
System.debug('Exception is ' + ex);
}