You need to sign in to do that
Don't have an account?
Alex Waddell 12
Apex Trigger firing every time
Hello everyone,
I am working on my first trigger and I was able to get the trigger to create a Case from a change in the account object but the only problem is, it is only supposed to create a Case if there isn't already a Case in the status of 'New' or 'On Service' in the Account
When i go to a test account, the trigger fires even when there is a Case that is New. I was wondering if anyone could look at my code and help me find out why this is happening
I am working on my first trigger and I was able to get the trigger to create a Case from a change in the account object but the only problem is, it is only supposed to create a Case if there isn't already a Case in the status of 'New' or 'On Service' in the Account
When i go to a test account, the trigger fires even when there is a Case that is New. I was wondering if anyone could look at my code and help me find out why this is happening
trigger caseCheck on Account (After update) { //Get all account and cases. List<Account> allAccounts = new List<Account>([Select id,Fuzion_Status__c,(select id from cases where status in('New','On Service')) from account where id in :Trigger.new]); List<Case> newCases = new List<Case>(); for(Account myAccount :allAccounts){ Account oldAccount = trigger.oldMap.get(myAccount.id); if(oldAccount.Fuzion_Status__c == 'Initial Phone call' && myAccount.Fuzion_Status__c != 'Initial Phone call'){ if(myAccount.cases !=null){ Case c = new Case(); c.Accountid = myAccount.Id; c.Type = 'ICM'; c.Origin = 'SHIP'; c.Division__c = 'Case Management'; c.Status = 'New'; c.RecordTypeId = '01236000000OJLq'; newCases.add(c); } } } if(!NewCases.isEmpty()){ insert newCases; } }
change
if(myAccount.cases !=null){
to
if(myAccount.cases.isEmpty()){
All Answers
change
if(myAccount.cases !=null){
to
if(myAccount.cases.isEmpty()){