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
Rahul H 4Rahul H 4 

Hi Everyone, Can you please help me in writing a trigger to truncate the characters on a field

We are loading the Case from the website. There is an issue with Images field in Case entity as displayed below:

Text in Images Field in Case entity : ["https:\/\/www.intops.com.au\/wp-content\/uploads\/gravity_forms\/2-cb870933d1056667864f32a0efe9bcff\/2018\/08\/MDC3521112.JPG";]
 
So, everytime we have to remove the junk characters and set it as below and check the images.
 
Need update: https:\\www.intops.com.au/wp-content/uploads/gravity_forms/2-cb870933d1056667864f32a0efe9bcff/2018/08/MDC3521112.JPG

Can you please help to write a trigger to truncate the junk characters. Thanks a lot 

 
Best Answer chosen by Rahul H 4
Sandeep WaliaSandeep Walia
Hi Rahul,

The only challenge in the above problem was to use escape sequence for the backslash('\') character and to write a special case for the 'https:\\' as that is the only string where '\/' has to be replaced with a backslah whereas everyehere else it has to be replaced with forward slash.
The below code works according to your use case:
Account acc = [SELECT Id,Description FROM Account WHERE Name ='test String'];
String  s = acc.description;
s = s.replace('https:\\/\\/','https:\\\\');
s = s.replace('\\/','/');
System.debug(s);

Replace the description field with your text field.

Input:

User-added image

Output:
User-added image

Hope this helps,
Sandeep 

All Answers

Sandeep WaliaSandeep Walia
Hi Rahul,

The only challenge in the above problem was to use escape sequence for the backslash('\') character and to write a special case for the 'https:\\' as that is the only string where '\/' has to be replaced with a backslah whereas everyehere else it has to be replaced with forward slash.
The below code works according to your use case:
Account acc = [SELECT Id,Description FROM Account WHERE Name ='test String'];
String  s = acc.description;
s = s.replace('https:\\/\\/','https:\\\\');
s = s.replace('\\/','/');
System.debug(s);

Replace the description field with your text field.

Input:

User-added image

Output:
User-added image

Hope this helps,
Sandeep 
This was selected as the best answer
Sandeep WaliaSandeep Walia
Please refer this thread https://salesforce.stackexchange.com/questions/143603/unexpected-token-trigger. You might find it helpful.

Thanks,
Sandeep