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
Vadzim DzenisenkaVadzim Dzenisenka 

How create case when you create contact?

This code doesnt work.



trigger CreateCase on Contact (before insert) {
 
    List<Case> listCase = new List<Case>();
     for(Contact a : Trigger.New) {  
          listCase.add(new Case(
          AccountID=a.AccountID,        
          ContactID = a.Name,
          Priority ='High',
          Origin = 'Phone',
          Status = 'Working')); 
          }
       
     if(listCase.size() > 0)
  {
        insert listCase;
    }
}
Best Answer chosen by Vadzim Dzenisenka
Michael MMichael M
Hi Vadzim, you can try this. If you need to reference the Contact ID then you will need to use "after insert" as the ID is not created until after the Contact is inserted.

trigger CreateCase on Contact (after insert ) {
 
    List<Case> listCase = new List<Case>();
     for(Contact a : Trigger.New) {  
          Case newCase = new Case();
          newCase.AccountID=a.AccountID;        
          newCase.ContactID = a.id;
          newCase.Priority ='High';
          newCase.Origin = 'Phone';
         newCase.Status = 'Working'; 
listCase.add(newCase);
          }
       
     if(listCase.size() > 0)
  {
        insert listCase;
    }
}

All Answers

Michael MMichael M
Hi Vadzim, you can try this. If you need to reference the Contact ID then you will need to use "after insert" as the ID is not created until after the Contact is inserted.

trigger CreateCase on Contact (after insert ) {
 
    List<Case> listCase = new List<Case>();
     for(Contact a : Trigger.New) {  
          Case newCase = new Case();
          newCase.AccountID=a.AccountID;        
          newCase.ContactID = a.id;
          newCase.Priority ='High';
          newCase.Origin = 'Phone';
         newCase.Status = 'Working'; 
listCase.add(newCase);
          }
       
     if(listCase.size() > 0)
  {
        insert listCase;
    }
}
This was selected as the best answer
Vanisha_GuptaVanisha_Gupta
Hi,

Can you try changing it to After insert:
trigger CreateCase on Contact (after insert) {
 
    List<Case> listCase = new List<Case>();
     for(Contact a : Trigger.New) {  
          listCase.add(new Case(
          AccountID=a.AccountID,        
          ContactID = a.Name,
          Priority ='High',
          Origin = 'Phone',
          Status = 'Working')); 
          }
       
     if(listCase.size() > 0)
  {
        insert listCase;
    }
}
Thanks!
 
Vadzim DzenisenkaVadzim Dzenisenka
Thanks. It's works!