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
StenderStender 

String.replace not working? I'm confused

Here is the short of it.  I have code trying to remove a substring from a field on deletion of a child record (the creation of each child record actually adds a string to a field on the parent record, bascially a way to have a roll-up summary of text).  Anyhow I need to REMOVE that text if a child record is deleted.  Here is the code that is giving me trouble:

		For(Opportunity o :opps){
				system.debug('Opp Looking at ------------------------------>  ' + o.ID);
			For(Opportunity_Product__c oP :oppProds){
				system.debug('Opp Prod Looking at ------------------------------>  ' + oP.ID);				
				If(oP.Opportunity_Name__c == o.ID){				
					string PSNameAndID = oP.Product_Service__r.Name;					
					system.debug('zOppProd4RuleFormulas before edit ------------------------------>  ' + o.zOppProd4RuleFormulas__c);
					system.debug('P/S Name ------------------------------>  ' + PSNameAndID);			
					o.zOppProd4RuleFormulas__c.replace(PSNameAndID, '');
					system.debug('zOppProd4RuleFormulas AFTER edit ------------------------------>  ' + o.zOppProd4RuleFormulas__c);																		
				}
			}
			oppsToUpdate.add(o);	
		}		
		update oppsToUpdate; 

 

 

Here is the output from a test of that:

36]|DEBUG|Opp Looking at ------------------------------>  006g0000001szvxAAA
11:31:47.129 (129161000)|SYSTEM_METHOD_EXIT|[36]|System.debug(ANY)
11:31:47.129 (129173000)|SYSTEM_METHOD_ENTRY|[37]|LIST<Opportunity_Product__c>.iterator()
11:31:47.129 (129207000)|SYSTEM_METHOD_EXIT|[37]|LIST<Opportunity_Product__c>.iterator()
11:31:47.129 (129217000)|SYSTEM_METHOD_ENTRY|[37]|system.ListIterator.hasNext()
11:31:47.129 (129229000)|SYSTEM_METHOD_EXIT|[37]|system.ListIterator.hasNext()
11:31:47.129 (129263000)|SYSTEM_METHOD_ENTRY|[38]|String.valueOf(Object)
11:31:47.129 (129290000)|SYSTEM_METHOD_EXIT|[38]|String.valueOf(Object)
11:31:47.129 (129303000)|SYSTEM_METHOD_ENTRY|[38]|System.debug(ANY)
11:31:47.129 (129312000)|USER_DEBUG|[38]|DEBUG|Opp Prod Looking at ------------------------------>  a1hg00000004DG4AAM
11:31:47.129 (129319000)|SYSTEM_METHOD_EXIT|[38]|System.debug(ANY)
11:31:47.129 (129349000)|SYSTEM_METHOD_ENTRY|[39]|Id.compareTo(Id, Boolean)
11:31:47.129 (129380000)|SYSTEM_METHOD_EXIT|[39]|Id.compareTo(Id, Boolean)
11:31:47.129 (129511000)|SYSTEM_METHOD_ENTRY|[42]|System.debug(ANY)
11:31:47.129 (129540000)|USER_DEBUG|[42]|DEBUG|zOppProd4RuleFormulas before edit ------------------------------>  Sabre PMS; XX Hardward Solution w/Software Licensing - a0Jg00000008yzWEAQ; XX Hardward Solution w/Software Licensing - a0Jg00000008yzWEAQ
11:31:47.129 (129550000)|SYSTEM_METHOD_EXIT|[42]|System.debug(ANY)
11:31:47.129 (129566000)|SYSTEM_METHOD_ENTRY|[43]|System.debug(ANY)
11:31:47.129 (129588000)|USER_DEBUG|[43]|DEBUG|P/S Name ------------------------------>  XX Hardward Solution w/Software Licensing
11:31:47.129 (129596000)|SYSTEM_METHOD_EXIT|[43]|System.debug(ANY)
11:31:47.129 (129661000)|SYSTEM_METHOD_ENTRY|[45]|System.debug(ANY)
11:31:47.129 (129686000)|USER_DEBUG|[45]|DEBUG|zOppProd4RuleFormulas AFTER edit ------------------------------>  Sabre PMS; XX Hardward Solution w/Software Licensing - a0Jg00000008yzWEAQ; XX Hardward Solution w/Software Licensing - a0Jg00000008yzWEAQ
11:31:47.129 (129695000)|SYSTEM_METHOD_EXIT|[45]|System.debug(ANY)
11:31:47.129 (129740000)|SYSTEM_METHOD_ENTRY|[37]|system.ListIterator.hasNext()
11:31:47.129 (129755000)|SYSTEM_METHOD_EXIT|[37]|system.ListIterator.hasNext()
11:31:47.129 (129793000)|SYSTEM_METHOD_ENTRY|[49]|LIST<Opportunity>.add(Object)
11:31:47.129 (129854000)|SYSTEM_METHOD_EXIT|[49]|LIST<Opportunity>.add(Object)
11:31:47.129 (129868000)|SYSTEM_METHOD_ENTRY|[35]|system.ListIterator.hasNext()
11:31:47.129 (129881000)|SYSTEM_METHOD_EXIT|[35]|system.ListIterator.hasNext()
11:31:47.129 (129942000)|DML_BEGIN|[51]|Op:Update|Type:Opportunity|Rows:1

 

 

Why is it not removing that string like it is supposed to?  I have tried setting the field to a string variable and then trying to use the replace() method, but still no dice.  What is the problem here?

 

Thanks,

Jeremy Stender

Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

do this... you are replacing but not assigning to field again... you will have to explicitly assign it to the field... it will not associate the value automatically to the field that you wanted to replace with...

 

o.zOppProd4RuleFormulas__c = o.zOppProd4RuleFormulas__c.replace(PSNameAndID, '');

 

 

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

do this... you are replacing but not assigning to field again... you will have to explicitly assign it to the field... it will not associate the value automatically to the field that you wanted to replace with...

 

o.zOppProd4RuleFormulas__c = o.zOppProd4RuleFormulas__c.replace(PSNameAndID, '');

 

 

This was selected as the best answer
StenderStender

Just realized that as well.  Thanks Sam!