1

I need help. I have a visualforce page named "Edit Quote". I need a button to open this page on lightning. since javascript is not supported I created a button to open another vf page names “test_quote”, if javascript condition inside vf page “test quote” are met it should open “edit quote” page else an alert is displayed. Inside this page I put the javascript code.

test_quote:

<apex:page standardController="Opportunity" >
    <script type = "text/javascript"> 
    window.onload=function() 
{

if('{!Opportunity.N_of_tasks__c}' > 0) { 
    alert('It is not possible to create a quote for this Opportunity since there are tasks associated'); 
    window.history.back();
} 
else if('{!Opportunity.No_Quotes__c}' == 1) { 
    alert ("It is not possible to create a quote in this process stage");
    window.history.back();
} 

else if('{!Opportunity.Opportunity_Channel__c}'== "Italy") && '{!Opportunity.Opportunity_Type__c}' == 'Professional Services' && '{!Opportunity.Technical_Feasibility__c}' == '') { 
    alert('It is not possible to create a quote for this Opportunity since must have a feasibility associated'); 
    window.history.back();

else { 
    window.location.href = "/apex/0Q0/EditQuote?retURL={!Opportunity.Id}&oppid={!Opportunity.Id}&AccountId={!Opportunity.AccountId}" 
}  


 };
    </script>  
</apex:page>

I don't know why but it doesn't work. No error on developer console. Only a rerender of the page. though I try only with this:

<apex:page standardController="Opportunity" >
    <script type = "text/javascript"> 
    window.onload=function() 
{

if('{!Opportunity.N_of_tasks__c}' > 0) { 
    alert('It is not possible to create a quote for this Opportunity since there are tasks associated'); 
    window.history.back();

}  


 };
    </script>  
</apex:page>

Can someone help me?

Thanks

1 Answer 1

2

Okay, first of all, don't associate code to the onload handler. You're asking for Bad Things™ to happen. Second, quoting the integer values can have fun and unpredictable results. Don't do that. Third, avoid encoding errors by cutting out the middle man; you can use formulas to achieve the desired script results:

<apex:page standardController="Opportunity" >
    <script type="text/javascript"> 
        if( {!Opportunity.N_of_tasks__c > 0} ) { 
            alert('It is not possible to create a quote for this Opportunity since there are tasks associated'); 
            window.history.back();
         }  
    </script>
</apex:page>
2
  • I cretaed ten buttons with window.onload=function() and all works. Maybe my approach is not correct but these buttons works perfectly.
    – Frank
    Aug 31, 2018 at 10:30
  • @Frank The page itself uses body onload=..., so if you do that, it can have undesirable side effects. Feel free to ignore my warning at your own perils.
    – sfdcfox
    Aug 31, 2018 at 10:41

You must log in to answer this question.

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