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
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Trigger to concantenate fields of child object and display n parent object--- please help!!!1

Hi All,

      I have Notes object looking up to a parent custom object . there are 3 fields on Notes object(A,B,C) and there is a custom field D__c on Custom object of text type, I want to write a trigger so that D__c = A+B+C (concantenation of 3 fields) , upon edit or insert of child Notes records.Please help me with the bulkified code.

 

Regards

Shrey Tyagi

Best Answer chosen by Admin (Salesforce Developers) 
Shweta_AgarwalShweta_Agarwal

Sample code : please replace Bigmachine__c wtih your parent custome object

trigger UpdateParent on Notes__c (after insert, after Update) {
set<id> noteIds = new SET<id>();
for(Notes__c note : Trigger.new ){
noteIds.add(note.Bigmachine__c);
}
List<Bigmachine__c> bigList = [select id, D__c from Bigmachine__c where Id In: noteIds];
for(Notes__c note : Trigger.new ){
for(Bigmachine__c big : bigList){
if(note.Bigmachine__c == big.id){
big.D__c = note.A__c + ' ' + note.B__c + ' ' + note.C__c;
}
}
}
if(bigList.Size() > 0){
update bigList;
}
}

All Answers

hitesh90hitesh90

Hi shreya,

 

Below is the sample trigger as per your requirement.

 

Try to use that trigger on before insert and update event.

 

Apex Trigger:

trigger trgConcatefield on Notes__c (before insert, before update) {
	for(Notes__c nt: trigger.New){
		D__c = A__c + ' ' + B__c + ' ' + C__c;
	}
}

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

Shweta_AgarwalShweta_Agarwal

Sample code : please replace Bigmachine__c wtih your parent custome object

trigger UpdateParent on Notes__c (after insert, after Update) {
set<id> noteIds = new SET<id>();
for(Notes__c note : Trigger.new ){
noteIds.add(note.Bigmachine__c);
}
List<Bigmachine__c> bigList = [select id, D__c from Bigmachine__c where Id In: noteIds];
for(Notes__c note : Trigger.new ){
for(Bigmachine__c big : bigList){
if(note.Bigmachine__c == big.id){
big.D__c = note.A__c + ' ' + note.B__c + ' ' + note.C__c;
}
}
}
if(bigList.Size() > 0){
update bigList;
}
}

This was selected as the best answer