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
Shazib MahmoodShazib Mahmood 

Cross object reference not working in my trigger

Hi guys,

I am trying to write a very simple trigger but running into some issues. On case I created a lookup field to Opportunity.

trigger UpdateCaseAndOppOwnerManager on Case (before update)
{
    for (Case caseselected : Trigger.new)
    {
        caseselected.Opportunity_Owner_Email__c = caseselected.Opportunity__r.Owner.Email; // Issue # 1. Brings back a null value every time.
        caseselected.Case_Owner_Manager_Email__c = caseselected.Owner.Manager.Email;    // Issue # 2. Owner.Manager reference is not valid
    }

 }

       
Issue # 3. I want these fields to be populated actively like a formula field and not just upon insert/update i.e save. What should I look into, to achieve that ? 

Thank you for all the help !

Cheers,
Shaz
Tejpal KumawatTejpal Kumawat
Hello Shaz,

Option # 1 : Best option to populated actively, you need to create  both fields as formula fields .
Opportunity_Owner_Email__c  = Opportunity__r.Owner.Email
Case_Owner_Manager_Email__c = Case_Owner_Manager_Email__c 

Option # 2
Create Workflow with field Update on Opportunity(2 workflow) & case( 2 workflow).

Option # 3
Create two trigger on both object.

If this answers your question then hit Like and mark it as solution!
 
Shazib MahmoodShazib Mahmood
Thanks for the reply Tejpal, I am going with Option # 3 here because of object reference limits. I thought 1 trigger would suffice for updating the two custom fields on case. Perhaps I can get some advice on the 3 mentioned issues of it.
James LoghryJames Loghry
Formula field is definitely the best approach for this in my book.  The issue with your trigger is that Owner is a polymorphic field.  For instance, Owner could refer to a user, group, or queue.  Because of this, it's difficult for SOQL to understand the relationship.  You can do it I believe (See: http://www.salesforce.com/us/developer/docs/dbcom_apex/Content/langCon_apex_SOQL_polymorphic_relationships.htm).  However, formulas are simpler to manage and a simple declaritive solution.
Shazib MahmoodShazib Mahmood
Thank you for the reply James. That helps !
I cannot use formulae because of object reference limits. I believe I can work around issue # 2 using the link you provided. I am still not sure how to go ahead with debugging issue # 1. Logs show me null value being returned for it.
Tejpal KumawatTejpal Kumawat

Hello Shaz

Issue # 1 : Its bring null because of it is difficult to get direct relationship contaxt without querying, so we need to query.
Issue # 2 : There are two owner Queue & User etc so reference is not valid
Issue # 3 : Case trigger check below

Use this code :
trigger UpdateCaseAndOppOwnerManager on Case (before update){
     if (Trigger.isBefore) {
        if (Trigger.isUpdate) {            
            map<Id, User> mapUser = new map<Id, User>([Select Email, Manager.Email from User]);
            map<Id, Opportunity> mapOpportunity = new map<Id, Opportunity>([Select OwnerId, Owner.Email from Opportunity]);
            for (Case c : Trigger.new){
                c.Opportunity_Owner_Email__c = (mapOpportunity.containsKey(c.Opportunity__c) ? mapOpportunity.get(c.Opportunity__c).Owner.Email : null);
                c.Case_Owner_Manager_Email__c = (mapUser.containsKey(c.OwnerId) ? mapUser.get(c.OwnerId).Manager.Email : null);
            }
        }
    }
 }

If this answers your question mark Best Answer it as solution and then hit Like!