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
Sahil YadavSahil Yadav 

Hello Mates needed some help on String Methods

There is dicription field in case obj
where it contains 
Customer Name: Smith Ng Customer Email: smith.ng@.com
now my task is basically to get the get the name from yhe description field and populate to some other field in case obj
Just name that is Smith Ng 
any suggestions !!!!
String casedesc = caserec.Description;
                                String SuppliedName = casedesc.substringAfter('Name:');
                                System.debug('@@'+SuppliedName);
                                String sn = SuppliedName.substringBefore(' Custumer Email:');
                                
                                
                                 System.debug('@@'+sn);
                                //caserec.SuppliedName = SuppliedName;

 
Best Answer chosen by Sahil Yadav
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sahil,

Can you try something like below.

 
String casedesc =  'Customer Name: Smith Ng Customer Email: smith.ng@.com';
String s2 = casedesc.substringBetween('Customer Name:',' Customer Email:');
string s3=casedesc.SubStringAfter('Customer Email:');
system.debug('name'+s2);
system.debug('email'+s3);

If this solution helps, please mark it as best answer.

Thanks,
​​​​​​​

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sahil,

Can you try something like below.

 
String casedesc =  'Customer Name: Smith Ng Customer Email: smith.ng@.com';
String s2 = casedesc.substringBetween('Customer Name:',' Customer Email:');
string s3=casedesc.SubStringAfter('Customer Email:');
system.debug('name'+s2);
system.debug('email'+s3);

If this solution helps, please mark it as best answer.

Thanks,
​​​​​​​
This was selected as the best answer
Sahil YadavSahil Yadav
Thank You Sai Praveen its worked but could we achieve this using regex ?
trigger caseNameEmailValidate on Case (before insert) {
    for(Case caserec : trigger.new)
       {
               if(caserec <> Null)
               {        
                   if (caserec.RecordTypeId <> Null && caserec.RecordTypeId == Schema.SObjectType.Case.getRecordTypeInfosByName().get('Internal Support').getRecordTypeId() && caserec.Origin == 'Email-to-Case')       
                   {
                         if(caserec.SuppliedEmail == Null && caserec.SuppliedName == Null )
                            {
                                String casedesc = caserec.Description;
                                String Name = casedesc.substringBetween('Customer Name:',' Customer Email:');
                                System.debug('@@'+Name);
                                caserec.SuppliedName = Name;
                                
                                //String sn = SuppliedName.substringBefore(' Custumer Email:');
                                
                                
                                // System.debug('@@'+sn);
                                //caserec.SuppliedName = SuppliedName;
                                
                                //Matcher m = Pattern.compile('[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+').matcher(casedesc);
                                Matcher m = Pattern.compile('[Customer Email]+[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+').matcher(casedesc);
                                
                               // Matcher m1 = Pattern.compile('[Custumer Name]+[A-Za-z ]+').matcher(casedesc);
                               
                               // Custumer_Name__c ,"([a-zA-Z ])*")
                                
                                String varCustomerEmail;
                                
                                //String varCustomerName;
                                
                                if (m.find()) 
                                 {
                                     varCustomerEmail = m.group();
                                     System.debug('match = '+varCustomerEmail);
                                     caserec.SuppliedEmail = varCustomerEmail;
                                 }
                                /*
                                if(m1.find()){
								     varCustomerName = m1.group();
									 System.debug('match = '+varCustomerName);
                                    caserec.SuppliedName = varCustomerName;
									 
									 
								 }*/
                                /*
                                 else 
                                 {
                                      System.debug('Customer Email - No match found');
                                 }*/

 

                       }
                       }//
               }
    

}
}