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
HilineHiline 

Case Trigger Not Working

Hello,

 

I have two custom lookup fields in my Case object:  User_Contact__c (User lookup)  and Opportunity_Name__c (Opportunity lookup).  I wrote a trigger to update the User_Contact__c to the Opportunity owner ID from the  Opportunity_Name__c field.  Below is my trigger code:

 

trigger Case_OpptyAttached on Case (before insert, before update) {
    
    for (case c : trigger.new) {
    
        if (c.User_Contact__c != c.Opportunity_Name__r.OwnerId) {
        
            c.User_Contact__c = c.Opportunity_Name__r.OwnerId;
        }
    }
}

 

 

When I update the case record, the custom User lookup field is blank.  Is my apex syntax incorrect?

 

Thank you so much.

 

David

Best Answer chosen by Admin (Salesforce Developers) 
SFDC-SDSFDC-SD

Below is logic that can get you the required details ... optimize code for bulk processing...

 

trigger Case_OpptyAttached on Case (before insert, before update) {

    Set<Id> cIds = new Set<Id>();

    for(case c : trigger.new)
       cIds.add(c.Opportunity_Name__c);   

    List<opportunity> opp = [select Id, OwnerId from Opportunity where Id IN :cIds] ;
      
    for (case c : trigger.new) {
        if (c.User_Contact__c != opp[0].OwnerId)       
            c.User_Contact__c = opp[0].OwnerId;

      }

All Answers

MiddhaMiddha

In a trigger, you only get values of the object on which the trigger is written. If you want to refer values in the parent object you have to query them.

 

In your case, you'll only get the value c.Opportunity_Name__c hence collect all the Opportunoity Ids in a set and query the Opportunity records to fetch Opportunity Owner Id and then assign those values.

Gunners_23Gunners_23

At the time of initialization only the information regarding the object is populated, inorder to get the information of the related

 

objects you need to query based on "Id" of the related object and get the details and need to populated the details in the object

 

which you want to insert

SFDC-SDSFDC-SD

Below is logic that can get you the required details ... optimize code for bulk processing...

 

trigger Case_OpptyAttached on Case (before insert, before update) {

    Set<Id> cIds = new Set<Id>();

    for(case c : trigger.new)
       cIds.add(c.Opportunity_Name__c);   

    List<opportunity> opp = [select Id, OwnerId from Opportunity where Id IN :cIds] ;
      
    for (case c : trigger.new) {
        if (c.User_Contact__c != opp[0].OwnerId)       
            c.User_Contact__c = opp[0].OwnerId;

      }

This was selected as the best answer
HilineHiline

Thank you very much SFDC-SD, that works! 

SFFSFF

Small problem with this trigger - it will assign all Cases to the first Opportunity found. I think you need something like this:

 

trigger Case_OpptyAttached on Case (before insert, before update)
{
   set<Id> cIds = new set<Id>();
   for (Case c : trigger.new)
      cIds.add(c.Opportunity_Name__c);   

    map<Id, Opportunity> oppMap = new map<Id, Opportunity>(
      [SELECT Id, OwnerId FROM Opportunity WHERE Id IN :cIds]);
      
   Opportunity o;
   for (Case c : trigger.new)
   {
      o = oppMap.get(c.Opportunity_Name__c);
      if (o != null)
      {
         if (c.User_Contact__c != o.OwnerId)
            c.User_Contact__c = o.OwnerId;
      }
   }
}

This will look up the correct Opportunity every time and assign the owner of that Opportunity as the user contact for the Case.

 

Good luck!