+ Start a Discussion
cribiscribis 

Concatenating Two Fields at the end of a trigger

I am working on a trigger that creates a new record and conducts conversion at the same time, while adding a label at the end of the value. I am now trying to update the source record with the value in the same trigger.

 

 For example. existing record has 60 miles in field a. The trigger will duplicate the record and calculate the miles in to kilometers. The final value in field a on the second record of 100 km. I am having trouble with changing the 60 to 60 miles in the source field on the source record.

 

How do I do this?

 

Here is my trigger that is working successfully to this point:

 

trigger BulkDensityConversion on Customer_Bulk_Density__c (before insert) {
    Set<Id> bIds = new Set<Id>();

list<Customer_Bulk_Density__c> customerBulkDensityList = new List<Customer_Bulk_Density__c>();

if(StaticClass.doNotExecute ==true)
{
system.debug('Inserting'+StaticClass.doNotExecute);
    for(Customer_Bulk_Density__c c:trigger.new)
    {
     bIds.add(c.id);
     
     if(c.Metric_Label__c =='Lbs./ft3')
     {
     Double conversion = Double.valueof(c.Name) * 16.018;
        
      Customer_Bulk_Density__c s = new Customer_Bulk_Density__c(
        Metric_Label__c = 'Kg/m3',
        Name = String.Valueof(conversion) + String.ValueOf(' ') + String.Valueof('Kg/m3'),
        Customer_Product__c=c.Customer_Product__c,
        Customer_Product_2__c=c.Customer_Product_2__c,
        Customer_Product_3__c=c.Customer_Product_3__c
        );
        customerBulkDensityList.add(s);   
        }
         else
        {
         Double conversion1 = Double.valueof(c.Name) * 1 / 16.018;
        
         Customer_Bulk_Density__c s = new Customer_Bulk_Density__c(
         
        Metric_Label__c = 'Lbs/ft3',
        Name = String.Valueof(conversion1) + String.ValueOf(' ') + String.Valueof('Lbs./ft3'),
        Customer_Product__c=c.Customer_Product__c,
        Customer_Product_2__c=c.Customer_Product_2__c,
        Customer_Product_3__c=c.Customer_Product_3__c
        
        );
        customerBulkDensityList.add(s);   
        } 
    }
    StaticClass.doNotExecute =false;
    if(!customerBulkDensityList.isEmpty())
    {
        insert customerBulkDensityList;
    }
    
    System.debug('****1 : B Id size '+ bIds.size());
 
  }
  else
  {
  StaticClass.doNotExecute =true;
  }

}

 

Thank you

Best Answer chosen by Admin (Salesforce Developers) 
cribiscribis

Didn't think I could, but was able to use a workflow for this solution, using the following formula in the workflow rule:

Name + ' ' + Text( Metric_Label__c )

All Answers

bob_buzzardbob_buzzard

When you say you are having trouble, can you give us a bit more information?  E.g. are you getting errors, don't know where to start etc?

 

Also, have you got some code that tries to do this and fails?  If you post that we may be able to give some targetted help.

 

Finally, what are the field types that you are trying to change?

 

If the field containing '60' in the second record is a text field, I wouldn't expect there to be any problems changing this to '60 miles'.

cribiscribis

Thank you for the clarification questions. I don't know where to start. I am starting with a text field and a picklisst. The value chosen in the picklist should be added to the end of the text field value. on the original record. For example. The text field will contain 60, and the picklist will contain miles as one of the choices. In the same record I need to add miles to the text field after the 60, resulting in "60 miles."

bob_buzzardbob_buzzard

You should just be able to set the value into the new record - picklist values can be pretty much treated like text in a trigger.

  E.g. if you have a record 'myObj', with picklist field 'Pick_Field__c'  and text field 'Text_Field__c' and you want to concat into 'Concat_Field__c':

 

 

myObj.Concat_Field__c=myObj.Pick_Field__c + ' ' + myObject.Text_Field__c;

 

 

 

cribiscribis

Didn't think I could, but was able to use a workflow for this solution, using the following formula in the workflow rule:

Name + ' ' + Text( Metric_Label__c )

This was selected as the best answer