-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
4Questions
-
5Replies
Apex trigger update problem
Hi,
I am attempting to write a trigger on a custom object but it doesn't seem to being updating - my logic must be flawed in some way. The basic premise is that I have 2 checkbox fields that are updated depending on if a date field ( on the same object) falls within the bounds of either THIS_QUARTER or NEXT_QUARTER. My code looks like this:
Any help would be much appreciated,
Thanks
Dave
I am attempting to write a trigger on a custom object but it doesn't seem to being updating - my logic must be flawed in some way. The basic premise is that I have 2 checkbox fields that are updated depending on if a date field ( on the same object) falls within the bounds of either THIS_QUARTER or NEXT_QUARTER. My code looks like this:
trigger SM_Ident_Quarter on ScheduleMirror__c (before insert, before update) {
for(ScheduleMirror__c SchedMirr : Trigger.New){
// if the schedule date is in This Quarter then update the this quarter flag
for( ScheduleMirror__c s: [SELECT ID, Current_Quarter__c, Next_quarter__c, schedule_date__c from schedulemirror__c where schedule_date__c = THIS_QUARTER]){
if(s.id != null)
s.Current_Quarter__c = TRUE;
}
// if the schedule date is in Next Quarter then update the next quarter flag
for( ScheduleMirror__c sm: [SELECT ID, Current_Quarter__c, Next_quarter__c, schedule_date__c from schedulemirror__c where schedule_date__c = NEXT_QUARTER]){
if(sm.id != null)
sm.Next_quarter__c = TRUE;
}
}
}
I feel this should be quite simple but I think I am missing something fundamental.Any help would be much appreciated,
Thanks
Dave
-
- DaveSt
- October 18, 2016
- Like
- 0
SObject field expression for put method
Hi,
I have been struggling with this piece of apex for the last few days - had considered using batchable context but was struggling to get my head around it. So, I am persevering with a schedulable piece of code - its not very elegant but initial testing on volume data indicates it won't blow the dml or cpu limits ( now that I am not doing a nested loop ) - what I am struggling with is the syntax to assign my values I have loaded in the Sobject to my list. Would someone be able to point me in the right direction?
My code is:
it is where I am building up the list " ScheduleMirror__c mymirror = new ScheduleMirror__c(); " that I have my issue:
My basic issue for example, is that I am trying to map mymirror.currencyIsoCode to the field CurrencyIsoCode from my SObject - I can't work out the syntax for this.
Secondly, once I get that resolved, I need to be able to build mymirror.Name by concatenating the year, month ( from the ScheduleDate ) and the Name from the SObject.
Any help would be appreciated.
Thanks
Dave
I have been struggling with this piece of apex for the last few days - had considered using batchable context but was struggling to get my head around it. So, I am persevering with a schedulable piece of code - its not very elegant but initial testing on volume data indicates it won't blow the dml or cpu limits ( now that I am not doing a nested loop ) - what I am struggling with is the syntax to assign my values I have loaded in the Sobject to my list. Would someone be able to point me in the right direction?
My code is:
Global class RefreshScheduleMirror implements Schedulable {
global void execute(SchedulableContext SC) {
RefreshSchedule();
}
public void RefreshSchedule() {
/// Delete the old mirror records ///
ScheduleMirror__c[] doomedMirrors = [SELECT Name FROM ScheduleMirror__c];
try {
delete doomedMirrors;
} catch (DmlException e) {
// Process exception here
}
/// use generic object to pull combined line item and schedule records
SObject[] allSchedules = [ SELECT CurrencyIsoCode, OpportunityLineItemId, ScheduleDate, Revenue,Quantity, Type, Description, OpportunityLineItem.Name, OpportunityLineItem.OpportunityId,
OpportunityLineItem.Product2Id, OpportunityLineItem.ProductCode
FROM OpportunityLineItemSchedule];
SObject IsoCode = getSObject('CurrencyIsoCode');
/// Create new list to store the records ( based on the structure defined by the custom object Schedule Mirror )
List<ScheduleMirror__c> newmirrors = new List<ScheduleMirror__c>();
/// for (OpportunityLineItem Schedule myschedule : allSchedules) {
for (SObject myschedule : allSchedules) {
// create variable 'mymirror' to hold the record values
ScheduleMirror__c mymirror = new ScheduleMirror__c();
mymirror.CurrencyIsoCode = myschedule.get(OpportunityLineItemSchedule.CurrencyIsoCode);
mymirror.Name = myschedule.get('ScheduleDate.year() + '-' + myschedule.ScheduleDate.month() + '-' + myschedule.Name');
//mymirror.LineItemID__c = string.valueOf(myschedule.get('OpportunityLineItemId'));
//mymirror.Opportunity__c = string.valueOf(myschedule.get('OpportunityId'));
//mymirror.Schedule_Amount__c = string.valueOf(myschedule.get('Revenue'));
//mymirror.Schedule_Date__c = string.valueOf(myschedule.get('ScheduleDate'));
//mymirror.Schedule_Description__c = string.valueOf(myschedule.get('Description'));
//mymirror.Schedule_Quantity__c = string.valueOf(myschedule.get('Quantity'));
//mymirror.Schedule_Type__c = string.valueOf(myschedule.get('Type'));
//mymirror.Schedule_Type__c = string.valueOf(myschedule.get('Product2Id'));
//mymirror.Product_Code__c = string.valueOf(myschedule.get('ProductCode'));
//mymirror.Name = string.valueOf(myschedule.get('Name'));
// Update the list with all the records retrieved in the for loop
newmirrors.add(mymirror);
}
// update database outwith the for loop!!!!
upsert newmirrors;
}
}
it is where I am building up the list " ScheduleMirror__c mymirror = new ScheduleMirror__c(); " that I have my issue:
mymirror.CurrencyIsoCode = myschedule.get(OpportunityLineItemSchedule.CurrencyIsoCode);
mymirror.Name = myschedule.get('ScheduleDate.year() + '-' + myschedule.ScheduleDate.month() + '-' + myschedule.Name');
My basic issue for example, is that I am trying to map mymirror.currencyIsoCode to the field CurrencyIsoCode from my SObject - I can't work out the syntax for this.
Secondly, once I get that resolved, I need to be able to build mymirror.Name by concatenating the year, month ( from the ScheduleDate ) and the Name from the SObject.
Any help would be appreciated.
Thanks
Dave
-
- DaveSt
- October 13, 2016
- Like
- 0
Creating a batch Apex process that uses data across multiple objects
Hi,
I created a piece of schedulable Apex code but encountered the "scheduled apex class getting Too many DML rows: 10001 error" as one of the queries exceeded the row limit. I am relatively new to Force and was aware of the governere limits but this has caught me out but after a bit of reading understand why.
I think my solution is to adapt my code to use the batch interface and then create a new schedulable piece of code to then call this. My problem is re-cutting my code into a batchable form - would anyone be able to suggest how I can re-structure this?
The original code is:
1 - I split the clear out of the custom object into a separate batch;
2 - I then 'tried' to create a batchable piece of Apex code to do the main processing but cannot get my head around how I transfer my query results into my list and then update my custom object.
3 - The idea was then to call both of these from the scheduble code i.e
Thanks
Dave
I created a piece of schedulable Apex code but encountered the "scheduled apex class getting Too many DML rows: 10001 error" as one of the queries exceeded the row limit. I am relatively new to Force and was aware of the governere limits but this has caught me out but after a bit of reading understand why.
I think my solution is to adapt my code to use the batch interface and then create a new schedulable piece of code to then call this. My problem is re-cutting my code into a batchable form - would anyone be able to suggest how I can re-structure this?
The original code is:
Global class RefreshScheduleMirror implements Schedulable {
global void execute(SchedulableContext SC) {
RefreshSchedule();
}
public void RefreshSchedule() {
/// Delete the old mirror records ///
ScheduleMirror__c[] doomedMirrors = [SELECT Name FROM ScheduleMirror__c];
try {
delete doomedMirrors;
} catch (DmlException e) {
// Process exception here
}
/// Create the current schedulve list and query and query to populate it
OpportunityLineItemSchedule[] allSchedules = [ SELECT CurrencyIsoCode, OpportunityLineItemId, ScheduleDate, Revenue, Quantity, Type, Description FROM OpportunityLineItemSchedule];
/// Create list for the product instance records and query to populate it
OpportunityLineItem[] AlllineItems = [ SELECT ID, Name, OpportunityId, Product2Id, ProductCode FROM OpportunityLineItem];
// Create new list to store the records ( based on the structure defined by the custom object Schedule Mirror )
List<ScheduleMirror__c> newmirrors = new List<ScheduleMirror__c>();
/// Loop around each line item schedule record - create variable 'myschedule' for each iteration through array 'allSchedules'
for (OpportunityLineItemSchedule myschedule : allSchedules) {
// inner loop around the Opportunity Product
for (OpportunityLineItem mylineitem : AlllineItems) {
//
if (mylineitem.ID == myschedule.OpportunityLineItemId) {
// create variable 'mymirror' to hold the record values
ScheduleMirror__c mymirror = new ScheduleMirror__c();
mymirror.CurrencyIsoCode = myschedule.CurrencyIsoCode;
mymirror.Name = myschedule.ScheduleDate.year() + '-' + myschedule.ScheduleDate.month() + '-' + mylineitem.Name;
mymirror.LineItemID__c = myschedule.OpportunityLineItemId;
mymirror.Opportunity__c = mylineitem.OpportunityId;
mymirror.Schedule_Amount__c = myschedule.Revenue;
mymirror.Schedule_Date__c = myschedule.ScheduleDate;
mymirror.Schedule_Description__c = myschedule.Description;
mymirror.Schedule_Quantity__c = myschedule.Quantity;
mymirror.Schedule_Type__c = myschedule.Type;
mymirror.ProductID__c = mylineitem.Product2Id;
mymirror.Product_Code__c = mylineitem.ProductCode;
// Update the list with all the records retrieved in the for loop
newmirrors.add(mymirror);
// update database
upsert newmirrors;
}
}
}
}
}
What I have so far....1 - I split the clear out of the custom object into a separate batch;
Global class PurgeOldMirrorsBatch implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query ='SELECT Name FROM ScheduleMirror__c';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<ScheduleMirror__c> scope){
delete scope;
}
global void finish(Database.BatchableContext BC){
}
}
2 - I then 'tried' to create a batchable piece of Apex code to do the main processing but cannot get my head around how I transfer my query results into my list and then update my custom object.
Global class RefreshScheduleMirrorBatch implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
/// Grab the data upfront
{
String query ='SELECT CurrencyIsoCode, OpportunityLineItemId, ScheduleDate, Revenue, Quantity, Type, Description, OpportunityLineItem.ID, OpportunityLineItem.Name, OpportunityLineItem.OpportunityId, OpportunityLineItem.Product2Id, OpportunityLineItem.ProductCode FROM OpportunityLineItemSchedule';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope) {
// Create new list to store the records ( based on the structure defined by the custom object Schedule Mirror )
List<ScheduleMirror__c> newmirrors = new List<ScheduleMirror__c>();
// This is the section I am having issues getting my head around - how to build up the records to populate my list from my query results.
for( ScheduleMirror__c myschedule : scope)
{
if (myschedule.ID == myschedule.OpportunityLineItemId) {
// create variable 'mymirror' to hold the record values
ScheduleMirror__c mymirror = new ScheduleMirror__c();
mymirror.CurrencyIsoCode = myschedule.CurrencyIsoCode;
mymirror.Name = myschedule.ScheduleDate.year() + '-' + myschedule.ScheduleDate.month() + '-' + myschedule.Name;
mymirror.LineItemID__c = myschedule.OpportunityLineItemId;
mymirror.Opportunity__c = myschedule.OpportunityId;
mymirror.Schedule_Amount__c = myschedule.Revenue;
mymirror.Schedule_Date__c = myschedule.ScheduleDate;
mymirror.Schedule_Description__c = myschedule.Description;
mymirror.Schedule_Quantity__c = myschedule.Quantity;
mymirror.Schedule_Type__c = myschedule.Type;
mymirror.ProductID__c = myschedule.Product2Id;
mymirror.Product_Code__c = myschedule.ProductCode;
// Update the list with all the records retrieved in the for loop
newmirrors.add(mymirror);
// update database
upsert newmirrors;
}
}
}
}
3 - The idea was then to call both of these from the scheduble code i.e
Global class RefreshScheduleMirrorExec implements Schedulable
{
global void execute(SchedulableContext SC)
{
PurgeOldMirrorsBatch Purge = new PurgeOldMirrorsBatch();
database.executeBatch(Purge);
RefreshScheduleMirrorBatch MirrorBatch = new RefreshScheduleMirrorBatch();
database.executeBatch(MirrorBatch);
}
}
Am I way off the mark in my approach? Any help would be appreciated.Thanks
Dave
-
- DaveSt
- October 12, 2016
- Like
- 0
OpportunityLineItemSchedule sum(revenue) in SOQL Query
Hoping you can explain what I am experiencing when running a SOQL query on the OpportunityLineItemSchedule to sum the revenue for this quarter. I have the following query:
SELECT sum(Revenue) FROM OpportunityLineItemSchedule where OpportunityLineItem.Opportunity.StageName in ('Closed Won','Contract','Pricing','Trial','Contract Signed','Schedule Variation Open','Schedule Variation–Signed') and ScheduleDate =THIS_QUARTER
but when I use the same criteria to run an report via SF I get a different amount. The value is more in the report.
The report type I am running is an Opportunity Schedule report and is based on Schedule Date range being in Current FQ (which is 01/07/2016 to 30/09/2016 ) and filtered on Stage equals ('Closed Won','Contract','Pricing','Trial','Contract Signed','Schedule Variation Open','Schedule Variation–Signed') and is returning the Opportunity Name, Stage and Schedule Amount. and returns the Schedule Amount ( as there is no option for Revenue )
Could you tell me what the relationship is between the Schedule Amount ( that I can get from the report ) and the 'Revenue' ( from the dev guides https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunitylineitemschedule.htm ) that I am referring to in the query? I did see a post https://success.salesforce.com/answers?id=9063A000000ZmsoQAC that mentioned sharing limits - I could see the relevance if the amount in the query was greater but it is the other way around and I am really struggling to reconcile the figures.
When I expand the query out to this...
SELECT OpportunityLineItem.Opportunity.ID, OpportunityLineItem.Opportunity.name, OpportunityLineItem.Opportunity.CloseDate, ScheduleDate, Revenue, CurrencyIsoCode FROM OpportunityLineItemSchedule where OpportunityLineItem.Opportunity.StageName in ('Closed Won','Contract','Pricing','Trial','Contract Signed','Schedule Variation–Open','Schedule Variation–Signed') and ScheduleDate = THIS_QUARTER
export to excel and manually sum the revenue i get a different figure than my original sum(revenue) query. Have I expanded this incorrectly?
The expanded query returns less rows than the report so this would explain the difference - I just don't know how to correct it
SELECT sum(Revenue) FROM OpportunityLineItemSchedule where OpportunityLineItem.Opportunity.StageName in ('Closed Won','Contract','Pricing','Trial','Contract Signed','Schedule Variation Open','Schedule Variation–Signed') and ScheduleDate =THIS_QUARTER
but when I use the same criteria to run an report via SF I get a different amount. The value is more in the report.
The report type I am running is an Opportunity Schedule report and is based on Schedule Date range being in Current FQ (which is 01/07/2016 to 30/09/2016 ) and filtered on Stage equals ('Closed Won','Contract','Pricing','Trial','Contract Signed','Schedule Variation Open','Schedule Variation–Signed') and is returning the Opportunity Name, Stage and Schedule Amount. and returns the Schedule Amount ( as there is no option for Revenue )
Could you tell me what the relationship is between the Schedule Amount ( that I can get from the report ) and the 'Revenue' ( from the dev guides https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunitylineitemschedule.htm ) that I am referring to in the query? I did see a post https://success.salesforce.com/answers?id=9063A000000ZmsoQAC that mentioned sharing limits - I could see the relevance if the amount in the query was greater but it is the other way around and I am really struggling to reconcile the figures.
When I expand the query out to this...
SELECT OpportunityLineItem.Opportunity.ID, OpportunityLineItem.Opportunity.name, OpportunityLineItem.Opportunity.CloseDate, ScheduleDate, Revenue, CurrencyIsoCode FROM OpportunityLineItemSchedule where OpportunityLineItem.Opportunity.StageName in ('Closed Won','Contract','Pricing','Trial','Contract Signed','Schedule Variation–Open','Schedule Variation–Signed') and ScheduleDate = THIS_QUARTER
export to excel and manually sum the revenue i get a different figure than my original sum(revenue) query. Have I expanded this incorrectly?
The expanded query returns less rows than the report so this would explain the difference - I just don't know how to correct it
-
- DaveSt
- September 07, 2016
- Like
- 0
Apex trigger update problem
Hi,
I am attempting to write a trigger on a custom object but it doesn't seem to being updating - my logic must be flawed in some way. The basic premise is that I have 2 checkbox fields that are updated depending on if a date field ( on the same object) falls within the bounds of either THIS_QUARTER or NEXT_QUARTER. My code looks like this:
Any help would be much appreciated,
Thanks
Dave
I am attempting to write a trigger on a custom object but it doesn't seem to being updating - my logic must be flawed in some way. The basic premise is that I have 2 checkbox fields that are updated depending on if a date field ( on the same object) falls within the bounds of either THIS_QUARTER or NEXT_QUARTER. My code looks like this:
trigger SM_Ident_Quarter on ScheduleMirror__c (before insert, before update) {
for(ScheduleMirror__c SchedMirr : Trigger.New){
// if the schedule date is in This Quarter then update the this quarter flag
for( ScheduleMirror__c s: [SELECT ID, Current_Quarter__c, Next_quarter__c, schedule_date__c from schedulemirror__c where schedule_date__c = THIS_QUARTER]){
if(s.id != null)
s.Current_Quarter__c = TRUE;
}
// if the schedule date is in Next Quarter then update the next quarter flag
for( ScheduleMirror__c sm: [SELECT ID, Current_Quarter__c, Next_quarter__c, schedule_date__c from schedulemirror__c where schedule_date__c = NEXT_QUARTER]){
if(sm.id != null)
sm.Next_quarter__c = TRUE;
}
}
}
I feel this should be quite simple but I think I am missing something fundamental.Any help would be much appreciated,
Thanks
Dave

