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
sml9099sml9099 

custom Button executing javascript not deleting records

Hey Guys,

 

I am trying to create a custom button executing javascript. This button is same like DELETE button . it will also delete records only when certain checkbox is  NOT checked off. If that checkbox is checked off , it should not delete records rather it should give an alert message. Below is the javascript. But Everytime , I click the button on record , I get this error: Please help me in fixing the code.

 

A problem with the OnClick JavaScript for this button or link was encountered:

Unexpected token '{'

 


Javascript:

 

{!REQUIRESCRIPT('/soap/ajax/28.0/connection.js')} 

var oppty = new sforce.SObject('OpportunityLineItem'); 
oppty.Id = '{!OpportunityLineItem.Id}'; 

If ( oppty.Trafficked__c = 'False'){ 
var resultoppty = sforce.connection.delete([oppty]); 

else 

alert('cannot delete.please retry again'); 
location.reload(); 
}

Deepa.B.AnkaliDeepa.B.Ankali
I am not sure what the error is referring to, but try fixing this syntax error :
If ( oppty.Trafficked__c = 'False'){

replace with

If ( oppty.Trafficked__c == 'False'){

Or just

If ( ! oppty.Trafficked__c){
sml9099sml9099

Hi, 

 

Thanks for responding. I did use that but still the same error.

 

Regards

Deepa.B.AnkaliDeepa.B.Ankali
@sml9099,

There deleteIds and not delete. Replace delete with deleteIds in sforce.connection.delete
sml9099sml9099

Again the same error.

Elie.RodrigueElie.Rodrigue
oppty.Trafficked__c wont work as your are not querying the object. You should use {!OpportunityLineItem.Trafficked__c} instead.
Remove the capital I from the if, thats what causing the unexpected token error.
As DeepaAnika.Ankali said, use deleteIds not delete.

Fixed code :
{!REQUIRESCRIPT('/soap/ajax/28.0/connection.js')}


if (!{!OpportunityLineItem.Trafficked__c}){
var resultoppty = sforce.connection.deleteIds(['{!OpportunityLineItem.Id}']);
location.reload();
}
else
{
alert('cannot delete.please retry again');
location.reload();
}

Dont forget to mark as solved if it fix your issue!
sml9099sml9099
Hey Elie, 

It's again the same error

A problem with the OnClick JavaScript for this button or link was encountered:
Unexpected token '{'