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
James_AdapxJames_Adapx 

Trigger issue updating fields

So I am trying to do something very simple but it doesnt want to update the field. After a user creates a case I want to update a field in that case based on what the user entered. Problem is it just doesnt put anything in there. The field is a text box on the case. This seems like it should put something in there.. Am I missing anything here?
 
trigger updateCase on Case (after update, after insert)
{
Case[] cs = [select Id, AccountId, Account_Owner__c, Account_Owner_Email__c  from Case where Id in :Trigger.new];
 
cs[0].Account_Owner_Email__c = 'Something';  //for now just hard coding something in to see the field actually update

}
SuperfellSuperfell
because "after insert" is run after the row is inserted into the DB. you want a before insert trigger.
James_AdapxJames_Adapx
Thanks, but now it gives me a List has no rows for assignment error because the query  doesnt have an Id yet I am assuming..?
SuperfellSuperfell
You don't need the query, trigger.new contains all the case objects being created,you can just set the fields on those case objects directly.
James_AdapxJames_Adapx
Thanks!
James_AdapxJames_Adapx

One more issue...it doesnt like what I have highlited in red. Error says unexpected token: trigger...

trigger updateCase on Case (before insert)
{

    for (Integer i = 0; i < Trigger.new.size(); i++)
    {
      
       Account[] acc = [select Id, Name, A_P_Contact_Name__c from Account where Name =: Trigger[i].new.Owner ];
       User[] usr = [select Id, Name, Email from User where Name =: acc[0].A_P_Contact_Name__c];           
       Trigger.new[i].Account_Owner__c = acc[0].A_P_Contact_Name__c;
       Trigger.new[i].Account_Owner_Email__c = usr[0].Email;
    }

 


}

James_AdapxJames_Adapx

 

I am alittle further. I can query in the apex data explorer using the AccountId to look for the account's contact just like below. But the code below doesnt work in this trigger...

Error: Compile Error: Invalid bind expression type of SOBJECT:Case does not match domain of foreign key at line 3 column 77

trigger updateCase on Case (before insert)
{
       Case[] newCase = [select id, AccountId from Case where AccountId in :Trigger.new];
       
       Account[] acc = [select Id, Name, A_P_Contact_Name__c from Account where Id =: newCase.AccountId];
       User[] usr = [select Id, Name, Email from User where Name =: acc[0].A_P_Contact_Name__c];           
       Trigger.new[0].Account_Owner__c = acc[0].A_P_Contact_Name__c;
       Trigger.new[0].Account_Owner_Email__c = usr[0].Email; 
   
}

GanuGanu

try this!

trigger updateCase on Case (before insert)
{
      for(Case cid : Trigger.new)
 {       

       //Case[] newCase = [select id, AccountId from Case where AccountId in:ccid];
       
       Account[] acc = [select Id, Name, A_P_Contact_Name__c from Account where Id =: cid.AccountId];//u can directly get the accountId from line no 3

       User[] usr = [select Id, Name, Email from User where Name =: acc[0].A_P_Contact_Name__c];   
       
       cid.Account_Owner__c = acc[0].A_P_Contact_Name__c;
       cid.Account_Owner_Email__c = usr[0].Email;

 }  
}

 

-- Ganu

James_AdapxJames_Adapx
That works, thank you very much..