0

I'm trying to edit a custom OnClick JS button which creates a PDF from a quote, provided the quote passes certain checks.

this code works:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
var q = new sforce.SObject("Quote"); 
q.id = "{!Quote.Id}"; 
q.Status = "Presented"; 
result = sforce.connection.update([q]); 

if('{!Quote.Status}'!= 'Cancelled'&&{!ISNULL(Quote.Invoice_Address_Lines__c)=FALSE}&&{!ISNULL(Quote.Invoice_Address_Country__c)=FALSE}) 
{ 
do some business logic here... 
var isOk = true; 
} if(isOk) 
{ 
var pdfOverlay = QuotePDFPreview.quotePDFObjs['quotePDFOverlay']; 

pdfOverlay.dialog.buttonContents = "<input value=\'Save to Invoice Overview\' class=\'btn\' name=\'save\' onclick=\"QuotePDFPreview.getQuotePDFObject(\'quotePDFOverlay\').savePDF(\'0\',\'0\');\" title=\'Save to Quote\' type=\'button\' ><input value='Save and Email' class='btn' name='saveAndEmail' onclick=\"QuotePDFPreview.getQuotePDFObject(\'quotePDFOverlay\').savePDF(\'1\');\"; title='Save and Email Quote' type='button' ><input value=\'Cancel\' class=\'btn\' name=\'cancel\' onclick=\"QuotePDFPreview.getQuotePDFObject(\'quotePDFOverlay\').close();\" title=\'Cancel\' type=\'button\' >"; 

//change this to use the correct template for your business/environment!! 
//TB: This has been adapted to choose the correct template based on the result Quote_Template_ID__c returns 
pdfOverlay.summlid = '{!Quote.Quote_Template_ID__c}'; 

pdfOverlay.setSavable(true); 

//change this to use the quote id 
pdfOverlay.setContents('/quote/quoteTemplateDataViewer.apexp?id= {!Quote.Id}','quote/quoteTemplateHeaderData.apexp?id={!Quote.Id}'); 

pdfOverlay.display(); 
} 
else 
{ 
//raise an alert to let the user know about some business rule 
alert('Invoices must have an Address and Country specified and cancelled Invoices cannot be regenerated.'); 
}

I need to edit the initial "if" to add a check for a situation where the invoice in question is marked as needing a VAT number (VAT_Number_Invoice__c) AND the VAT number (VAT_Number__c) is blank.

I'm replacing

if('{!Quote.Status}'!= 'Cancelled'&&{!ISNULL(Quote.Invoice_Address_Lines__c)=FALSE}&&{!ISNULL(Quote.Invoice_Address_Country__c)=FALSE}) 
{ 

with

if(
'{!Quote.Status}'!= 'Cancelled'&&
{!ISNULL(Quote.Invoice_Address_Lines__c)=FALSE}&&
{!ISNULL(Quote.Invoice_Address_Country__c)=FALSE}&&
(
{!ISNULL(Quote.VAT_Number__c)=FALSE} ||
'{!Quote.VAT_Number_Invoice__c}' = 'FALSE')
)
{

or variations on this. I cant seem to get past left hand side alignment errors, or the code just not picking up the last condition.

Has anyone got any ideas how to get this code working? Am I doing anything obviously wrong?

essentially: build the PDF if (1 isnt blank AND 2 isnt blank AND 3 isnt blank AND (4 isnt blank OR 5 is false))

2 Answers 2

2

I always recommend evaluating your conditions within the merge field, which reduces the probability of errors. Here's a revised if statement:

if({!NOT(ISPICKVAL(Quote.Status,'Cancelled'))&&
    NOT(ISBLANK(Quote.Invoice_Address_Lines__c))&& 
    NOT(ISBLANK(Quote.Invoice_Address_Country__c))&&
    (NOT(ISBLANK(Quote.VAT_Number__c)) ||
     NOT(Quote.VAT_Number_Invoice__c))}) {

Using this method will force compiler errors when you try to save the code, which prevents trivial mistakes, and eliminates the various errors you might get when using merge syntax with semi-complicated JavaScript.

7
  • VAT_Number_Invoice__c is a checkbox, so i changed this to if({ !Quote.Status!='Cancelled'&& NOT(ISBLANK(Quote.Invoice_Address_Lines__c))&& NOT(ISBLANK(Quote.Invoice_Address_Country__c))&& (NOT(ISBLANK(Quote.VAT_Number__c)) || NOT(Quote.VAT_Number_Invoice__c) = TRUE) }) { this then caused an error "unexpected token !"
    – user47837
    Aug 17, 2017 at 13:19
  • @user47837 Do not separate the { and ! at the beginning of the merge field. There must be no intervening space.
    – sfdcfox
    Aug 17, 2017 at 14:28
  • if('{!Quote.Status}'!= 'Cancelled'&& NOT(ISBLANK(Quote.Invoice_Address_Lines__c))&& NOT(ISBLANK(Quote.Invoice_Address_Country__c))&& (NOT(ISBLANK(Quote.VAT_Number__c)) || NOT(Quote.VAT_Number_Invoice__c) = TRUE)} ) is still showing an invalid left hand side in assignment error
    – user47837
    Aug 23, 2017 at 10:42
  • @user47837 The entirety of the stuff inside the if(...) should be a merge field. Based on your prior feedback, Ive updated the code in my answer. Copy that code exactly and start from there.
    – sfdcfox
    Aug 23, 2017 at 12:58
  • copying in the code exactly causes "Field Quote.Status is a picklist field. Picklist fields are only supported in certain functions." The original code had single quotes ' around {!Quote.Status}. I've tried adding this back in, but struggling to get them in the right place to not cause "unexpected token" errors in the button, with characters from later in the formula.
    – user47837
    Aug 23, 2017 at 13:14
-1

Replace your if condition as below

if(
'{!Quote.Status}' != 'Cancelled' &&
{!NOT(ISNULL(Quote.Invoice_Address_Lines__c))} &&
{!NOT(ISNULL(Quote.Invoice_Address_Country__c))} &&
({!NOT(ISNULL(Quote.VAT_Number__c))} || '{!Quote.VAT_Number_Invoice__c}' == 'FALSE')
)

From the above if condition, you want to check if Quote.VAT_Number_Invoice__c is not False.

('{!Quote.VAT_Number_Invoice__c}' = 'FALSE') will basically process the expression within the curly braces, hence it was trying to assign the value which is incorrect, whereas

('{!Quote.VAT_Number_Invoice__c}' == 'FALSE') will check if it false or not. For string comparison in javascript, use == instead of =

Refer VisualForce functions for more info.

6
  • 5
    Please do not just post a code dump but rather explain using words what the OP was doing wrong and how this change will fix it.
    – Adrian Larson
    Aug 2, 2017 at 12:27
  • Yes sure. I will explain it
    – Ankita
    Aug 2, 2017 at 12:28
  • 3
    Your explanation seems way off base. Inside a merge field, single or double equals both perform comparison.
    – Adrian Larson
    Aug 2, 2017 at 12:36
  • the only thing that doesnt make sense from the answer posted above is that I now need to add !NOT into areas of the formula which were working before? can someone explain the logic of this?
    – user47837
    Aug 2, 2017 at 12:46
  • Updated the answer
    – Ankita
    Aug 2, 2017 at 12:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .