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
Sandra WicketSandra Wicket 

Trigger - create new record

Hey Guys,

i need to complete this trigger. It works like it is.. the problem is, everytime when i update the account, the trigger creates a new Case  ( i am not a Coder but i understand why ^^) The Trigger is simple and i hope you can see what i want. I want the trigger to stop if the last (newest) case is in progress (status = in progress). So the trigger should only fire when there is no Case in progress. How can i fix this ? I want to learn apex and i understand why the trigger is still running but i dont know how to fix it. Thanks for your help guys :)



User-added image


 
Best Answer chosen by Sandra Wicket
Rahul Kumar 151Rahul Kumar 151
trigger createCaseafterAccount on Account (ater update){
  List<case> caToInsert = new List<case>();
  for(Account acc : Trigger.new){
  if(acc.Rating_Status__C != 'time for Rating'){
  integer num = [select count() from case where AccountId = :acc.id and Status = 'in progress'];
  if(num==0)
  {
     Case cas = new Case(Account=acc.id);
     cas.subject = 'Rating time';
     cas.origin =  'Rating'; 
     cas.Status = 'in progress';
     cas.betreut_von_c='Donalod Duck';

     caToInsert.add(cas);
     
  }
  
}
}
    try{
      insert caToInsert; 

}
    catch(system.Dmlexception e){
    system.debug(e);

}
 
}



Insert this code it help u ...

All Answers

Rahul Kumar 151Rahul Kumar 151
trigger createCaseafterAccount on Account (ater update){
  List<case> caToInsert = new List<case>();
  for(Account acc : Trigger.new){
  if(acc.Rating_Status__C != 'time for Rating'){
  integer num = [select count() from case where AccountId = :acc.id and Status = 'in progress'];
  if(num==0)
  {
     Case cas = new Case(Account=acc.id);
     cas.subject = 'Rating time';
     cas.origin =  'Rating'; 
     cas.Status = 'in progress';
     cas.betreut_von_c='Donalod Duck';

     caToInsert.add(cas);
     
  }
  
}
}
    try{
      insert caToInsert; 

}
    catch(system.Dmlexception e){
    system.debug(e);

}
 
}



Insert this code it help u ...
This was selected as the best answer
Sandra WicketSandra Wicket
@Rahul
thanks a lot !  Can i ask you sth ? I dont understand this line

where AccountId = :acc.id and Status = 'in progress'

why " =" + " : " 
i dont understand the spelling ^^

Best Wishes

Jorma
Rahul Kumar 151Rahul Kumar 151
":"  is using for variable in soql query...
Sandra WicketSandra Wicket
yeah allright..