0

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) {
List<hed__Course_Offering__c> con = [select Id,hed__Course__r.Name from hed__Course_Offering__C];
for(hed__Course_Offering__c con : trigger.New){
    string nameupd = '';
    nameupd += con.hed__Course__c +','+ con.hed__Term__c ;
    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.

1

Lookup values always contain the ID values and not Name values, if you need Name values, you will have to do an explicit query on that respective object.

Also, you won't be able to access the related fields using the relationship field, as only the fields of the object that is in transaction will be accessible in trigger.

So, you trigger should be something like below. Assuming hed__Course__c as the object name of Course and hed__Term__c as object name of Term, if they are not correct, then replace it with the right API names in below code.

trigger ModuleOfferingName on hed__Course_Offering__c (before insert) { 
    Set<Id> setCourseName = new Set<Id>();
    Set<Id> setTermName = new Set<Id>();
    for(hed__Course_Offering__c con : trigger.New){ 
        if(string.isNotBlank(con.hed__Course__c)){
            setCourseName.add(con.hed__Course__c);
        }
        if(string.isNotBlank(con.hed__Term__c)){
            setTermName.add(con.hed__Term__c);
        }
    }
    Map<id,hed__Course__c> mapCourse = new Map<id,hed__Course__c> ([select Id,Name from hed__Course__c WHERE id IN: setCourseName]); 
    Map<id,hed__Term__c> mapTerm = new Map<id,hed__Course__c> ([select Id,Name from hed__Term__c WHERE id IN: setTermName]); 

    for(hed__Course_Offering__c con : trigger.New){ 
        con.Name = mapCourse.containsKey(con.hed__Course__c) ? mapCourse.get(con.hed__Course__c).Name : '' + ',' + mapTerm.containsKey(con.hed__Term__c) ? mapTerm.get(con.hed__Term__c).Name : '';
    } 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy