Wednesday, 19 February 2014

Trigger to create a new record

Here 'Interviewer' is child object and 'Poisition' is parent object.
When ever a position record is created then this trigger automatically 
creates record in Interviewer. 
 
trigger AutoCreateInterviewer on Position__c (after insert) {
    List<Interviewer__c> interviewers = new List<Interviewer__c>();

    //For each position processed by the trigger, add a new  
    //interviewer record for the specified hiring manager.  
    //Note that Trigger.New is a list of all the new positions  
    //that are being created.  
    
    for (Position__c newPosition: Trigger.New) {
        if (newPosition.Hiring_Manager__c != null) {
            interviewers.add(new Interviewer__c(
                        Name = '1',
                        Position__c = newPosition.Id,
                        Employee__c = newPosition.Hiring_Manager__c,
                        Role__c = 'Managerial'));
        }
    }
    insert interviewers;
}

No comments:

Post a Comment