0

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; 
     }  
    }
}
2
  • 1
    Please precise your requirement. Where the parent appointment is supposed to come from? Jul 20, 2017 at 8:27
  • Reqiurement is whenever Opportuntiy is created automatically Appointment should be created. with specified values. There is a lookup relation Appointment --> Account and Opportuntiy. Account__c should come from Opportunity.Accountid, Parent_Opportunity__c should be the opportunity id . Please ignore Parent_Appointment
    – User 7864
    Jul 20, 2017 at 8:30

1 Answer 1

0

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; 
     }
}
5
  • Due to some issues in other triggers it is not working.So i am including this logic in Apex class and trying to call from different exisitng trigger.It is throwing invalid method name error in trigger. Please find the above new class and New exising trigger
    – User 7864
    Jul 20, 2017 at 9:09
  • You need to pass the Trigger.New records to the method createrecord()
    – Ankita
    Jul 20, 2017 at 9:17
  • This is how i passed trigger.new in trigger AppointmentOnOpp.createrecord(Trigger.New);
    – User 7864
    Jul 20, 2017 at 9:20
  • I got this error now "Method does not exist or incorrect signature: AppointmentOnOpp.createrecord(List<Opportunity>) at line 2 column 2
    – User 7864
    Jul 20, 2017 at 9:21
  • Trigger.new needs to be typecasted to List<Opportunity>
    – Ankita
    Jul 20, 2017 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.