+ Start a Discussion
Sainath VenkatSainath Venkat 

apex trigger to concatinate and update another field

I am working on one trigger where I need to concatinate two fields and populate in third field.
On hed__Course_Offering__c object, I want to concatinate hed__Course__c and hed__Term__c field and need to populate on Name field. I tried below trigger.
trigger ModuleOfferingName on hed__Course_Offering__c (before insert) {
    for(hed__Course_Offering__c con : trigger.New){
        string nameupd = '';
        nameupd += con.hed__Course__r.Name +','+ con.hed__Term__r.Name ;
        con.Name = nameupd;
}
}
The issue is its populating but the hed__Course__c and hed__Term__c are lookup fields so its populating ids but I want names to populate. Can anyone help me out in this issue please.
 
Manish ArvindManish Arvind
Hi Sainath,
Greetings to you!

To achive your requirement, there are 2 ways:
1) There is no need to create trigger. You need to create a formula field on object hed__Course_Offering__c which formula may as:
hed__Course__r.Name + ',' + hed__Term__r.Name

2) Just create 2 formula fields on object hed__Course_Offering__c to find out Name of Course Name(Course_Name__c) and Term Name (Term_Name__c) separately, as below:
i)        Course_Name__c as
hed__Course__r.Name
ii)       and Term_Name__c as
hed__Term__r.Name
iii)      Write your trigger as :
trigger ModuleOfferingName on hed__Course_Offering__c (before insert) {
    for(hed__Course_Offering__c con : trigger.New){
        string nameToUpdate = con.Course_Name__c + ',' + con.Term_Name__c;
        con.Name = nameToUpdate;
    }
}

I hope you find the above solution helpful. If it does, please like it and mark as Best Answer to help others too.

Thanks and Regards,
Manish Arvind
 
Oshin AgrawalOshin Agrawal
Hi Sainath, 

First of all there is no need to write Trigger to concatinate two fields and populate in third field. You can use formula field for the same.
Where you can create the Name field on hed__Course_Offering__c  as formula text field as following:
hed__Course__r.Name + ',' + hed__Term__r.Name
this will definitly solve your issue but in case you explicitly want to use trigger only then:

The data held in trigger.new (or trigger.old) does not include parent object data.To access it you will need to get it out of the database yourself.
For that you will have to use something like this:
set<id> triggerIds = trigger.new.keyset();
list<hed__Course_Offering__c  > listWithParentData = [select hed__Course__r.Name, hed__Term__r.Name from hed__Course_Offering__c  where id in :triggerIds];
But in this case there will be an extra query so to save that you can create 2 new formula fields :

hed__Course_Name__c as
hed__Course__r.Name
and, 
hed__Term_Name__c  as
hed__Term__r.Name
and then using it in trigger like:
trigger ModuleOfferingName on hed__Course_Offering__c (before insert) {
    for(hed__Course_Offering__c con : trigger.New){
        string nameToUpdate = con.hed__Course_Name__c + ',' + con.hed__Term_Name__c  ;
        con.Name = nameToUpdate;
    }
}

I hope this works for you. If it does, please like it and mark as Best Answer to help others too.
Thank you.