Hi I am trying to replace one string value with another however my string is being wrapped with {} and I don't know why and causes the replace not to be able to find the string in my ETF_Summary__c field. How can I stop this from happening so the replace will work?

public static void ETFSummaryupdate(ETF__c[] etfnew){
    boolean etfnameamount = false;
    boolean updatefundslist = false;
    Set<String> relatedfundID = new Set<String>();
    Set<String> newETFNameAmount = new Set<String>();
    Set<String> oldETFNameAmount = new Set<String>();

    for(ETF__c etf : etfnew){
        ETF__c oldetf = (ETF__c)Trigger.oldMap.get(etf.Id);
        if(etf.Name != oldetf.name && etf.ETF_Amount__c != oldetf.ETF_Amount__c){
            etfnameamount = true;
            oldETFNameAmount.add(oldetf.Name + ' $' + oldetf.ETF_Amount__c);
            newETFNameAmount.add(etf.Name + ' $' + etf.ETF_Amount__c);
    list<Funds_to_Trade__c> relatedFundslist = new list<Funds_to_Trade__c>();
    relatedFundslist = [select Id, Name, ETF_Summary__c from Funds_to_Trade__c where ID in: relatedfundID];
    for(Funds_to_Trade__c fund : relatedFundslist){
        if(etfnameamount == true){
            fund.ETF_Summary__c = fund.ETF_Summary__c.replace(string.valueof(oldETFNameAmount),string.valueof(newETFNameAmount));
        if(relatedFundslist.size() == 0){

    if(relatedFundslist.size() > 0 && updatefundslist == false){
        update relatedFundslist;
        updatefundslist = true;            

  • I think Its because oldETFNameAmount and newETFNameAmount are Set variables. So are you trying to replace a set of values with another set of values? I don't think this is the best way to do this. – AslamK Mar 18 '16 at 7:08

You have to convert the Setto String using temp List. So you will avoid the braces:

List<String> tmpList = new List<String>();
String stringFromSet = String.join(tmpList, ',');

But better work with sets methods addAll(...) and removeAll(...) instead of replacing strings. Assuming the value in the field fund.ETF_Summary__c is a semicolon-separated string:

// Creating a temp set. We will use it lates to assign back to the field
String val = fund.ETF_Summary__c;
Set<String> tempSet = new Set<String>();

for(Funds_to_Trade__c fund : relatedFundslist){
    if(etfnameamount == true){      

        // Remove old values form the set
        // Add new values

// Converting set to list to be able to "export" values to the string
List<String> tempList = new List<String>();

// Assigning a new value to the field (semicolon separated string)
fund.ETF_Summary__c = String.join(tempList, ';');
  • I also saw that you use assigned the val = fund.ETF_Summary__c before fund has been declared. – Michael Mar 20 '16 at 23:32
  • So I used your method of remove all and add all, it works for adding the value but does not remove my old value. – Michael Mar 21 '16 at 1:00
  • Figured it out, I was separating the field by line break but didn't update the split to \n and only the join to \n so thats why the add worked but not the remove. I also added everything above to within the for loop as you have used fund above and below it. – Michael Mar 21 '16 at 2:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.