You need to sign in to do that
Don't have an account?
IR_Admin IR_Salesforce
apex class for updating Opportunity Line Item everyday
I need to write a schedulable apex class that will update a custom field in the OpportunityLineItem object everyday.
I have this so far:
global class UpdateOLI Implements Schedulable {
global void execute(SchedulableContext sc) {
updateOLI();
}
public void updateOLI() {
List<OpportunityLineItem> affected = [SELECT Id, Name
FROM OpportunityLineItem
WHERE position_status__c <> Closed];
// either do your logic actually here with a Message.SingleEmailMessage here, or just fire an update
for(OpportunityLineItem thisOLI : affected) {
// Some dummy field to trigger the workflow (gross huh!)
thisOLI.Apex_Schedule__c = thisOLI.Apex_Schedule__c +1;
}
update affected;
}
}
But have received this error:
Apex script unhandled exception by user/organization:
Scheduled job 'Update OLI' threw unhandled exception.
caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: OpportunityLineItem.Apex_Schedule__c
Class.UpdateOLI.updateOLI: line 15, column 1
Class.UpdateOLI.execute: line 4, column 1
I have this so far:
global class UpdateOLI Implements Schedulable {
global void execute(SchedulableContext sc) {
updateOLI();
}
public void updateOLI() {
List<OpportunityLineItem> affected = [SELECT Id, Name
FROM OpportunityLineItem
WHERE position_status__c <> Closed];
// either do your logic actually here with a Message.SingleEmailMessage here, or just fire an update
for(OpportunityLineItem thisOLI : affected) {
// Some dummy field to trigger the workflow (gross huh!)
thisOLI.Apex_Schedule__c = thisOLI.Apex_Schedule__c +1;
}
update affected;
}
}
But have received this error:
Apex script unhandled exception by user/organization:
Scheduled job 'Update OLI' threw unhandled exception.
caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: OpportunityLineItem.Apex_Schedule__c
Class.UpdateOLI.updateOLI: line 15, column 1
Class.UpdateOLI.execute: line 4, column 1
<pre>
public class UpdateOLI implements Schedulable, Database.Batchable<sObject>
{
public void execute( SchedulableContext sc )
{
Database.executeBatch( this );
}
public Database.QueryLocator start( Database.BatchableContext bc )
{
return Database.getQueryLocator
( String.join
( new List<String>
{ 'SELECT Id, Apex_Schedule__c'
, 'FROM OpportunityLineItem'
, 'WHERE Position_Status__c != \'Closed\''
}
, ' '
)
);
}
public void execute( Database.BatchableContext bc, List<sObject> scope )
{
for ( OpportunityLineItem thisOLI : (List<OpportunityLineItem>) scope )
{
// Some dummy field to trigger the workflow (gross huh!)
thisOLI.Apex_Schedule__c = thisOLI.Apex_Schedule__c + 1;
}
update scope;
}
public void finish( Database.BatchableContext bc )
{
// nothing to do here...
}
}
</pre>
All Answers
Thanks for replying. I actually don't want to send an email update, but I actually want to update a field every night so that workflows can trigger on the opportunity line item. Seems like I maybe quite off here. Could you help point me in the right direction?
Thanks!
<pre>
public class UpdateOLI implements Schedulable, Database.Batchable<sObject>
{
public void execute( SchedulableContext sc )
{
Database.executeBatch( this );
}
public Database.QueryLocator start( Database.BatchableContext bc )
{
return Database.getQueryLocator
( String.join
( new List<String>
{ 'SELECT Id, Apex_Schedule__c'
, 'FROM OpportunityLineItem'
, 'WHERE Position_Status__c != \'Closed\''
}
, ' '
)
);
}
public void execute( Database.BatchableContext bc, List<sObject> scope )
{
for ( OpportunityLineItem thisOLI : (List<OpportunityLineItem>) scope )
{
// Some dummy field to trigger the workflow (gross huh!)
thisOLI.Apex_Schedule__c = thisOLI.Apex_Schedule__c + 1;
}
update scope;
}
public void finish( Database.BatchableContext bc )
{
// nothing to do here...
}
}
</pre>
The error can be solved by quering Apex_Schedule__c in your query as below:
Hope this helps.
Thanks.