Scenario 1 - Create Normal Appointment (Type of Appointment:- “Appointment”):-
Note:- Here is the code for creating a “normal” appointment. It’s working fine when we fetch the following data from the org: service provider, customers, Location, Category, and Appointment Type. Currently, we have fetched the data from the org using queries.
Sample Code:-
// Fetching the service provider
Contact sp = [select id, email, name from contact where Appointed__Type_of_Record__c = 'Service_Provider' And email = 'serviceprovider@example.com' limit 1];
//creating the customer list
List<Contact> conList = new List<Contact>();
public void createCustomers() {
Contact con = new Contact();
con.FirstName = 'test';
con.LastName = 'Customer';
con.email = 'test@gmail.com';
conList.add(con);
upsert conList;
}
createCustomers();
//List<Contact> conList = [Select id, name from Contact where Appointed__Type_of_Record__c = 'Customer' limit 2];
// Fetching the required data for creating the normal appointment
Appointed__AppointmentType__c apptTypeObj = [Select id from Appointed__AppointmentType__c where name = 'Open Appointment SM' limit 1];
Appointed__Location__c locationObj = [Select id from Appointed__Location__c where name = 'Pune' limit 1];
Appointed__Category__c categoryObj = [Select id from Appointed__Category__c where name = 'School management' limit 1];
public void createAppointment(DateTime StartDateTime,DateTime EndDateTime, Contact sp, String subject, String apptType, List<Contact> conList, Appointed__Location__c locObj, Appointed__Category__c catObj, Appointed__AppointmentType__c apptType2Obj){
Appointed__Appointment_Detail__c appDetailsObj = new Appointed__Appointment_Detail__c();
appDetailsObj.Appointed__StartDateTime__c = StartDateTime;
appDetailsObj.Appointed__EndDateTime__c = EndDateTime;
appDetailsObj.Appointed__AD_Appointment_Type__c = apptType2Obj.id;
appDetailsObj.Appointed__Location_Lookup__c = locObj.id;
appDetailsObj.Appointed__Category__c = catObj.id;
appDetailsObj.Appointed__No_of_Participants__c = 1;
appDetailsObj.Appointed__Primary_Service_Provider_Id__c = sp.id;
appDetailsObj.Appointed__Customers_Name__c = sp.name;
appDetailsObj.Appointed__Customer_Contact__c = sp.id;
appDetailsObj.Appointed__WhoId__c = sp.id;
appDetailsObj.Appointed__Event_Type__c = apptType;
appDetailsObj.Appointed__ApptSubject__c = subject;
insert appDetailsObj;
List<Appointed__Appointment_Stakeholder__c> appSHList = new List<Appointed__Appointment_Stakeholder__c>();
// for the service provider
Appointed__Appointment_Stakeholder__c appSHObj = new Appointed__Appointment_Stakeholder__c();
appSHObj.Appointed__IsParent__c = true;
appSHObj.Appointed__Parent_Appointment__c = appDetailsObj.id;
appSHObj.Appointed__Contact_Relation__c = sp.id;
appSHObj.Appointed__RelationId__c = sp.id;
appSHList.add(appSHObj);
if(conList.size() > 0) {
for(contact con : conList) {
Appointed__Appointment_Stakeholder__c appSHObj2 = new Appointed__Appointment_Stakeholder__c();
appSHObj2.Appointed__IsParent__c = true;
appSHObj2.Appointed__IsInvitee__c = true;
appSHObj2.Appointed__Parent_Appointment__c = appDetailsObj.id;
appSHObj2.Appointed__Contact_Relation__c = con.id;
appSHObj2.Appointed__RelationId__c = con.id;
appSHList.add(appSHObj2);
}
}
insert appSHList;
System.debug('>>>>'+appDetailsObj);
}
//Calling the method
createAppointment(DateTime.Valueof('2020-12-26 16:30:00'),DateTime.Valueof('2020-12-26 17:00:00'), sp, 'Testing Appointment',' Appointment', conList, locationObj, categoryObj, apptTypeObj);
After that, a new Appointment will be created in the Appointiv calendar.
Scenario 2. Create Unavailability for a Service Provider:-
Here is the “unavailability” appointment type event code:-
public void createUnavailableAppointment(DateTime StartDateTime,DateTime EndDateTime, Contact objContact, String subject, String apptType){
Appointment_Detail__c appDetailsObj = new Appointment_Detail__c();
appDetailsObj.StartDateTime__c = StartDateTime;
appDetailsObj.EndDateTime__c = EndDateTime;
appDetailsObj.Primary_Service_Provider_Id__c = objContact.id;
appDetailsObj.Customers_Name__c = objContact.name;
appDetailsObj.Customer_Contact__c = objContact.id;
appDetailsObj.WhoId__c = objContact.id;
appDetailsObj.Event_Type__c = apptType;
appDetailsObj.ApptSubject__c = subject;
appDetailsObj.Is_External__c = true;
insert appDetailsObj;
Appointment_Stakeholder__c appSHObj = new Appointment_Stakeholder__c();
appSHObj.IsParent__c = true;
appSHObj.Parent_Appointment__c = appDetailsObj.id;
appSHObj.Contact_Relation__c = objContact.id;
appSHObj.RelationId__c = objContact.id;
insert appSHObj;
System.debug('>>>>'+appDetailsObj);
}
// Picking at least one sp contact for creating appt.
Contact con = [select id, email, name from contact where Appointed__Type_of_Record__c = 'Service_Provider' And Email = 'serviceprovider@example.com' limit 1];
// Calling the method
createUnavailableAppointment(DateTime.Valueof('2020-11-26 14:00:00'),DateTime.Valueof('2020-11-26 14:30:00'), con, 'Testing Appointment','Unavailable');
Comments
0 comments
Please sign in to leave a comment.