You need to sign in to do that
Don't have an account?

Scheduled Apex Class not running
Hi Everyone,
I have recently scheduled an apex class to run via the Schedule Apex interface that will create new records in a custom object called "Oppty_Line_Item_History" when certain criteria is met on the standard object "OpportunityLineItem". The class works via trigger (although it does a weird duplicate thing because of workflows) but it does not seem to run when I schedule it it to run every night. I also can't even figure out WHERE I can see what went wrong.
Here is the class I want to run:
public class OpptyLineItemHISTORY{
public void method1(List<OpportunityLineItem> changes){
//List to hold LineItemHistory
List <Oppty_Line_Item_History__c> insertHIST = new List <Oppty_Line_Item_History__c>();
for (OpportunityLineItem o: changes){
if (o.priorvalues__c=='Yes'&& o.LastModifiedDate==date.today())
{
changes.add(o);
Oppty_Line_Item_History__c IH = new Oppty_Line_Item_History__c();
IH.Action_Date__c=o.lastmodifieddate;
IH.Action__c = 'Product Updated';
IH.Oppty_Line_Item_id__c = o.id;
IH.Opportunity__c = o.OpportunityId;
IH.Product_Name__c=o.Product_Name_in_TEXT__c;
IH.Oppty__c=o.opptytrigger__c;
IH.Line_Item__c=o.Line_Item_Id__c;
IH.Product_Family__c = o.Product_Family__c;
IH.Site__c=o.Site__c;
IH.fixed_Variable__c=o.fixed_Variable__c;
IH.discount__c=o.discount__c;
IH.Modification__c=o.Modification__c;
IH.Modification_Type__c= o.Modification_Type__c;
IH.Quantity__c=o.Quantity;
IH.Sales_Price__c = o.UnitPrice;
IH.Total_Price__c = o.Total__c;
insertHIST.add(IH);
insert IH;
}
//once loop is done, you need to insert new records in SF
//DML operations might cause an error, so you need to catch it with try/catch block.
} //once loop is done, you need to insert new records in SF
// dml operations might cause an error, so you need to catch it with try/catch block.
insert insertHIST;
}
}
Here is my schedulable class:
global class ScheduleLineItemHistory implements Schedulable {
global void execute (SchedulableContext SC) {
lineitemhistoryHELPER HELPER = new lineitemhistoryHELPER();
HELPER.CreateHistoryasNeeded();
}
}
Your utility class name is "OpptyLineItemHISTORY"
but you seemed to call "lineitemhistoryHELPER" from the Schedulable class.
When I change the code below, I get the following error:
Error: Compile Error: Method does not exist or incorrect signature: [OpptyLineItemHISTORY].CreateHistoryasNeeded() at line 6 column 5
global class ScheduleLineItemHistory implements Schedulable {
global void execute (SchedulableContext SC) {
OpptyLineItemHISTORY HELPER = new OpptyLineItemHISTORY();
HELPER.CreateHistoryasNeeded();
}
}
Yikes! I pasted the wrong Apex Class:
This is the correct one!
public class lineitemhistoryHELPER{
public void CreateHistoryasNeeded(){
List<OpportunityLineItem> lineitems = new List <OpportunityLineItem>();
List<Oppty_Line_Item_History__c> historytocreate = new List<Oppty_Line_Item_History__c>();
for(OpportunityLineItem o:lineitems){
DateTime DT= o.LastModifiedDate;
Date LMD = Date.newinstance(DT.Year(),DT.Month(),DT.Day());
if(o.priorvalues__c=='Yes'&& LMD == Date.today()){
Oppty_Line_Item_History__c IH = new Oppty_Line_Item_History__c();
IH.Action_Date__c=o.lastmodifieddate;
IH.Action_Initiated_By__c=o.LastModifiedBy.alias;
IH.Action__c = 'Product Updated';
IH.Oppty_Line_Item_id__c = o.id;
IH.Opportunity__c = o.OpportunityId;
IH.Product_Name__c=o.Product_Name_in_TEXT__c;
IH.Oppty__c=o.opptytrigger__c;
IH.Line_Item__c=o.Line_Item_Id__c;
IH.Product_Family__c = o.Product_Family__c;
IH.Site__c=o.Site__c;
IH.fixed_Variable__c=o.fixed_Variable__c;
IH.discount__c=o.discount__c;
IH.Modification__c=o.Modification__c;
IH.Modification_Type__c= o.Modification_Type__c;
IH.Quantity__c=o.Quantity;
IH.Sales_Price__c = o.UnitPrice;
IH.Total_Price__c = o.Total__c;
historytocreate.add(IH);
}
}
insert historytocreate;
}
}