[SalesForce] Create a New Record with lookup values

I am trying to create a child (Appointment__c)record using after insert trigger.There are lookup values like Parent_Appointment__c(lookup on itself),Account__c (Lookup to Account),Parent_Opportunity__c(Lookup on Opportunity),Parent_Lost_Business__c(lookup on Custom object) which are need to be create.Please help with the code where can i fetch lookup values and create record

trigger AppointmentOnOpp on Opportunity (after insert) 
{
    Account Acc;
   List<Appointment__c> recordsToCreate = new List<Appointment__c>();
       for(Opportunity Opps:Trigger.New)
       {
        if (Opps.RecordTypeId =='012b0000000M8s5AAC') 
        {
            Appointment__c la = new Appointment__c();
            la.Category__c   ='Monthly Update';
            la.Name ='Monthly Update';
            la.Duration__c ='0.5 hour';
            la.Account__c= [select id,Name from Account where Account__c :=Account].id;
           la.Parent_Appointment__c= ??
           la.Start_Date_Time__c = la.CreatedDate__c;   
            recordsToCreate.add(la);
        }  
       }


insert recordsToCreate; 


}

New Trigger

trigger CreateEmailTask on Opportunity (after insert, after Update){
 AppointmentOnOpp.createrecord();


 List<Task> taskInsertList = new List<Task>();
 for(Opportunity opp : trigger.new){
  if(opp.Account.Account_Status__c== 'Active' && opp.IsClosed == FALSE && opp.Days_Overdue__c > 10 && opp.Check_on_Status__c == FALSE && opp.RecordTypeId != '012b00000000pLn')
   taskInsertList.add(new Task(OwnerId = opp.OwnerId, WhatId = opp.Id, Subject =''));

  }

   if(taskInsertList.size()>0)

     insert taskInsertList;

}

New class

public class AppointmentOnOpp {

    public static List<Appointment__c> createrecord()
    {
         List<Appointment__c> recordsToCreate = new List<Appointment__c>();
         for(Opportunity Opps :Trigger.New){
            if (Opps.RecordTypeId =='012b0000000M8s5AAC'){
                Appointment__c la = new Appointment__c();
                la.Category__c   ='Monthly Update';
                la.Name ='Monthly Update';
                la.Duration__c ='0.5 hour';
                la.Account__c= Opps.AccountId;
                la.Parent_Opportunity__c = Opps.Id;
                la.Start_Date_Time__c = la.CreatedDate__c;  
                recordsToCreate.add(la);
            }  
       }
     if(recordsToCreate.size() > 0){
         insert recordsToCreate; 
     }  
    }
}

Best Answer

Updated trigger code:

trigger AppointmentOnOpp on Opportunity (after insert){
    List<Appointment__c> recordsToCreate = new List<Appointment__c>();
        for(Opportunity Opps :Trigger.New){
            if (Opps.RecordTypeId =='012b0000000M8s5AAC'){
                Appointment__c la = new Appointment__c();
                la.Category__c   ='Monthly Update';
                la.Name ='Monthly Update';
                la.Duration__c ='0.5 hour';
                la.Account__c= Opps.AccountId;
                la.Parent_Opportunity__c = Opps.Id;
                la.Start_Date_Time__c = la.CreatedDate__c;  
                recordsToCreate.add(la);
            }  
       }
     if(recordsToCreate.size() > 0){
         insert recordsToCreate; 
     }
}
Related Topic