- DaveSt
- October 18, 2016
- Like
- 0
SObject field expression for put method
Hi,
I have been struggling with this piece of apex for the last few days - had considered using batchable context but was struggling to get my head around it. So, I am persevering with a schedulable piece of code - its not very elegant but initial testing on volume data indicates it won't blow the dml or cpu limits ( now that I am not doing a nested loop ) - what I am struggling with is the syntax to assign my values I have loaded in the Sobject to my list. Would someone be able to point me in the right direction?
My code is:
it is where I am building up the list " ScheduleMirror__c mymirror = new ScheduleMirror__c(); " that I have my issue:
My basic issue for example, is that I am trying to map mymirror.currencyIsoCode to the field CurrencyIsoCode from my SObject - I can't work out the syntax for this.
Secondly, once I get that resolved, I need to be able to build mymirror.Name by concatenating the year, month ( from the ScheduleDate ) and the Name from the SObject.
Any help would be appreciated.
Thanks
Dave
I have been struggling with this piece of apex for the last few days - had considered using batchable context but was struggling to get my head around it. So, I am persevering with a schedulable piece of code - its not very elegant but initial testing on volume data indicates it won't blow the dml or cpu limits ( now that I am not doing a nested loop ) - what I am struggling with is the syntax to assign my values I have loaded in the Sobject to my list. Would someone be able to point me in the right direction?
My code is:
Global class RefreshScheduleMirror implements Schedulable {
global void execute(SchedulableContext SC) {
RefreshSchedule();
}
public void RefreshSchedule() {
/// Delete the old mirror records ///
ScheduleMirror__c[] doomedMirrors = [SELECT Name FROM ScheduleMirror__c];
try {
delete doomedMirrors;
} catch (DmlException e) {
// Process exception here
}
/// use generic object to pull combined line item and schedule records
SObject[] allSchedules = [ SELECT CurrencyIsoCode, OpportunityLineItemId, ScheduleDate, Revenue,Quantity, Type, Description, OpportunityLineItem.Name, OpportunityLineItem.OpportunityId,
OpportunityLineItem.Product2Id, OpportunityLineItem.ProductCode
FROM OpportunityLineItemSchedule];
SObject IsoCode = getSObject('CurrencyIsoCode');
/// Create new list to store the records ( based on the structure defined by the custom object Schedule Mirror )
List<ScheduleMirror__c> newmirrors = new List<ScheduleMirror__c>();
/// for (OpportunityLineItem Schedule myschedule : allSchedules) {
for (SObject myschedule : allSchedules) {
// create variable 'mymirror' to hold the record values
ScheduleMirror__c mymirror = new ScheduleMirror__c();
mymirror.CurrencyIsoCode = myschedule.get(OpportunityLineItemSchedule.CurrencyIsoCode);
mymirror.Name = myschedule.get('ScheduleDate.year() + '-' + myschedule.ScheduleDate.month() + '-' + myschedule.Name');
//mymirror.LineItemID__c = string.valueOf(myschedule.get('OpportunityLineItemId'));
//mymirror.Opportunity__c = string.valueOf(myschedule.get('OpportunityId'));
//mymirror.Schedule_Amount__c = string.valueOf(myschedule.get('Revenue'));
//mymirror.Schedule_Date__c = string.valueOf(myschedule.get('ScheduleDate'));
//mymirror.Schedule_Description__c = string.valueOf(myschedule.get('Description'));
//mymirror.Schedule_Quantity__c = string.valueOf(myschedule.get('Quantity'));
//mymirror.Schedule_Type__c = string.valueOf(myschedule.get('Type'));
//mymirror.Schedule_Type__c = string.valueOf(myschedule.get('Product2Id'));
//mymirror.Product_Code__c = string.valueOf(myschedule.get('ProductCode'));
//mymirror.Name = string.valueOf(myschedule.get('Name'));
// Update the list with all the records retrieved in the for loop
newmirrors.add(mymirror);
}
// update database outwith the for loop!!!!
upsert newmirrors;
}
}
it is where I am building up the list " ScheduleMirror__c mymirror = new ScheduleMirror__c(); " that I have my issue:
mymirror.CurrencyIsoCode = myschedule.get(OpportunityLineItemSchedule.CurrencyIsoCode);
mymirror.Name = myschedule.get('ScheduleDate.year() + '-' + myschedule.ScheduleDate.month() + '-' + myschedule.Name');
My basic issue for example, is that I am trying to map mymirror.currencyIsoCode to the field CurrencyIsoCode from my SObject - I can't work out the syntax for this.
Secondly, once I get that resolved, I need to be able to build mymirror.Name by concatenating the year, month ( from the ScheduleDate ) and the Name from the SObject.
Any help would be appreciated.
Thanks
Dave

