You need to sign in to do that
Don't have an account?
AL
Trigger Collection - Illegal Assignment from Set to String
Hello I would like to rollup a field value from all of the assets on an account up to that account's field. Currently, the trigger is updating the account field, but it's overwriting the existing value, and not collecting the asset field values. On line 24 I'm getting an error of illegal assignment from set to string (ProductsOwned_c += prodFam.clone()).
I'm trying to take all of the unique string values in the ProdFam set variable and load them into the ProductsOwned__ field. Can I load a collection into a text field? Thank you for your help.
trigger BulkifyAssets on Asset (before insert, before update) {
Set<Id> accountId = new Set<Id>();
Map<Id, Account> parentRecords = new Map<Id, Account>();
Map<String,Account> accProductsOwnedMap = new Map<String,Account>();
Set<String> prodFam = new Set<String>();
for(Asset a: trigger.new){
if(a.Status != null || a.status != 'Retired' || a.status != 'Obsolete' )
accountId.add(a.AccountId);
accountId.remove(null);
String prodFamAccount = a.ProductFamily__c;
prodFam.add(prodFamAccount);
for(Id parentId: accountId){
String prodFamSingle;
for(String f: prodFam)
parentRecords.put(parentId, new Account(Id = parentId, ProductsOwned__c =+ prodFam.clone()));
for(Asset asset: [Select Id, ProductFamily__c, AccountId from Asset Where Id In: accountId]){
if(parentRecords.get(asset.AccountId).ProductsOwned__c != null){
parentRecords.get(asset.AccountId).ProductsOwned__c += ',' + parentRecords.values();
}
else{
parentRecords.get(asset.AccountId).ProductsOwned__c += ',' + parentRecords.values();
}
}
if(parentRecords.size()>0)
{
update parentRecords.values();
}
}
}
}
I'm trying to take all of the unique string values in the ProdFam set variable and load them into the ProductsOwned__ field. Can I load a collection into a text field? Thank you for your help.
trigger BulkifyAssets on Asset (before insert, before update) {
Set<Id> accountId = new Set<Id>();
Map<Id, Account> parentRecords = new Map<Id, Account>();
Map<String,Account> accProductsOwnedMap = new Map<String,Account>();
Set<String> prodFam = new Set<String>();
for(Asset a: trigger.new){
if(a.Status != null || a.status != 'Retired' || a.status != 'Obsolete' )
accountId.add(a.AccountId);
accountId.remove(null);
String prodFamAccount = a.ProductFamily__c;
prodFam.add(prodFamAccount);
for(Id parentId: accountId){
String prodFamSingle;
for(String f: prodFam)
parentRecords.put(parentId, new Account(Id = parentId, ProductsOwned__c =+ prodFam.clone()));
for(Asset asset: [Select Id, ProductFamily__c, AccountId from Asset Where Id In: accountId]){
if(parentRecords.get(asset.AccountId).ProductsOwned__c != null){
parentRecords.get(asset.AccountId).ProductsOwned__c += ',' + parentRecords.values();
}
else{
parentRecords.get(asset.AccountId).ProductsOwned__c += ',' + parentRecords.values();
}
}
if(parentRecords.size()>0)
{
update parentRecords.values();
}
}
}
}
for(Asset asset: [Select Id, ProductFamily__c, AccountId from Asset Where Id In: accountId]){
if(parentRecords.get(asset.AccountId).ProductsOwned__c != null){
for(account instAcc:parentRecords.values()){
parentRecords.get(asset.AccountId).ProductsOwned__c += ',' + instAcc.OwnerID;
}
}
Thanks
for(String f: prodFam)
parentRecords.put(parentId, new Account(Id = parentId, ProductsOwned__c =+ prodFam.clone()));
Why you are assigning the complete list to 'ProductsOwned__c'? I think you should use 'f' instead of 'prodFam.clone()'.
trigger Asset5 on Asset (after insert, after update) {
if(Trigger.isAfter){
if(Trigger.isUpdate){
try{
for(Asset a:trigger.old){
String s = a.ProductFamily__c;
Account account = [Select Id, ProductsOwned__c from Account Where Id =: a.AccountId];
List<Asset> listAsset = [Select Id, ProductFamily__c, Status from Asset where (AccountId =:account.Id) AND (Status != 'Retired' OR Status != 'Obsolete') ];
Map<Id, Set<String>> mapTemp = new Map<Id, Set<String>>();
for(Asset asset: listAsset){
Set<String> assetCollection = new Set<String>();
assetCollection.add(asset.ProductFamily__c);
mapTemp.put(asset.Id, assetCollection);
System.debug('---------------------' + mapTemp);
String por =+ '' +assetCollection;
account.ProductsOwned__c = por;
}
update account;
}
}
catch(Exception e){
System.debug(e);
}
}
}
}