You need to sign in to do that
Don't have an account?
Patti Abeyratne
Update / Create Custom object in Contacts
Following code doesn't update the custom object on contact anybody can me help to find error
global class calculateBooking implements Database.Batchable<sObject>{
//global Integer intNumberOfBooking ;
global Database.QueryLocator start(Database.BatchableContext BC){
//String gstrQuery = 'SELECT Contact_ID__c, Most_Recent_Booking__c, Name,Last_6_Months__c from Booking_Details__c';
String gstrQuery = 'SELECT ID,Name,Salesforce_Contact_ID__c from Contact';
return Database.getQueryLocator(gstrQuery);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
List<Booking_Details__c> listBookingInsert = new List<Booking_Details__c>();
List<Booking_Details__c> listBookingUpdate = new List<Booking_Details__c>();
List<Booking_Details__c> listBooking= [SELECT Booking_Salesforce_Contact_ID__c, Most_Recent_Booking__c, Name,Last_6_Months__c from Booking_Details__c];
//List<Contact> listContact = new List<Contact>();
for(SObject objSObject : scope)
{
//Booking_Details__c objBooking = (Booking_Details__c)objSObject;
Contact objContact = (Contact) objSObject;
Integer intNumberOfBooking = [SELECT count()FROM Booking__c WHERE Contact__C = :objContact.ID AND Date_Time__c = LAST_N_DAYS:365];
for(Booking_Details__c bd: listBooking)
{
if (bd.Contact_ID__c==objContact.ID)
{
bd.Last_6_Months__c = intNumberOfBooking+6;
//bd.Booking_Salesforce_Contact_ID__c= objContact.Salesforce_Contact_ID__c;
//objBooking.Name =;
listBookingUpdate.add(bd);
}
}
}
//insert listBookingInsert;
update listBookingUpdate;
//System.debug('Update');
}
global void finish(Database.BatchableContext BC){
// System.debug(intNumberOfBooking);
System.debug('Finish');
/*//Send an email to the User after your batch completes
AsyncApexJob a =[SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob WHERE Id =
:BC.getJobId()];
// Send an email to the Apex job's submitter
// notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Record Clean Up Status: ' + a.Status);
mail.setPlainTextBody
('The batch Apex job processed ' + a.TotalJobItems +
' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
*/}
}
global class calculateBooking implements Database.Batchable<sObject>{
//global Integer intNumberOfBooking ;
global Database.QueryLocator start(Database.BatchableContext BC){
//String gstrQuery = 'SELECT Contact_ID__c, Most_Recent_Booking__c, Name,Last_6_Months__c from Booking_Details__c';
String gstrQuery = 'SELECT ID,Name,Salesforce_Contact_ID__c from Contact';
return Database.getQueryLocator(gstrQuery);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
List<Booking_Details__c> listBookingInsert = new List<Booking_Details__c>();
List<Booking_Details__c> listBookingUpdate = new List<Booking_Details__c>();
List<Booking_Details__c> listBooking= [SELECT Booking_Salesforce_Contact_ID__c, Most_Recent_Booking__c, Name,Last_6_Months__c from Booking_Details__c];
//List<Contact> listContact = new List<Contact>();
for(SObject objSObject : scope)
{
//Booking_Details__c objBooking = (Booking_Details__c)objSObject;
Contact objContact = (Contact) objSObject;
Integer intNumberOfBooking = [SELECT count()FROM Booking__c WHERE Contact__C = :objContact.ID AND Date_Time__c = LAST_N_DAYS:365];
for(Booking_Details__c bd: listBooking)
{
if (bd.Contact_ID__c==objContact.ID)
{
bd.Last_6_Months__c = intNumberOfBooking+6;
//bd.Booking_Salesforce_Contact_ID__c= objContact.Salesforce_Contact_ID__c;
//objBooking.Name =;
listBookingUpdate.add(bd);
}
}
}
//insert listBookingInsert;
update listBookingUpdate;
//System.debug('Update');
}
global void finish(Database.BatchableContext BC){
// System.debug(intNumberOfBooking);
System.debug('Finish');
/*//Send an email to the User after your batch completes
AsyncApexJob a =[SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob WHERE Id =
:BC.getJobId()];
// Send an email to the Apex job's submitter
// notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Record Clean Up Status: ' + a.Status);
mail.setPlainTextBody
('The batch Apex job processed ' + a.TotalJobItems +
' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
*/}
}
Instead of using generic sobject, try using contact specifically.
May this help.