function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zishan RazzaqZishan Razzaq 

Trigger Help please Set is not obtaining Unique values

Hi all,
I am trying to write a trigger that would obtain unique set of values from a child object and update the parent object Multi-Select Picklist field.
Now the issue is that when I edit the same record in the child twice and the status is equal to Active it comes up in the Multi-Select twice when it should be there once.
For example,
Market Type Child field = RV,Truck,Car
On the account the Market Type is the same until I edit the child record again let say RV
on the Account it will then show RV;RV;Truck;Car...
Here is my code:
trigger PopulateMarketType on Account_Market__c (after insert,after update) {
/*************************Variable Section*************************************/
    List<ID> AccIDs = new List<ID>();
    
    List<Account> updateAccList = new List<Account>();
    
    Map<ID,Account> AccUpdateMap = new Map<ID,Account>();
    
    Set<String> subAccNames = new Set<String>();
    
    List<Account> clearvaluesMT = new List<Account>();
    
    String[] UniqueMTs;
    
    String FinalUnique;
    Integer x=0;
 /****************************************************************************/
 
/**********************Retrieve Account Id's from the new AMI records***************/    
    For( Account_Market__c ami: Trigger.New)
    {
            AccIDs.add(ami.Account__c);
    }
     
    For(Account acc : [Select ID, Market_Type__c from Account where ID in:AccIDs])
    {
             AccUpdateMap.put(acc.ID ,acc );
    }

    
    /****I like to clear out all the values in the Market Type Field in the Account and then re-insert them in****/
   /*For (Account clearthis : [Select Market_Type__c from Account where ID in:AccIDs])
    {
            System.Debug('zzz32'+clearthis.Market_Type__c);
            clearthis.Market_Type__c = '';   
                    
            clearvaluesMT.add(clearthis);
            System.Debug('zzz35'+clearthis.Market_Type__c);
             update clearvaluesMT;  
    }    
    If (clearvaluesMT != null && clearvaluesMT.size() > 0) 
    {             
        update clearvaluesMT;   
        System.Debug('zzz38'+clearvaluesMT);
    }*/
 /**********************************************************************************************************/   
    /**Re-Insert the Market Types in for the associated Account*****/ 
    For (Account_Market__c amii: Trigger.New)
    {
    
             Account updateacc = AccUpdateMap.get(amii.Account__c);
                If (amii.Status__c == 'Active') 
                 {
                     
                     If (updateacc.Market_Type__c ==null)
                     {                        
                         updateacc.Market_Type__c = amii.Market_Type__c;   
                      }
                      else
                      {
                      
                      //1st - get all the values
                          updateacc.Market_Type__c += +';'+ amii.Market_Type__c; 
                           
                     //2nd - place them in a set to  become unique     
                          subAccNames.add(updateacc.Market_Type__c );
                     
                     //3rd - put them in a string   
                          FinalUnique = String.valueof(subAccNames);
                          System.Debug('zzz74'+FinalUnique);
                      
                      //4th - take away the {} from value
                            if(FinalUnique.contains('{'))
                            {
                                FinalUnique = FinalUnique.replace('{', ' ');
                            }
                            
                            if(FinalUnique.contains('}'))
                            {
                                FinalUnique = FinalUnique.replace('}', ' ');                                  
                            }                         
                     
                     //5th - Finally update the Market type with the Final Value     
                         updateacc.Market_Type__c  = FinalUnique;      
                 
                     }               
                 updateAccList.add(updateacc);    
                 } 
                   
    }
    If (updateAccList != null && updateAccList.size() > 0)
        
            update updateAccList;

}

Thank you
kiranmutturukiranmutturu
I didn't get 100% what you narrated but
Set will not accept duplicates... https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections_sets.htm
pradeep naredlapradeep naredla
Hi zishan,
     Can u pls eloberate with a clear example.;...
Thanks,
Pradeep
Zishan RazzaqZishan Razzaq
Thank you both, so what I am trying to do is the following,
I have a child object Account Market with a text field Market Type and the parent object is Account that has a Multi Select Picklist field named Market Type.
When the child object gets records inserted in and or updated through a webservice then I need the trigger to take all the values in the child object for the Market Type field when Status equals Active and place them into the Market Type field on the Account Object.
Ideally I would love to clear out the Market Type Values on the Account object and re-insert them in...
So my issue is that I utilized a Set to place the values in and basically the set is still getting duplicates.
When I go to a account market record that has let say RV as the market type (Child object) the trigger updates the Account (Parent) market Type field from RV;Auto to RV;RV;Auto...
I dont understand why? Hope this helps.
Thank you
Zishan
pradeep naredlapradeep naredla
Hi zishan,
   I got ur senario every time ur updating the record the values are entering without checking the values already existing so ur getting duplicate values is it true.

thanks,
pradeep.
kiranmutturukiranmutturu
You should get all the existing picklist values and then compare the current vaule with the existing list of values and insert if it is not in the list..I hope here you want to check with the field values but not with the database records for that field which have the same value.