function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Stavros McGillicuddyStavros McGillicuddy 

Delete all Opportunity Product Line Items syntax - Unknown error parsing query

I'm trying to delete all Product Line Items for an Opportunity via a javascript button.
This gives me an Unknown error parsing query error in the developer Console
Select OpportunityID From OpportunityLineItem WHERE OpportunityId = '0063300000iu2IC' delete OpportunityID

 
RAM AnisettiRAM Anisetti
Hi,
try below lines of code
OpportunityLineItem opp=[Select OpportunityID From OpportunityLineItem WHERE OpportunityId = '0063300000iu2IC'];
delete opp;

 
SandhyaSandhya (Salesforce Developers) 
Hi Stavros McGillicuddy,

If you are doing in query editor of developer console, then DML operations cannot be done in the query editor.

You need to this in Debug ---  Open Execute Anonymous code.

Please try below code in Open Execute Anonymous code
List<OpportunityLineItem> olis = new List<OpportunityLineItem>();
olis = [Select Id From OpportunityLineItem Where OpportunityId ='0063300000iu2IC'];
if(olis.size()>0)
delete olis;

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 
Stavros McGillicuddyStavros McGillicuddy
Hello Sandhya,

Your code throws an Invalid left-hand side in assignment message.

This code is on a custom opportunity button and should delete the olis from the current opportunity.

This is how it looks modified.
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}
var record = new sforce.SObject("Opportunity");
record.Id = '{!Opportunity.Id}';

List<OpportunityLineItem> olis = new List<OpportunityLineItem>();
olis = [Select Id From OpportunityLineItem Where OpportunityId ='Record.ID'];
if(olis.size()>0) delete olis;

sforce.connection.update([record]);
window.location.reload();

 
Stavros McGillicuddyStavros McGillicuddy
Hello RAM

Your code throws an unexpected identifier error

Here is how it looks on my button
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}


OpportunityLineItem opp=[Select OpportunityID From OpportunityLineItem WHERE OpportunityId = '0063300000iu2IC'];
delete opp;

sforce.connection.update([record]);
window.location.reload();

 
Stavros McGillicuddyStavros McGillicuddy
Ram... It seems this code will fail if there are more than 1 Products

The code runs fine in the Developers Console but I get an unexpected error when I run it from a button 
 
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}


OpportunityLineItem opp=[Select OpportunityID From OpportunityLineItem WHERE OpportunityId = '0063300000iu2IC'];
delete opp;

sforce.connection.update([record]);
window.location.reload();

 
RAM AnisettiRAM Anisetti
Hi,
Use below code
var olis=sforce.connection.query("Select Id From OpportunityLineItem Where OpportunityId='"+{!Opportunity.Id}+"'");
var records = olis.getArray("records");
var data = Array();
for(var i=0;i< records.length; i++){
data.push (records[i].Id);
}
var deleteIds= sforce.connection.deleteIds(data);