2

I created the following trigger to extract lead information coming into Salesforce from G2. I have been able to extract the value that comes after each line that I declare using contains but I need help deleting the prefix words of each line.

For example, I need help deleting the text '*Email: *' - I would think the Line Remove function would do it, but when the record save it continues to copy the "*Email: *" portion + the name of the submitter

Here is my code - thank you all as this is my first apex trigger:

trigger GrabCaseDescriptionText on Case (before insert, before update)  {
for (Case c: Trigger.new) {
    if (c.Description != null) {
        List<String> lines = new List<String>();
        lines = c.Description.split('\n');
        
        for (String line: lines) {
            if(line.contains('*EMAIL: *')){
                line.remove('*EMAIL: *');
                system.debug(line);
                c.CaseEmailAddress__c = line;
            }
             if(line.contains('*NAME:* ')){
                line.remove('*NAME:* ');
                system.debug(line);
                c.Submitter_Name__c = line;
            }
            if(line.contains('*ACTION TAKE ON G2:* ')){
                line.remove('*ACTION TAKE ON G2:* ');
                system.debug(line);
                c.Action_Take_on_G2__c = line;
            }
            if(line.contains('*INDUSTRY:* ')){
                line.remove('*INDUSTRY:* ');
                system.debug(line);
                c.Industry__c = line;
            }
            if(line.contains('*COMPANY SIZE:* ')){
                line.remove('*COMPANY SIZE:* ');
                system.debug(line);
                c.Company_Size__c  = line;
            }
            if(line.contains('*COMPANY:* ')){
                line.remove('*COMPANY:* ');
                system.debug(line);
                c.Submitter_s_Company__c  = line;
            }
            if(line.contains('PHONE NUMBER: *')){
                line.remove('PHONE NUMBER: *');
                system.debug(line);
                c.Submitter_s_Phone__c  = line;
            }
            
            
        }
        if(c.CaseEmailAddress__c !=null){
            List<Contact> cons = [SELECT Id FROM Contact WHERE Email=:c.CaseEmailAddress__c LIMIT 1];
            if(cons.size()>0)c.ContactId=cons[0].id;
        }
    }
} 
2
  • Are you trying to remove those substrings from the Description, or only the fields you copy data into?
    – Adrian Larson
    Aug 15, 2022 at 23:35
  • I believe only the fields I copy data into. Because when this triggers, the email field I am copying from description into a new field called CaseEmailAddress will come out as *EMAIL: *[email protected] And therefore, I just want the new field CaseEmailAddress to say [email protected]
    – andrew
    Aug 15, 2022 at 23:56

1 Answer 1

1

In Apex, strings are immutable. This means that no string method will modify an existing string, but instead will return a new copy of the string with the desired operation applied.

That means your code should look like:

c.CaseEmailAddress__c = line.remove('*EMAIL: *');
2
  • 1
    OMG THANK YOU! If I become a contributing member to this forum I will say that you were my first inspiration lol - this gives me hope for my Apex journey
    – andrew
    Aug 16, 2022 at 0:00
  • @andrew thanks! Your first question is definitely higher quality than most, I'd think you'd be a great contributor. 😊
    – sfdcfox
    Aug 16, 2022 at 0:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .