+ Start a Discussion
ALAL 

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();
}  
}
}
}
 
kumud thakur 20kumud thakur 20
parentRecords.values() return a list of account records, not string. I am not sure why you want to append list of account in the text field of account. if you want to do it, you need to iterate the list and append the account fields. e.g.
 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
 
ALAL
Thank you Kumud. I was trying to create a set of Asset product families (string-prodFam), so that I could take all of the asset product families on that account and roll it up to the Account's ProductsOwned__c field. So I was trying to insert that set of strings, prodFam into the account map's value. 
Sfdc UserSfdc User
For the below code:

 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()'. 
ALAL
I was looking to see if I could make mass updates on a list of assets. So if I mass update 15 assets that are all linked to the same account, I'd like to take those assets' ProductFamily_c string values (the unique ones) and roll them up to the Account's ProductsOwned__c field.
ALAL
Hello, I have reworked the code to pull all of the assets are that updated and are linked to the same account. I am attempting to link a set<string> to a string field (String por =+ '' + assetCollection). However when I do a mass update on the assets, the account field is only grabbing one of the asset updates. Is there a way to set the set<string> in the mapTemp field and assign that to a string field? From there I would like to take the account ProductsOwned__c field and assign it to the new string field. Thank you. 

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);
        }
    }
}
}