2

I have about 20 text and picklist fields from a web-to-lead form that I am looking to concatenate into one long text field on the lead record on insert. So for instance, I would want the field to look something like this: Product Name: dksjfkldsfjsldk; Product Type: jsdfkdjfkds; Product Size: fjdfjsdk;

However, I'd ideally like to only display the text if the field is filled in on the web form. So for instance, if product type were left blank, then product type would not appear in this concatenated field.

My apex skills are rudimentary though I think this should be a rather simple trigger. Is it enough to do a series of If statements, something along the lines of below?

trigger UpdateMiscLeadInfo on Lead (before insert) { List Leads = new List{}; miscstring; for (Lead l: Trigger.new) { If (l.Other_Manufacturer__c != Null) { miscstring += 'Other Manufacturer: ' + l.Other_Manufacturer__c + '; '}; If (l.product_name__c != Null) { miscstring += 'Product Name: ' + l.Product_Name__c + '; '}; 

.....

Any advice is much appreciated--thank you!

1
  • 2
    This can be done via a workflow rule field update, although, I question the need for this at all.....I would think long and hard about "Why" you are doing this and see if there is a better way to accomplish your goals. Commented Jul 29, 2015 at 14:56

3 Answers 3

4

Any reason why you have to use a trigger? I always recommend against coding where you can use the declaritive approach ("clicks, not code").

if you create a formula field, it should achieve the same results, and will dynamically update if any of the referenced fields are changed after the record is inserted.

  1. Go to Setup> Lead> Fields.
  2. Under the Lead Cusom Fields & Relationships section, click New.
  3. Set Data Type to Formula
  4. Give the field a name, such as Web Lead Submission
  5. Set Formula Return Type to Text
  6. Enter the following formula in the advanced formaul section

    IF(ISBLANK(Product_Name__c),"","Product Name: " + Product_Name__c + BR()) + IF(ISBLANK(Product_Type__c),"","Product Type: " + Product_Type__c+ BR()) + IF(ISBLANK(Product_Size__c),"","Product Size: " + Product_Size__c+ BR())

You can continue adding a new line for each field...

What this formula does is checks whether the field is blank/null. If it is, then display nothing; ELSE, display header text and field value. This way, the formula results will only show data when it is populated and keep things neat.

The expected out output for users will be:

Product Name: Example Product

Product Type: Premium

Product Size: Large

If any values were missing, the line would simply not appear.

BR() is a simple line break.

If you require semicolon at the end of each value, you can replace BR() with ";". Now to avoid having a semicolon left at the end, you'll need to modify the formula to:

Substitute( IF(ISBLANK(Product_Name__c),"","Product Name: " + Product_Name__c + ";") + IF(ISBLANK(Product_Type__c),"","Product Type: " + Product_Type__c+ ";") + IF(ISBLANK(Product_Size__c),"","Product Size: " + Product_Size__c+ ";") + [continue with remainder of desired fields...] + ";" , ";;", "")

By appending an extra semicolon at the end of your string, you'll always end up with ";;" as the last 2 characters in your string. Apply a Substitute() function to replace ";;" with an empty string "" so you don't have an extra semicolon at the end.

2
  • 3
    Great solution, but if the user really wants semicolons, an extra one would appear at the end. That's either insignificant or a showstopper, depending on who you ask. Commented Jul 29, 2015 at 15:48
  • Thanks and good point. I'll append to my answer a clever workaround. Commented Jul 29, 2015 at 15:57
1

And if you want to go further on eliminating duplication in the code you can drive the logic by a collection of SObjectField objects:

public class Leads { private static final SObjectField[] FIELDS = new SObjectField[] { Lead.Other_Manufacturer__c, Lead.Product_Name__c, .... }; public static String buildInfoString(Lead l) { String[] items = new String[] {}; for (SObjectField f : FIELDS) { Object o = l.get(f); if (o != null) items.add(f.getDescribe().getLabel() + ': ' + o); } return String.join(items, '; '); } } 

with the class called from the trigger:

for (Lead l: Trigger.new) { String miscstring = Leads.buildInfoString(l); ... } 
3
  • I thought about suggesting this, but the describes are kind of costly in bulk. However, if you used a map to store the labels, you'd get a big boost on speed. Commented Jul 29, 2015 at 15:31
  • @sfdcfox As well as the governor limits going seems like these are well cached behind the scenes now; just tried 10 different lead field describes and they are taking about 3ms. Not super fast but fast enough. Commented Jul 29, 2015 at 15:43
  • Interesting indeed. I like the idea of being able to describe on command, in the interest of reducing overall heap usage. I'm glad they made describes more reasonable than they once were. Commented Jul 29, 2015 at 15:50
0

You've got the basic premise right, but there is a slightly better way to implement concatenation: String.join.

for(Lead. Record: Trigger.new) { String[] parts = new String[0]; if(record.field1!= null) { parts.add('Field 1: '+record.field1); } ... record.concatField = String.join(parts, '; '); } 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.