+ Start a Discussion
Thomas CailletThomas Caillet 

Put Account OwnerId via lookup on Case Trigger

Hello,

I tried to create a case when another is closed. On case, I have an Account lookup field and I want to put the account owner on the new case.
Here is my trigger :

trigger CreatePresentationCallCase on Case (before update) {

if((trigger.new[0].Status=='closed') && (trigger.old[0].Status<>'closed') && (trigger.new[0].Opportunity_Case__c != null) && ((trigger.new[0].Subject=='New hotel : Training') || (trigger.new[0].Subject=='New hotel : Training')))
                {

                case c1=new case(
                Subject='New hotel : Presentation Call To Be Done',
                OwnerId = trigger.new[0].Account.OwnerId,
                Alfred_Scope__c = 'Other',
                Subject__c = 'New Hotel',
                Type = 'Action Request',
                Origin ='Automatic case',  
                Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                AccountId = trigger.new[0].AccountId,
                Description = 'Kick off made on ' + trigger.new[0].Kick_off_date__c + ' by ' + trigger.new[0].Kick_off_person__c + '\n' + 'Training made on ' + trigger.new[0].Training_Date__c + ' by ' + trigger.new[0].Training_Person__c
                );

                insert c1;
                
                }
                
}

I can save it but when I try to close my case I've got this error : 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CreatePresentationCallCase caused an unexpected exception, contact your administrator: CreatePresentationCallCase: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]: Trigger.CreatePresentationCallCase: line 18, column 1

Could you help me please ?
Thanks
Best Answer chosen by Thomas Caillet
Ishwar ShindeIshwar Shinde
Hi, 

In trigger you can't access the fields which are not part of current record. in this case, account.ownerId. AccountId is available on case trigger context as it on case record. Please try below approach - 

Create formula field on Case to fetch the AccountOwnerId -> Account.OwnerId

As you are doing creation of records in trigger, it should be moved to after trigger and should support the bulkification. 
I tried to write below code for you. Hope it will help.
 
trigger CreatePresentationCallCase on Case (after update) {
    List<Case> caseRecordList = new List<Case>();
    for(Case caseRecord : trigger.new) {
        if((caseRecord.Status=='closed') && (trigger.old[0].Status<>'closed') && (caseRecord.Opportunity_Case__c != null) && ((caseRecord.Subject=='New hotel : Training') || (caseRecord.Subject=='New hotel : Training')))
            {

                case c1=new case(
                Subject='New hotel : Presentation Call To Be Done',
                OwnerId = caseRecord.AccountOwnerId,
                Alfred_Scope__c = 'Other',
                Subject__c = 'New Hotel',
                Type = 'Action Request',
                Origin ='Automatic case',  
                Opportunity_Case__c = caseRecord.Opportunity_Case__c,
                AccountId = caseRecord.AccountId,
                Description = 'Kick off made on ' + caseRecord.Kick_off_date__c + ' by ' + caseRecord.Kick_off_person__c + '\n' + 'Training made on ' + caseRecord.Training_Date__c + ' by ' + caseRecord.Training_Person__c
            );

                caseRecordList.add(c1);
            
            }
        }
            
        insert caseRecordList;
}

 

All Answers

Ishwar ShindeIshwar Shinde
Hi, 

In trigger you can't access the fields which are not part of current record. in this case, account.ownerId. AccountId is available on case trigger context as it on case record. Please try below approach - 

Create formula field on Case to fetch the AccountOwnerId -> Account.OwnerId

As you are doing creation of records in trigger, it should be moved to after trigger and should support the bulkification. 
I tried to write below code for you. Hope it will help.
 
trigger CreatePresentationCallCase on Case (after update) {
    List<Case> caseRecordList = new List<Case>();
    for(Case caseRecord : trigger.new) {
        if((caseRecord.Status=='closed') && (trigger.old[0].Status<>'closed') && (caseRecord.Opportunity_Case__c != null) && ((caseRecord.Subject=='New hotel : Training') || (caseRecord.Subject=='New hotel : Training')))
            {

                case c1=new case(
                Subject='New hotel : Presentation Call To Be Done',
                OwnerId = caseRecord.AccountOwnerId,
                Alfred_Scope__c = 'Other',
                Subject__c = 'New Hotel',
                Type = 'Action Request',
                Origin ='Automatic case',  
                Opportunity_Case__c = caseRecord.Opportunity_Case__c,
                AccountId = caseRecord.AccountId,
                Description = 'Kick off made on ' + caseRecord.Kick_off_date__c + ' by ' + caseRecord.Kick_off_person__c + '\n' + 'Training made on ' + caseRecord.Training_Date__c + ' by ' + caseRecord.Training_Person__c
            );

                caseRecordList.add(c1);
            
            }
        }
            
        insert caseRecordList;
}

 
This was selected as the best answer
Thomas CailletThomas Caillet
Thanks very much Shinde :)
Thomas