- DaveSt
- October 13, 2016
- Like
- 0
Creating a batch Apex process that uses data across multiple objects
Hi,
I created a piece of schedulable Apex code but encountered the "scheduled apex class getting Too many DML rows: 10001 error" as one of the queries exceeded the row limit. I am relatively new to Force and was aware of the governere limits but this has caught me out but after a bit of reading understand why.
I think my solution is to adapt my code to use the batch interface and then create a new schedulable piece of code to then call this. My problem is re-cutting my code into a batchable form - would anyone be able to suggest how I can re-structure this?
The original code is:
1 - I split the clear out of the custom object into a separate batch;
2 - I then 'tried' to create a batchable piece of Apex code to do the main processing but cannot get my head around how I transfer my query results into my list and then update my custom object.
3 - The idea was then to call both of these from the scheduble code i.e
Thanks
Dave
I created a piece of schedulable Apex code but encountered the "scheduled apex class getting Too many DML rows: 10001 error" as one of the queries exceeded the row limit. I am relatively new to Force and was aware of the governere limits but this has caught me out but after a bit of reading understand why.
I think my solution is to adapt my code to use the batch interface and then create a new schedulable piece of code to then call this. My problem is re-cutting my code into a batchable form - would anyone be able to suggest how I can re-structure this?
The original code is:
Global class RefreshScheduleMirror implements Schedulable {
global void execute(SchedulableContext SC) {
RefreshSchedule();
}
public void RefreshSchedule() {
/// Delete the old mirror records ///
ScheduleMirror__c[] doomedMirrors = [SELECT Name FROM ScheduleMirror__c];
try {
delete doomedMirrors;
} catch (DmlException e) {
// Process exception here
}
/// Create the current schedulve list and query and query to populate it
OpportunityLineItemSchedule[] allSchedules = [ SELECT CurrencyIsoCode, OpportunityLineItemId, ScheduleDate, Revenue, Quantity, Type, Description FROM OpportunityLineItemSchedule];
/// Create list for the product instance records and query to populate it
OpportunityLineItem[] AlllineItems = [ SELECT ID, Name, OpportunityId, Product2Id, ProductCode FROM OpportunityLineItem];
// Create new list to store the records ( based on the structure defined by the custom object Schedule Mirror )
List<ScheduleMirror__c> newmirrors = new List<ScheduleMirror__c>();
/// Loop around each line item schedule record - create variable 'myschedule' for each iteration through array 'allSchedules'
for (OpportunityLineItemSchedule myschedule : allSchedules) {
// inner loop around the Opportunity Product
for (OpportunityLineItem mylineitem : AlllineItems) {
//
if (mylineitem.ID == myschedule.OpportunityLineItemId) {
// create variable 'mymirror' to hold the record values
ScheduleMirror__c mymirror = new ScheduleMirror__c();
mymirror.CurrencyIsoCode = myschedule.CurrencyIsoCode;
mymirror.Name = myschedule.ScheduleDate.year() + '-' + myschedule.ScheduleDate.month() + '-' + mylineitem.Name;
mymirror.LineItemID__c = myschedule.OpportunityLineItemId;
mymirror.Opportunity__c = mylineitem.OpportunityId;
mymirror.Schedule_Amount__c = myschedule.Revenue;
mymirror.Schedule_Date__c = myschedule.ScheduleDate;
mymirror.Schedule_Description__c = myschedule.Description;
mymirror.Schedule_Quantity__c = myschedule.Quantity;
mymirror.Schedule_Type__c = myschedule.Type;
mymirror.ProductID__c = mylineitem.Product2Id;
mymirror.Product_Code__c = mylineitem.ProductCode;
// Update the list with all the records retrieved in the for loop
newmirrors.add(mymirror);
// update database
upsert newmirrors;
}
}
}
}
}
What I have so far....1 - I split the clear out of the custom object into a separate batch;
Global class PurgeOldMirrorsBatch implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query ='SELECT Name FROM ScheduleMirror__c';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<ScheduleMirror__c> scope){
delete scope;
}
global void finish(Database.BatchableContext BC){
}
}
2 - I then 'tried' to create a batchable piece of Apex code to do the main processing but cannot get my head around how I transfer my query results into my list and then update my custom object.
Global class RefreshScheduleMirrorBatch implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
/// Grab the data upfront
{
String query ='SELECT CurrencyIsoCode, OpportunityLineItemId, ScheduleDate, Revenue, Quantity, Type, Description, OpportunityLineItem.ID, OpportunityLineItem.Name, OpportunityLineItem.OpportunityId, OpportunityLineItem.Product2Id, OpportunityLineItem.ProductCode FROM OpportunityLineItemSchedule';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope) {
// Create new list to store the records ( based on the structure defined by the custom object Schedule Mirror )
List<ScheduleMirror__c> newmirrors = new List<ScheduleMirror__c>();
// This is the section I am having issues getting my head around - how to build up the records to populate my list from my query results.
for( ScheduleMirror__c myschedule : scope)
{
if (myschedule.ID == myschedule.OpportunityLineItemId) {
// create variable 'mymirror' to hold the record values
ScheduleMirror__c mymirror = new ScheduleMirror__c();
mymirror.CurrencyIsoCode = myschedule.CurrencyIsoCode;
mymirror.Name = myschedule.ScheduleDate.year() + '-' + myschedule.ScheduleDate.month() + '-' + myschedule.Name;
mymirror.LineItemID__c = myschedule.OpportunityLineItemId;
mymirror.Opportunity__c = myschedule.OpportunityId;
mymirror.Schedule_Amount__c = myschedule.Revenue;
mymirror.Schedule_Date__c = myschedule.ScheduleDate;
mymirror.Schedule_Description__c = myschedule.Description;
mymirror.Schedule_Quantity__c = myschedule.Quantity;
mymirror.Schedule_Type__c = myschedule.Type;
mymirror.ProductID__c = myschedule.Product2Id;
mymirror.Product_Code__c = myschedule.ProductCode;
// Update the list with all the records retrieved in the for loop
newmirrors.add(mymirror);
// update database
upsert newmirrors;
}
}
}
}
3 - The idea was then to call both of these from the scheduble code i.e
Global class RefreshScheduleMirrorExec implements Schedulable
{
global void execute(SchedulableContext SC)
{
PurgeOldMirrorsBatch Purge = new PurgeOldMirrorsBatch();
database.executeBatch(Purge);
RefreshScheduleMirrorBatch MirrorBatch = new RefreshScheduleMirrorBatch();
database.executeBatch(MirrorBatch);
}
}
Am I way off the mark in my approach? Any help would be appreciated.Thanks
Dave

- DaveSt
- October 12, 2016
- Like
- 0