+ Start a Discussion
Gdickens3Gdickens3 

Display Account Owner on Case

I have modified a code for inserting the account owner on an opportunity record to show the same on a case object, but getting error: Compile Error: Incompatible element type Schema.SObjectField for collection of Id at line 4 column 36

 

I am new to apex and have only successfully modified current triggers when fields change, any help is appreciated.

 

trigger trgAccOwner on Case (before insert,before update) {
    Set<Id> acctIDs = new Set<Id>();
    for(Case tl:Trigger.new) {
        if(case.AccountId != null) acctIDs.add(case.AccountId);
    }
    
    Map<Id, Account> acctMap = new Map<Id, Account>([select Owner.Name from Account where Id in :acctIDs]);

for(Case tl: Trigger.new) {
        if(case.AccountId != null) case.Account_Owner__c = acctMap.get(Case.AccountId).Owner.Name;
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

I've made the appropriate changes on your trigger. You can see the difference in what is marked in red. On your loop, you are calling each record within your Trigger.new collection as 'tl'. Due to this, you must refer to your record instance as 'tl' while inside your loop.

 

trigger trgAccOwner on Case (before insert,before update) {
    Set<Id> acctIDs = new Set<Id>();
    for(Case tl :Trigger.new) {
        if(tl.AccountId != null) acctIDs.add(tl.AccountId);
    }
    
    Map<Id, Account> acctMap = new Map<Id, Account>([select Owner.Name from Account where Id in :acctIDs]);

for(Case tl: Trigger.new) {
        if(tl.AccountId != null) tl.Account_Owner__c = acctMap.get(tl.AccountId).Owner.Name;
    }
}

 

All Answers

Alex.AcostaAlex.Acosta

I've made the appropriate changes on your trigger. You can see the difference in what is marked in red. On your loop, you are calling each record within your Trigger.new collection as 'tl'. Due to this, you must refer to your record instance as 'tl' while inside your loop.

 

trigger trgAccOwner on Case (before insert,before update) {
    Set<Id> acctIDs = new Set<Id>();
    for(Case tl :Trigger.new) {
        if(tl.AccountId != null) acctIDs.add(tl.AccountId);
    }
    
    Map<Id, Account> acctMap = new Map<Id, Account>([select Owner.Name from Account where Id in :acctIDs]);

for(Case tl: Trigger.new) {
        if(tl.AccountId != null) tl.Account_Owner__c = acctMap.get(tl.AccountId).Owner.Name;
    }
}

 

This was selected as the best answer
Gdickens3Gdickens3

Thank you!