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
Pal AduPal Adu 

Help with a Field Update Trigger

I need the field on Trip Report>Company Overview to be updated on the Display page with the value from Account>Company Overview once a new trip report is saved.


trigger UpdateCompanyOverviewField on Trip_Report__c (after insert,after update) {
    Trip_Report__c trip=trigger.new[0];
            if(trip.Company_Name__c!=null){
                trip.Company_Overview__c = [SELECT Company_Overview__c FROM Account WHERE Name=:trip.Company_Name__c];
                update trip;
            }
}
Error: Compile Error: Illegal assignment from LIST<Account> to String at line 4 column 17

I did try using the Formula field and a field update workflow to implement this but it didn't work because
A. Trip Report>COmpany Name field is a lookup field to Account
B. The Company Overview fields are both Text Area fields.

Any help please
Virendra ChouhanVirendra Chouhan
Hello Pal Adu,

Soql query return a list and you store the result in a field it is not possible.
you have some options-
  1. Use LIMIT cause in query. i.e
    Account acc = [Select Company_Overview__c From Account Where Name =:trip.Company_Name__c limit 1];
    trip.Company_Overview__c = acc.Company_Overview__c;
    
    
  2. Use this code--
    trip.Company_Overview__c = [SELECT Company_Overview__c FROM Account WHERE Name =:trip.Company_Name__c].Company_Overview__c;
It'll help you.

Pal AduPal Adu
Hi Virendra,

I used your 2 option and even though the trigger got saved- the Details page throws an error on save

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger FetchCompanyOverviewFromAccount caused an unexpected exception, contact your administrator: FetchCompanyOverviewFromAccount: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.FetchCompanyOverviewFromAccount: line 5, column 1
Pal AduPal Adu
Any suggestions on what I could do next?
Virendra ChouhanVirendra Chouhan
Hello Pal Adu,

Firstly Bulkfy your trigger. Iterate the trigger Contaxt.
trigger UpdateCompanyOverviewField on Trip_Report__c (after insert,after update) {
list<Trip_Report__c> Tlist = new List<Trip_Report__c>();  
for(Trip_Report__c trip : trigger.new)
{
   if(trip.Company_Name__c!=null){
     trip.Company_Overview__c = [SELECT Company_Overview__c FROM Account WHERE Name=:trip.Company_Name__c].Company_Overview__c;
     Tlist.add(trip);
         
}
update Tlist;  
}
try this.