+ Start a Discussion
David81David81 

Trigger that creates case but only once

Hello all, 

 

I'm just a beginner when it comes to Apex, so go easy on me. We have a trigger created by a third party consultant that creates a new case on for an account when a picklist value equals a certain value. The problem with the trigger they created is, every time the account is edited/updated it creates another identical case (assuming that it is still in that stage).

 

Is there an easy way to edit the trigger that will prevent this?

 

 



//creating case for waiting rollover
trigger CreateCaseonAccount on Account (After insert,After update) {
Case objCase = new Case();
list lstCase = new list();
list lstCase1 = new list();

for(Account objAccount1 : trigger.new){
if(objAccount1.Status__c == 'Waiting Rollover'){
objCase.Notes_Status__c = 'Completed Notes';
objCase.Status = 'New';
objCase.Subject = 'Waiting Rollover';
objCase.Origin = 'Web';
objCase.Priority = 'Medium';
objCase.Type = 'Outbound Call';
objCase.Sub_Type__c = 'Welcome Call';
objCase.Reason = 'Account Overview';
objCase.Case_Reason_Type__c = 'Rollover';
if(objAccount1.Client__c != null)
objCase.Client__c = objAccount1.Client__c;
if(objAccount1.id != null)
objCase.AccountId = objAccount1.id;
if(objAccount1.Contact__c != null)
objCase.ContactId = objAccount1.Contact__c;
lstCase.add(objCase);
}
if(lstCase.size()>0){
insert lstCase;
}
//creating case for closed status
if(objAccount1.Status__c == 'Closed'){
objCase.Notes_Status__c = 'Completed Notes';
objCase.Status = 'New';
objCase.Subject = 'Welcome Call';
objCase.Origin = 'Web';
objCase.Priority = 'Medium';
objCase.Type = 'Outbound Call';
objCase.Sub_Type__c = 'Welcome Call';
objCase.Reason = 'Activity Status Request';
objCase.Case_Reason_Type__c = 'Account Open';
if(objAccount1.Client__c != null)
objCase.Client__c = objAccount1.Client__c;
if(objAccount1.id != null)
objCase.AccountId = objAccount1.id;
if(objAccount1.Contact__c != null)
objCase.ContactId = objAccount1.Contact__c;
lstCase1.add(objCase);
}
if(lstCase1.size()>0){
insert lstCase1;
}
}//end of for
//end
}//end of trigger

Message Edited by David81 on 02-01-2010 06:03 AM
Best Answer chosen by Admin (Salesforce Developers) 
David81David81

Ok, new line to account for inserts:

 

 

if(objAccount1.Status__c == 'Waiting Rollover' && (trigger.isInsert || objAccount1.Status__c != Trigger.oldMap.get(objAccount1.id).Status__c )){

 

 

 

All Answers

bob_buzzardbob_buzzard

You can do this by checking the trigger to see if its an update or insert and, if its an update, only creating the case if the previous value of the picklist is different to the new value (and the new value equals the status you are interested in).

 

This does still leave you open to the picklist value flipflopping into and out of the value that triggers the case creation. 

 

Good luck and post back if you need any help with the code.

David81David81

Bob, 

 

That sounds like exactly what I need. I did find one of your older posts that reference trigger.OldMap but can't quite get it to work for me.  I'll be going through developer training soon (very soon hopefully) but for now is there a chance you could  give some assistance with the code?

 

Thanks much. 

bob_buzzardbob_buzzard

 Here's some of it - I've just coded this on the fly, so you may get the odd typo:

 

 

if (trigger.isInsert) { // do as you are now } else if (trigger.isUpdate) { for (Integer recCount=0; recCount<tigger.new.size(); recCount++) { Account oldAcc=trigger.old.get(recCount); Account newAcc=trigger.new.get(recCount); if ( (newAcc.Status__c == 'Waiting Rollover') && (oldAcc.Status__c != newAcc.Status__c) ) { // create new case and add to lstCase } } }

 

 

 

 So the concept is that if the trigger is an update, iterate the old/new accounts in order and pick up those where the status has transitioned into 'Waiting Rollover'.  This will need to be replicated for closed status, and I'd recommend moving some of the case creation into a utility class, but that's best practice rather than necessary.

 

David81David81

Ok, so I just played around before seeing your response and came up with changing

 

 

if(objAccount1.Status__c == 'Waiting Rollover'){

 

 to 

 

if(objAccount1.Status__c == 'Waiting Rollover' && objAccount1.Status__c != Trigger.oldMap.get(objAccount1.id).Status__c ){

 

Replicated of course to the "Closed" status section.

 

This seems to get the job done. Am I missing something?

 

 

Message Edited by David81 on 02-01-2010 07:28 AM
David81David81

Ok, new line to account for inserts:

 

 

if(objAccount1.Status__c == 'Waiting Rollover' && (trigger.isInsert || objAccount1.Status__c != Trigger.oldMap.get(objAccount1.id).Status__c )){

 

 

 

This was selected as the best answer
bob_buzzardbob_buzzard
Yep, that's a fine way of doing it.