+ Start a Discussion
TheLearnerTheLearner 

Concating the text while displaying the record

Hi Experts,

My requirement is that, i enter the value in the  Text box field called NCT09a Traffic Management(NCT09a_Traffic_Management__c )= 'two way lights to be used'. Then i have field called  Condition Text(Condition_Text__c ) in that box value has to display like this  'NCT09a two way traffic lights to be used'. This is my trigger ,like this there are 20  fields are there i will enter the in those fields and values has to display in that field ,but i posted only one field , could anyone help me please.

trigger Update_Condition_Text on SW_Condition__c (before insert, before update) 
{
    String newConditionType='';
    for(SW_Condition__c swCon:trigger.new)
    {
        if(swCon.Condition_Text__c != null && swCon.Condition_Text__c != '')
            newConditionType = swCon.Condition_Text__c;
      
        if(trigger.isInsert)
        {
          if(swCon.NCT09a_Traffic_Management__c !='' && swCon.NCT09a_Traffic_Management__c != null)
                newConditionType = newConditionType + ', NCT09a Traffic Management : '+swCon.NCT09a_Traffic_Management__c ;
        }
     if(trigger.isUpdate)
        {
             if((swCon.NCT09a_Traffic_Management__c !=Trigger.oldMap.get(swCon.ID).NCT09a_Traffic_Management__c) && swCon.NCT09a_Traffic_Management__c != null && swCon.NCT09a_Traffic_Management__c !='')
                newConditionType = newConditionType + ', NCT09a Traffic Management: '+swCon.NCT09a_Traffic_Management__c;
       }
    if(newConditionType != null && newConditionType != '')
        {
            newConditionType = newConditionType.removeStart(', ');
            if(newConditionType.length()>500)        
            {
               swCon.addError('Edit text in the fields in the section NCT Details to make it 500 or less so record can save');
            }
            else
                swCon.Condition_Text__c = newConditionType;
        }
    }
}
pconpcon
What I would do is the following code:
 
List<String> fieldNames = new List<String>{
    'NCT09a_Traffic_Management__c',
    'field2__c',
    'field3__c'
};

Map<String, String> fieldLabels = new Map<String, String>{
    'NCT09a_Traffic_Management__c' => 'NCT09a Traffic Management',
    'field2__c' => 'Field Label 2',
    'field3__c' => 'Field Label 3'
};

List<String> parts = new List<String>();

for (SW_Condition__c swCon : Trigger.new) {
    SW_Condition__c oldCon = null;

    if (Trigger.isUpdate) {
        oldCon = Trigger.oldMap.get(swCon.Id);
    }

    for (String fieldName : fieldNames) {
        if (
            swCon.get(fieldName) == null || 
            swCon.get(fieldName).trim() == ''
        ) {
            continue;
        }

        if (
            Trigger.isInsert ||
            oldCon.get(fieldName) != swCon.get(fieldName)
        ) {
            
        } else if (Trigger.isInsert) {
            parts.add(fieldLabels.get(fieldName) + ': ' + swCon.get(fieldName));
        }
    }

    if (parts.isEmpty()) {
        continue;
    }

    swCon.Condition_Text__c = String.join(parts, ', ');
}
NOTE: This code has not been tested and may contain typographical or logical errors.

This makes the whole trigger a lot cleaner and more scalable.  Since we are using a list on line 1 to name your fields you can control the order in which everything is concatenated.

Alternately you could probably do this in a regular formula field
TheLearnerTheLearner
HI Pcon,

Thanks for the reply but  is alreay existing trigger with lot of fields so , i need to do amendment in that, so its not possible to me change the entire code in the tirgger just i gave you example of one field with requirement. so could you please make an amendement in my code only.