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
MSVRadMSVRad 

Auto Populate Account Name/Account Id on new Case record.

I understand that when I create a new Case using the New button on the Case related list on the Account page that the Account Name should auto populate in the Account Name lookup field. I have written a trigger that creates new cases when a picklist value changes from one value to another. I am able to populate other Case fields using this trigger but am not able to get the Account Name to populate. Here is the trigger:

trigger AutoCreateIncident on Account (after update) { List<Case> c = new List<Case>(); List<Id> pracIds = new List<Id>(); for (Account accNew : Trigger.new) { pracIds.add(accNew.Id); //A set of Practice Accounts } Map<Id, Account> pAccounts = new Map<Id, Account>([SELECT id, Name FROM Account WHERE id IN :pracIds]); for (Integer i = 0; i < Trigger.new.size(); i++) { Account newCase = Trigger.new[i]; Account OldCase = Trigger.old[i]; if(newCase.Operational_Status__c == 'Operational' && oldCase.Operational_Status__c == 'IT GO'){ if(newCase.Account_Services_Rep__c == 'Ade Adeyemi' || oldCase.Account_Services_Rep__c == 'Ade Adeyemi'){ for(Integer j = 0; j < 24; j++){ c.add(new Case(Type = 'Health Check', Reason__c = 'Health Check', Account = pAccounts.get(newCase.Id), OwnerId = '005700000016ePl', Priority = 'High', Due_date__c = System.today() + (j*30) + 1)); } } else if(newCase.Account_Services_Rep__c == 'Chris Nelson' || oldCase.Account_Services_Rep__c == 'Chris Nelson'){ for(Integer k = 0; k < 24; k++){ c.add(new Case(Type = 'Health Check', Reason__c = 'Health Check', Account = pAccounts.get(newCase.Id), OwnerId = '005700000016eQ4', Priority = 'High', Due_date__c = System.today() + (k*30) + 1)); } } } } insert c; }

Any suggestions as to why the account name field (Account = pAccounts.get(newCase.Id)) is not populating on the new Case?

Best Answer chosen by Admin (Salesforce Developers) 
gm_sfdc_powerdegm_sfdc_powerde
Try AccountId = newCase.Id instead

All Answers

gm_sfdc_powerdegm_sfdc_powerde
Try AccountId = newCase.Id instead
This was selected as the best answer
MSVRadMSVRad
Thank you! This worked out perfectly and I knew I was overthinkiing it.