+ Start a Discussion
riffindusriffindus 

capturing field value from lookup object on trigger.new

Hi,

I am writing a update trigger, when the opportunity is updated, captured program__r.name to Progcon.name. but it is not capturing the name.

since i use trigger.new, there is no SOQL to add the lookup object field in SOQL.

can anyone give me some suggestion to it.

when i do Opp.Program__C , it copies the Id of the record but i want name of the record.


Shweta_AgarwalShweta_Agarwal
you need to write SOQL query for Program object and then assign the name  to Progcon.name. 

Elie.RodrigueElie.Rodrigue
Indeed you need to use an soql query :
List<Id> listOfProgramId = new List<Id>();
for(YourObject__c obj : Trigger.new)
{
        if(obj.Program__c != null)
        {
                listOfProgramId.add(obj.Program__c); 
        }
}
if(listOfProgramId.size() > 0)
{
       Map<Id,Program__c> programs = new Map<Id,Program__c>([Select Id, Name from Program where Id in: listOfProgramId ]);
        for(YourObject__c obj : Trigger.new)
        {
               if(obj.Program__c != null)
                {
                        if(programs.containsKey(obj.Program__c))
                        {
                                 obj.ProgconName__c programs.get(obj.Program__c).Name;
                        }
                }
       }
}



By the way, if what you do is really just populate the name of a lookup in a field, you would be better off using a formula field or event a workflow field update.