+ Start a Discussion
Dilip Kulkarni 12Dilip Kulkarni 12 

trigger for lookup relation

Employee object is in lookup relation with account,When we will create Event Record with Condition (Event Status = Closed) and once we click on save ,it should create Employee record automatically.What will be the trigger for it?
Best Answer chosen by Dilip Kulkarni 12
VamsiVamsi
HI Dilip,

Trigger should be written on Event not account. Also you can get this done with process builder as well, If you prefer.
 
trigger AutoCreateEmployee on Event (after insert)
 {
    List<Employee__c> employees = new List<Employee__c>();
    
    for (Event newEmployee: Trigger.New) {
        if (newEmployee.Event_Status = Closed) 
   {
            employees.add(new Employee__c (
                        Name = '1',
                        Employee__c = newPosition.Id,
                        Role__c = 'Managerial'));
        }
    }
    insert employees ;
}

Please mark as best answer if the above helps ..!!!
 

All Answers

Surya Prakash TomarSurya Prakash Tomar

Hi Dilip,

May this will help you!

trigger AutoCreateEmployee on Account(after insert) {
    List<Employee__c> employees = new List<Employee__c>();
    
    for (Employee__c newEmployee: Trigger.New) {
        if (newEmployee.Event_Status = Closed) {
            employees.add(new Employee__c (
                        Name = '1',
                        Employee__c = newPosition.Id,
                        Role__c = 'Managerial'));
        }
    }
    insert employees ;
}
Dilip Kulkarni 12Dilip Kulkarni 12
Thanks Surya,
I have one question the event is in activities object, so should we write trigger on account or on event?
VamsiVamsi
HI Dilip,

Trigger should be written on Event not account. Also you can get this done with process builder as well, If you prefer.
 
trigger AutoCreateEmployee on Event (after insert)
 {
    List<Employee__c> employees = new List<Employee__c>();
    
    for (Event newEmployee: Trigger.New) {
        if (newEmployee.Event_Status = Closed) 
   {
            employees.add(new Employee__c (
                        Name = '1',
                        Employee__c = newPosition.Id,
                        Role__c = 'Managerial'));
        }
    }
    insert employees ;
}

Please mark as best answer if the above helps ..!!!
 
This was selected as the best answer
Dilip Kulkarni 12Dilip Kulkarni 12
Hi Surya,
I am facing error at line if (newEmployee.Event_Status = Closed)  as   Variable does not exist: Closed at line 9 column 40
 
VamsiVamsi
Please us approriate API name for Event status 
Dilip Kulkarni 12Dilip Kulkarni 12
Thanks Vamshi. I will check and revert
Dilip Kulkarni 12Dilip Kulkarni 12
Hi Vamsi.
Your trigger works. Can you please provide me test class for the same.
VamsiVamsi
Hi Dilip,

Please find the below test class 

@IsTest
public class AutoCreateEmployee_Test
{
  @IsTest static void Testmethod1() // This condition will cause the trigger to execute 
{
 Event ev = new Event (Event_Status = 'Closed', specify any required field with API names); // Also make sure Event_Status is a correct API name 
 list<Employee__c> em = [select id from Employee__c where createddate = today()];   // If there is any relation between employee and event them retrieve the employee record based on that relation . For example I am retrieving the employees created today.
system.asserEquals(em.size(),1); 
}    

@IsTest static void Testmethod2() // This condition will not cause the trigger to execute 
{
 Event ev = new Event (Event_Status = 'New', specify any required fields with API names);// Also make sure Event_Status is a correct API name 
 list<Employee__c> em = [select id from Employee__c where createddate = today()];   // If there is any relation between employee and event them retrieve the employee record based on that relation . For example I am retrieving the employees created today.
system.asserEquals(em.size(),0); 
}    
}

Please mark as best answer if the above helps ..!!!
Dilip Kulkarni 12Dilip Kulkarni 12
Hi Vamsi,
Thanks. Can I use upto testmethod1 ?  Also I am getting error 'unexpected syntax: 'mismatched input '(' expecting RSQUARE' at line 7 column 78'  at line list<Employee__c> em = [select id from Employee__c where createddate = today()];

Please help.
VamsiVamsi
Yes you can use but its a good practice to test the negative condition as well 
VamsiVamsi
testmethod2 is used to test negative condition 
 
VamsiVamsi
Sorry I missed to insert the event in above code. Please make use of below one 

 @IsTest static void Testmethod1() // This condition will cause the trigger to execute 
{
 Event ev = new Event (Event_Status = 'Closed', specify any required field with API names); // Also make sure Event_Status is a correct API name 
Insert ev;
// Test the results by retrieving employee
 list<Employee__c> em = [select id from Employee__c where createddate = Today()];   // If there is any relation between employee and event them retrieve the employee record based on that relation . For example I am retrieving the employees created today.
system.assertEquals(em.size(),1); 
}    
 
Dilip Kulkarni 12Dilip Kulkarni 12
OK. I will check.
Dilip Kulkarni 12Dilip Kulkarni 12
Hi Vamsi,
I am getting error unexpected syntax: 'mismatched input '(' expecting RSQUARE'  at line list<Employee__c> em = [select id from Employee__c where createddate = Today()];

Please help.
Dilip Kulkarni 12Dilip Kulkarni 12
Hi Vamsi,
I saved and run the test for class, but it failed in test.What is required here?

Regards.
VamsiVamsi
Hi, What is the error that you are receiving on run
Dilip Kulkarni 12Dilip Kulkarni 12

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [DurationInMinutes]: [DurationInMinutes]
Class.AutoCreateEmployee_Test.Testmethod1: line 8, column 1
VamsiVamsi
Please specify the required fields while creation of new event at line
 Event ev = new Event (Event_Status = 'Closed'); // specify required fields and their values seperated by comma