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
Daytona JimDaytona Jim 

Trigger help - 'substring' on a dynamic position

I"m attempting to strip a specific line of text from a text field.  In my current trigger, I can substring the text field but what I really want to do is have the starting position of the substring to be dynamic based on finding specific text.   In other words, when I find the text 'subject:', I want to start at subject and get the next x number characters of text.  It would be great if I could get all text starting at subject and then ending at another string such as :end...but I'd settle for just retrieving subject: plus 100 characters.  Any idea how to use FIND other function to making the starting position of the substring dynamic.

 

This is my trigger.

 

1
2
3
4
5
6
7
8
9
trigger PassLongField on BMCServiceDesk__Incident__c (before update) {
    for (BMCServiceDesk__Incident__c Incident : System.Trigger.new) {

                      
            Incident.Description_Strip_Text__c=Incident.BMCServiceDesk__incidentDescription__c.substring(0,100); 
       
  }

}

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
Incident.Description_Strip_Text__c =
  Incident.BMCServiceDesk__IncidentDescription__c.substring(
    Incident.BMCServiceDesk__IncidentDescription__c.indexOf('subject:'),
    Incident.BMCServiceDesk__IncidentDescription__c.indexOf(':end') );

You should check to make sure that:
* IncidentDescription__c is not blank (String.isBlank)

* Both String.indexOf functions do not return -1.

 

All Answers

sfdcfoxsfdcfox
Incident.Description_Strip_Text__c =
  Incident.BMCServiceDesk__IncidentDescription__c.substring(
    Incident.BMCServiceDesk__IncidentDescription__c.indexOf('subject:'),
    Incident.BMCServiceDesk__IncidentDescription__c.indexOf(':end') );

You should check to make sure that:
* IncidentDescription__c is not blank (String.isBlank)

* Both String.indexOf functions do not return -1.

 

This was selected as the best answer
Daytona JimDaytona Jim

Perfect..than you!