You need to sign in to do that
Don't have an account?
Sainath 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.
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.
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:
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 ii) and Term_Name__c as iii) Write your trigger as :
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
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: 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: 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 and,
hed__Term_Name__c as and then using it in trigger like:
I hope this works for you. If it does, please like it and mark as Best Answer to help others too.
Thank you.