title: Salesforce APEX Auto Create Contact from Case
body: <p>So my end goal for this project is to create a contact and a case for each customer that registers a product on my companies warranty registration page. </p>
  
  <p>I have already got a python script that connects to a MySQL database and saves the needed info into an .xlsx file and then another python script currently sending that same data from the .xlsx file into salesforce and it works for a Case OR a Contact but not both. Apex trigger only works if I actually do a web-to-case or email-to-case but for some reason does not work with python.</p>
  
  <p>This will be something that will be triggered roughly every 6 hours indefinitely. I already got it setup to where it does not pull the same data twice from the database so it only grabs new contacts from the MySQL database by writing a '1' to a new column in each table and does a check if '1' exists and if it does then skip it. Everything is working EXCEPT for creating a Contact AND a Case. Contact specifically because it will have all the address information we need for when a customer calls in for troubleshooting of their product, we want a case also created because that is where the product and serial numbers are taken down and allows a trigger to send the customer an email once the case has been created.</p>
  
  <pre><code>public class CaseAutocreateContactTest {
  
  public static testMethod void testBulkContactsGetCreated() {
      List&lt;Case&gt; newCases = new List&lt;Case&gt;();
      for (Integer i = 0; i&lt;100; i++) {
          Case c = new Case(SuppliedEmail='jdoe_test_test@doe.com' + i,
                              SuppliedName='John Doe' + i,
                              Subject='Feedback - Something' + i);
          newCases.add(c);
      }
      insert newCases;
  
      System.debug('here');
      List&lt;Id&gt; newCaseIds = new List&lt;Id&gt;();
      for (Case caseObj:newCases) {
          newCaseIds.add(caseObj.Id);    
      }
  
      List&lt;Case&gt; updatedCases = [Select ContactId From Case Where Id in :newCaseIds];
  
      for (Case caseObj:updatedCases) {
          System.debug(caseObj.Id + ' ' + caseObj.ContactId);
          System.assert(caseObj.ContactId!=null,'There should be no null contacts');
      }
  }
  
  public static testMethod void testContactGetsCreated() {
      Case c = new Case(SuppliedEmail='jdoe_test_test@doe.com',
                          SuppliedName='John Doe',
                          Subject='Feedback - Something');
      insert c;
  
      List&lt;Contact&gt; johnDoes = [select Id from Contact where Email='jdoe_test_test@doe.com'];
  
      //there should be only 1 -- the trigger should not have created another
      System.assert(johnDoes.size()==1, 'There should be one John Doe!');
  
      Case caseObj = [select ContactId from Case where Id=:c.Id];
      System.assert(caseObj.ContactId!=null,'There should be no null contact on the case');
  }
  
  public static testMethod void testNoDupesAreCreated() {
      Contact cnt1 = new Contact(FirstName = 'John',
                              LastName = 'Doe',
                              Email='jdoe_test_test@doe.com');
  
      insert cnt1;
  
      Case case1 = new Case(SuppliedEmail='jdoe_test_test@doe.com',
                          SuppliedName='John Doe',
                          Subject='Feedback - Something');
  
      insert case1;
  
      List&lt;Contact&gt; johnDoes = [select Id from Contact where Email='jdoe_test_test@doe.com'];
  
      //there should be only 1 -- the trigger should not have created another
      System.assert(johnDoes.size()==1, 'There should be only one John Doe!');
  }
  
  public static testMethod void testEmailNameDoesntGetCreated() {
      Case c = new Case(SuppliedEmail='testEmailNameDoesntGetCreated@doe.com',
                          SuppliedName='testEmailNameDoesntGetCreated@doe.com',
                          Subject='Feedback - Something');
      insert c;
  
      List&lt;Contact&gt; johnDoes = [select Id from Contact where Email='testEmailNameDoesntGetCreated@doe.com'];
  
      //there should be only 1 -- the trigger should not have created another
      System.assert(johnDoes.size()==0, 'There should be no John Does!');
  }
  }
  </code></pre>