+ Start a Discussion
b.gonzalezb.gonzalez 

Field Product.Quantity does not exist. Check spelling? - Custom JavaScript button

I am modifying a custom javascript button to include a validation that prevents the user from entering quanitities less than 50 for each product that is listed on the Opportunity record. Products are a related list on the Opportunity record. 

However I am not sure how to reference the Quantity field in the button. See bolden section. Please help!
 
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/19.0/apex.js")} 

var strStatus = '{!Opportunity.Opportunity_Status__c}'; 
switch(strStatus) { 
case 'Denied': 
alert('This opportunity cannot be approved because it has been denied.'); 
break; 
case 'Closed': 
case 'Awaiting Claim': 
case 'Claim Submitted': 
case 'Closed Expired': 
case 'Closed Won': 
case 'Closed Lost': 
alert('This opportunity cannot be updated.'); 
break; 
} 
var strAmount = '{!Opportunity.Amount}'; 
switch(strAmount){ 
case '$0.00': 
alert('This opportunity cannot be approved because no products are listed.'); 
break; 
} 
var strQuantity = '{!Product.Quantity}'; 
switch(strQuantity){ 
case 'Quantity < 50'  
alert('Quantity on products cannot be less than 50.'); 
break; 
default: 
sforce.apex.execute("CMR_DesignWebService", "ApproveToPMIDesign",{OpportunityId:'{!Opportunity.Id}'}); 
alert('Opportunity Approved in PMI system.'); 
window.location = window.location; 
break; 
}

 
YuchenYuchen
Try Product2.Quantity. In the backend, the API Name for product is actually Product2. 
b.gonzalezb.gonzalez
Hi Yuchen, unforunately 'Product2' did not work. I ended up using {!OpportunityLineItem.Quantity}'. That appeared to work. However when test the button, I recieve the folllowing error: "A problem with Onclick Javascri[pt for this button or link was encountered. Unexpected Identifier".
 
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/19.0/apex.js")} 

var strStatus = '{!Opportunity.Opportunity_Status__c}'; 
switch(strStatus) { 
case 'Denied': 
alert('This opportunity cannot be approved because it has been denied.'); 
break; 
case 'Closed': 
case 'Awaiting Claim': 
case 'Claim Submitted': 
case 'Closed Expired': 
case 'Closed Won': 
case 'Closed Lost': 
alert('This opportunity cannot be updated.'); 
break; 
} 
var strAmount = '{!Opportunity.Amount}'; 
switch(strAmount){ 
case '$0.00': 
alert('This opportunity cannot be approved because no products are listed.'); 
break; 
var strQuantity = '{!OpportunityLineItem.Quantity}'; 
switch(strQuantity){ 
case 'Quantity < 50' 
alert('Quantity on products cannot be less than 50.'); 
break; 
default: 
sforce.apex.execute("CMR_DesignWebService", "ApproveToPMIDesign",{OpportunityId:'{!Opportunity.Id}'}); 
alert('Opportunity Approved in PMI system.'); 
window.location = window.location; 
break; 
}

 
BalajiRanganathanBalajiRanganathan
1) you have to use for loop as one opportunity will have multiple products.{!Opportunity.OpportunityLineItems} will give the list of line items.
2) Also expressions {! Opportunity.Amount} will be evaluated on server so it will not have values entered by the user in UI unless it has been updated in the server. you might have to use document.getElementById using $Component global variables.

http://www.salesforce.com/docs/developer/pages/Content/pages_variables_global_component.htm