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
sam_Adminsam_Admin 

Need help in fixing the trigger

Hi Devs,

      I have Trigger and basically it creates new case when a contact is created, below is my code, so when a user creates new contact and if he selects Notification_Type__c (picklist field from contact) == 'New Employee or New Contractor then a case gets created for New hire and after some months if this contact is terminated he goes to the same contact and selects Notification_Type__c == 'Term Employee then a case gets created for termination employee, but since my events are after insert, after update when a user first creates a contact he fills out some details from contact page and when he hits save case gets created but whenever he edits the contact and if he change some contact info again the case gets created since i have after update , i want the case to be created only once , do i have to write a separate trigger for New Employee and Term Employee, can't we include it in one?

 

 

Trigger: trigger ContactTrigger on Contact (after insert, after update) {
 
  ContactTriggerClass ct  = new ContactTriggerClass();
   ct.createCase(Trigger.newMap);  
    
}

 

Class: public class ContactTriggerClass
{
 
    public void createCase(Map<Id,Contact> TriggerNewValues)
   {
   
    List<Case> c = new List<Case>();
    
 for(Contact ct :[select c.Id,c.Name,c.AccountId,c.Notification_Type__c,c.Manager_Name__r.Name,c.EmployeeType__c,c.Location__c,c.Start_Date__c,,c.Special_Instructions_Hire__c,c.Special_Instructions_Term__c,c.Unix_Account__c,c.Termination_Date__c, c.Termination_Time__c from  Contact c where c.Id IN:TriggerNewValues.keyset()])
    {
       if((ct.Notification_Type__c == 'New Employee') || (ct.Notification_Type__c =='New Contractor'))
       {
          c.add(new Case(ContactId = ct.Id,AccountId = ct.AccountId,Type = 'PC SETUP_(New/Reused;Desktops,Laptops)',Status='Assigned',Priority = 'Standard',OwnerId = '00G400000015Yoo',Manager_Name__c = ct.Manager_Name__c,
Subject = 'New PC Setup :'+ct.Name+'- Start Date :'+string.valueof(ct.Start_Date__c),

Description = 'Name :'+ ct.Name+'\r\n\r\nManager :'+ct.Manager_Name__r.Name));

     }

  else if(ct.Notification_Type__c == 'Term Employee')
     {
            c.add(new Case(ContactId = ct.Id,AccountId = ct.AccountId,Type = 'PC SETUP_(New/Reused;Desktops,Laptops)',Status='Assigned',Priority = 'Standard',OwnerId = '00G400000015Yoo',
 Subject = 'PC Termination :'+ct.Name,

Description = 'Name :'+ct.Name+'\r\n\r\nManager :'+ct.Manager_Name__r.Name));
  }
        
     
   }
    insert c;
   }
 
}               
                   

 

 

tharristharris

Can you use the trigger context variables to solve this? They are explained here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

 

If you can check whether a case for the user already exists you could do something like this:

 

if(Trigger.isInsert || (Trigger.isUpdate && caseDoesNotExist)) {

    /* create case */

}

 

Damien_Damien_

Why are you using trigger.newMap instead of trigger.new?  This gives you a list of Contacts you can easily iterate over.

 

Inside of your createCase function...

 

Map<ID, Case> caseMap = new Map<ID, Case>();

for(Case curr: [SELECT ContactID FROM Case WHERE Contact IN: trigger.new)

  caseMap.put(ContactId, Case);

 

List<Case> c = new List<Case>();

for(Contact curr: trigger.new)

{

  if (!caseMap.containsKey(curr.id))

  {

    //do your work in here.

  }

}

insert c;