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
Surendra123Surendra123 

Splitting the field in product trigger

Hi I have two fields in product object where i need to split the data and send to the other custom field for this i have split the data

eg:cable/1234  1234 will be placed in the other custom field there are some records like cable/MO1234 i just want to remove the string Mo for such records any help or make them as default to be 1 for such records ant help thanks in advance

trigger ProductFieldUpdate on Product2 (Before insert, before update){
    for(Product2 p : trigger.new){
  
        if(p.Ampics_PL__c != null && p.Ampics_PL__c == 'UTILUX'  ) {
        if(p.description!= null && p.description.lastIndexOf('/') != -1) {
      
      
      
        Integer lastIndx = p.description.lastIndexOf('/');
        p.GPL_Desc__c  = string.valueOf(p.description.subString(lastIndx + 1));
       
      
        }
       
           } else {
        p.GPL_Desc__c = '1';
           }
        }
        }

cmoylecmoyle

Well...I can't really understand what you're asking, but I'm going to try and answer this. It sounds like you are trying to split a string up to populate other fields. To do this, Strings have a 'Split' method that you can use to break up a string into an array.

 

So when String a = 'cable/1234' and you want to break it up into 2 strings based on the '/'

String[] splitString = a.split('/', 0);

String firstString = splitString[0];    //Will equal 'cable'

String secondString = splitString[1];     //Will equal '1234'

 

Hopefully this answers your question.

 

Also the line p.description.lastIndexOf('/') != -1 may not work. I would recommend changing it to !p.description.contains('/')

 

Good luck!