4

How can I retrieve the first word in a string in Salesforce? Something like this (this is Ruby, but you get the idea):

"John Smith".split(' ').first
-> "John"

Any help? Thanks.

Update

Great suggestions, but they're not exactly succinct. Is the best way really to create another field for the sObject and store it in there?

Update two

Here's the code:

Hi {!Lead.FirstName},
<br><br>
Congratulations on taking Step 1 today with {!LEFT(Lead.Picklist_Value__c, IF(FIND(Lead.Picklist_Value__c, " ") = 0, LEN(Lead.Picklist_Value__c), FIND(Lead.Picklist_Value__c, " ")))}!
<br><br>
Best Wishes,
<br>
{!Lead.Picklist_Value__c}

The first returns blank, the second returns the whole name. I've tried all the suggestions in this post, too.

  • do you need to do this on the VF page itself with a formula expression or in the controller? – cropredy Mar 24 '16 at 23:00
  • Preferably in the VF page since it's an email template and without its own controller. – okay56k Mar 24 '16 at 23:07
  • There are 3 solutions suitable for email templates, and they are working for me. Could you post your code? Are you sure that field level security is fine? – kurunve Mar 24 '16 at 23:26
  • Updated. Should be all good? – okay56k Mar 24 '16 at 23:30
  • 1
    @CD-RUM unfortunately, when your field is picklist type, those doesn't work, (no idea why). Probably, fastest solution would be custom formula creation. – kurunve Mar 25 '16 at 6:11
4

In Visualforce, you can use normal formula syntax:

{!LEFT(lead.Picklist_Value__c, FIND(' ', lead.Picklist_Value__c))}
  • Returns blank, too. And the field definitely has words in it. Do I need to convert it to a string or something first? All these attempts are returning blank in the email template. – okay56k Mar 24 '16 at 23:11
  • 1
    Love the moniker. Anyways, I actually did test this before posting. Possibly you're referencing the wrong object? Might help if you copy and paste your template in. – sfdcfox Mar 24 '16 at 23:17
  • Ha, thanks. I've posted a bit of it in update two above. – okay56k Mar 24 '16 at 23:32
  • aak - how could I have forgotten about LEFT(..) . I use it all the time – cropredy Mar 24 '16 at 23:58
  • 1
    @CD-RUM Your find parameters are reversed. The space has to come first. – sfdcfox Mar 25 '16 at 0:05
4

In apex, you could use the String.substringBefore(string) method. Essentially the same behavior as the .split(' ')[0] mechanism but substringBefore uses a string instead of a RegEx pattern.

String s1 = 'John Smith';
String s2 = s1.substringBefore(' ');
System.assertEquals('John', s2);
  • 2
    While it doesn't matter much for small strings, split actually accepts a pattern instead of a literal string, thus adding some processing overhead. – sfdcfox Mar 24 '16 at 23:12
3

Considered next scenarios:

Apex

Split method in salesforce returns array of string, so you can just use:

'John Smith'.split(' ').get(0)

or

'John Smith'.split(' ')[0]

Visualforce or formula

You can use combination of LEFT, LEN and FIND formulas to find first separator in variable if that exists For example, if separator is space and variable is text__c. so, formula would look like next:

{!LEFT(text__c,IF(FIND(" ",text__c) = 0, LEN(text__c), FIND(" ",text__c )))}

Literally that mean pick left N character of string, where N is position of first space if spaces are in string or N is length of string if string doesn't contain spaces.

  • {!lead.Picklist_Value__c.split(' ')[0]} returns blank? – okay56k Mar 24 '16 at 22:56
  • 1
    also, check for pickList_value__c == null before split – cropredy Mar 24 '16 at 22:59
1

To get the first word out of a field value in a VF email template without a corresponding formula field on the sobject or a VF component controller....

Use a formula expression.

Here, the merge field is relatedTo.foo__c

{!IF(ISBLANK(relatedTo.foo__c),NULL, /* null field displays as null */
  IF(NOT(CONTAINS(relatedTo.foo__c,' ')),relatedTo.foo__c, /* no blank, use whole fld */
  MID(relatedTo.foo__c, 1, FIND(' ', relatedTo.foo__c)) /* locate blank and substr to it (inclusive) */
))}  

Your use case may need to use recipient.foo__c if that is the field you are referencing is who is getting the email.

0

Since this question pertained to picklists, and no solutions here work in VF for picklists, I'll post my solution. It was to use @kurunve's split(' ')[0] method in an Apex trigger and fill out another custom field like so:

trigger ValueLead on Lead (before insert, before update) {
    for (Lead lead: Trigger.new) {
        if (lead.Picklist_Value__c != null) {
            lead.Picklist_Value_First_Name__c = lead.Picklist_Value__c.split(' ')[0];
        }
    }
}

Not ideal but it works.

  • Why did the FIND( ) method not work? Did you correct the problem you have in your Update 2 edit of your question with the parameters being in the wrong order as noted above and still not have success? – Mark Pond Mar 25 '16 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.