+ Start a Discussion
b.gonzalezb.gonzalez 

OnClick JavaSCript for this button encountered. Unexpected toekn ILLEGAL

A problem with the OnClick JavaSCript for this button or link was encountered. Unexpected toekn ILLEGAL. Not sure what is "illegal". 

PLEASE HELP!
 
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/19.0/apex.js")} 

if('{!Opportunity.Opportunity_Status__c}' == 'Denied') { 
alert('This opportunity cannot be approved because it has been denied.'); 
} else if('{!Opportunity.Opportunity_Status__c}' != 'Approved') { 
alert('This opportunity must be approved in order to complete this action.'); 
}else if('{!Opportunity.Amount}' != '0.00') { 
alert('This opportunity cannot be updated because no products are listed'); 
}else if('var strQuantity = sforce.connection.query("select Id, Quantity from opportunityLineItem where OpportunityId = '{!Opportunity.Id}'")'); 
}else if('var records = strQuantity.getArray("records")'); 
}else if('var quantity; 
for (var i=0; i< records.length; i++) { 
var record = records[i]; 
if (record.Quantity <50) { 
quantity = record.Quantity'); 
} 
} 
}else if('switch(true){ 
case quantity<50:'){ 
alert('Quantity on products cannot be less than 50.')); 
}else { 
sforce.apex.execute("CMR_DesignWebService", "UpdateToPMIDesign",{OpportunityId:'{!Opportunity.Id}'}); 
alert('Opportunity Approved in PMI system.'); 
window.location = window.location; 
}

Beth
Best Answer chosen by b.gonzalez
Yury BondarauYury Bondarau
Hello Beth,
I think I understand how it should work. Please try the  updated snippet
In case this code returns an error could you please attach a screenshot?
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/19.0/apex.js")} 

if('{!Opportunity.Opportunity_Status__c}' == 'Denied') { 
	alert('This opportunity cannot be approved because it has been denied.'); 
} else if('{!Opportunity.Opportunity_Status__c}' != 'Approved') { 
	alert('This opportunity must be approved in order to complete this action.'); 
}else if('{!Opportunity.Amount}' == 0) { 
	alert('This opportunity cannot be updated because no products are listed'); 
} else {
	var strQuantity = sforce.connection.query("select Id, Quantity from opportunityLineItem where OpportunityId = '{!Opportunity.Id}'"); 
	var records = strQuantity.getArray("records"); 
	var quantity; 
	for (var i=0; i< records.length; i++) { 
		var record = records[i]; 
		if (record.Quantity < 50) { 
			quantity = record.Quantity; 
			break;
		} 
	} 

	if( quantity<50 ) {
		alert('Quantity on products cannot be less than 50.'); 
	} else { 
		sforce.apex.execute("CMR_DesignWebService", "UpdateToPMIDesign",{OpportunityId:'{!Opportunity.Id}'}); 
		alert('Opportunity Approved in PMI system.'); 
		window.location = window.location; 
	}
}

 

All Answers

Yury BondarauYury Bondarau
Could you please explain in couple oif words what this javascript should do?
Actually, this snippet has a lot of syntax mistakes

line 10: trying to perform SOQL select in IF condition
line 11: trying to perform SOQL select in IF condition
line 12: trying to define a variable in IF condition
line 19: specifying SWITCH instead of IF condition

it should loks like
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/19.0/apex.js")} 

if('{!Opportunity.Opportunity_Status__c}' == 'Denied') { 
	alert('This opportunity cannot be approved because it has been denied.'); 
} else if('{!Opportunity.Opportunity_Status__c}' != 'Approved') { 
	alert('This opportunity must be approved in order to complete this action.'); 
}else if('{!Opportunity.Amount}' != '0.00') { 
	alert('This opportunity cannot be updated because no products are listed'); 
} else {
	var strQuantity = sforce.connection.query("select Id, Quantity from opportunityLineItem where OpportunityId = '{!Opportunity.Id}'"); 
	var records = strQuantity.getArray("records"); 
	var quantity; 
	for (var i=0; i< records.length; i++) { 
		var record = records[i]; 
		if (record.Quantity <50) { 
			quantity = record.Quantity; 
		} 
	} 

	switch(true){ 
		case quantity<50:
			alert('Quantity on products cannot be less than 50.'); 
	}
}else { 
	sforce.apex.execute("CMR_DesignWebService", "UpdateToPMIDesign",{OpportunityId:'{!Opportunity.Id}'}); 
	alert('Opportunity Approved in PMI system.'); 
	window.location = window.location; 
}

 
b.gonzalezb.gonzalez
Hi Yury,

I modified the button to include a validation. This validation will not allow the user to "Update to PMI Design" if any of the products have a quantity less than 50. I took what you provided above and update my button in sandbox but I am still recieving the same error. 


Beth
Yury BondarauYury Bondarau
Hello Beth,
I think I understand how it should work. Please try the  updated snippet
In case this code returns an error could you please attach a screenshot?
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/19.0/apex.js")} 

if('{!Opportunity.Opportunity_Status__c}' == 'Denied') { 
	alert('This opportunity cannot be approved because it has been denied.'); 
} else if('{!Opportunity.Opportunity_Status__c}' != 'Approved') { 
	alert('This opportunity must be approved in order to complete this action.'); 
}else if('{!Opportunity.Amount}' == 0) { 
	alert('This opportunity cannot be updated because no products are listed'); 
} else {
	var strQuantity = sforce.connection.query("select Id, Quantity from opportunityLineItem where OpportunityId = '{!Opportunity.Id}'"); 
	var records = strQuantity.getArray("records"); 
	var quantity; 
	for (var i=0; i< records.length; i++) { 
		var record = records[i]; 
		if (record.Quantity < 50) { 
			quantity = record.Quantity; 
			break;
		} 
	} 

	if( quantity<50 ) {
		alert('Quantity on products cannot be less than 50.'); 
	} else { 
		sforce.apex.execute("CMR_DesignWebService", "UpdateToPMIDesign",{OpportunityId:'{!Opportunity.Id}'}); 
		alert('Opportunity Approved in PMI system.'); 
		window.location = window.location; 
	}
}

 
This was selected as the best answer
b.gonzalezb.gonzalez
Hi Yury,

That worked. Thank you for your help. I appreciate it!

Beth