function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Milan HrdlickaMilan Hrdlicka 

trigger - how to make a String of multiple lines using SOQL query

Hello,

I need some advice on how to write a bulk trigger - I have custom object Branch__c with address details like street, street number, city, postal code etc. and I added a "Test_Coumn__c" in which I want to store these address values as a concatenated String.
I have created a list for each field I want to concatenate (if you can help if another data structure apart of multiple Lists can be used, it will be appreciated).
What I dont know is how to concatenate these multiple list address columns in one and how to put concatenated String (update column) to column Test_Column__c...I need to use this address string later as an argument to function which calls a web service getting latitude and longtitude details. 
Many thanks for advice on this.

Milan

MY TRIGGER:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 trigger updGPStest on Branch__c (before insert, before update) {
  
    List<Branch__c> brStreet = new List<Branch__c>();
    List<Branch__c> brStreetNo1 = new List<Branch__c>();
    List<Branch__c> brStreetNo2  = new List<Branch__c>();
    List<Branch__c> brCity = new List<Branch__c>();
    List<Branch__c> brPostalCode = new List<Branch__c>();
    
    List<Branch__c> addrString   = new List<Branch__c>();
    
    
    for(Branch__c br1 : Trigger.New) {

        brStreet = [SELECT Street__c from Branch__c WHERE Id IN :Trigger.New];
        brStreetNo1 = [SELECT Street_No1__c from Branch__c WHERE Id IN :Trigger.New];
        brStreetNo2 = [SELECT Street_No2__c from Branch__c WHERE Id IN :Trigger.New];
        brCity= [SELECT City__c from Branch__c WHERE Id IN :Trigger.New];
        brPostalCode = [SELECT Postal_Code__c from Branch__c WHERE Id IN :Trigger.New];
        
       // - THIS DOES NOT WORK :
        addrString = brStreet + brStreetNo1;
        
  
      

    }
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Best Answer chosen by Milan Hrdlicka
Amit Singh 1Amit Singh 1
Use below code for your trigger.
 
trigger updGPStest on Branch__c (before insert, before update) {
    for(Branch__c br1 : Trigger.New) {
	    br1.Test_Coumn__c = br1.Street__c+", "+br1.Street_No1__c+" "+br1.Street_No2__c+ ", "+br1.City__c+ br1.Postal_Code__c
    }
}
OR

You can create a workflow rule which action will update that "Test_Coumn__c" field with value(Use a formula to set the new value) like below

Street__c+", "+Street_No1__c+" "+Street_No2__c+ ", "+City__c+ Postal_Code__c

OR 

You can create a formula field with the given formula.

Street__c+", "+Street_No1__c+" "+Street_No2__c+ ", "+City__c+ Postal_Code__c

Let us know if this helps :)

Thanks,
Amit Singh

All Answers

Hemant_JainHemant_Jain
If concatenation and getting the field for webservice is the only requirement. You can use a formula field as well.

Like Test_Column__cwould be the field name to store concatenated values and the formula would be:
 
Test_Column__c = Street__c & ", " & Street_No1__c...All other fields
Amit Singh 1Amit Singh 1
Use below code for your trigger.
 
trigger updGPStest on Branch__c (before insert, before update) {
    for(Branch__c br1 : Trigger.New) {
	    br1.Test_Coumn__c = br1.Street__c+", "+br1.Street_No1__c+" "+br1.Street_No2__c+ ", "+br1.City__c+ br1.Postal_Code__c
    }
}
OR

You can create a workflow rule which action will update that "Test_Coumn__c" field with value(Use a formula to set the new value) like below

Street__c+", "+Street_No1__c+" "+Street_No2__c+ ", "+City__c+ Postal_Code__c

OR 

You can create a formula field with the given formula.

Street__c+", "+Street_No1__c+" "+Street_No2__c+ ", "+City__c+ Postal_Code__c

Let us know if this helps :)

Thanks,
Amit Singh
This was selected as the best answer
Milan HrdlickaMilan Hrdlicka
Hi both,

the formula field solution works, thanks! :)
I will try the other solutions as well later.
Appreciate your help!
Milan