[SalesForce] how to call javascript function from action function

found error after clicking button'Genrate invoice'

Error:: Uncaught SyntaxError: Unexpected token **

how to resolve this??

visual force page::

<apex:page standardController="Payment__c" extensions="Search" sidebar="false" showHeader="false"  id="pageid" docType="html-5.0">

<apex:commandButton value="Generate Invoice" onclick="GenerateInvoicePay()"/>
                    <apex:actionFunction name="GenerateInvoicePay" action="{!GenerateInvoice}" oncomplete="openConga('{!Payment_Id}');" reRender="pageid"/>

<Script Language="JavaScript"> 
var stringvalue='{!Payment_Id}';
             function openConga(stringvalue) 
             { 
             GenerateInvoicePay();

                alert('payment Id2');

                window.location.href = '/apex/APXTConga4__Conga_Composer?SolMgr=1&'+
                                       'id='+{!Payment_Id}'&'+
                                        'serverUrl={!$API.Partner_Server_URL_370}';
               } 

  </Script> 

controller::

      public Id Payment_Id {get;private set;}
            public Id  PaymId{get; set;}

            public PageReference GenerateInvoice() 
        {
        selectedEsr = new List<Payment__c>();
        for(wrappayment wp : paymentList)
            {
                if(wp.selected == true) 
                {

               Payment_Id=wp.pay.id;
                system.debug('Payment_Idline517'+Payment_Id);
                payid=Payment_Id;
                 system.debug('payidline520'+payid);

    }
    }

    return null;
        }

Best Answer

You are getting this error because you are not constructing the URL properly. You need to make few changes for this to work.

  • You are calling the actionFunction again from javascript method which takes your code into recursion which is not needed.
  • There is no need of actionFunction as you can call your javascript method from oncomplete event of commandButton.

This is the working code, see if it can help you

<apex:page standardController="Payment__c" extensions="Search" sidebar="false" showHeader="false"  id="pageid" docType="html-5.0">
    <apex:form >
        <apex:commandButton value="Generate Invoice" action="{!GenerateInvoice}" oncomplete="openConga('{!Payment_Id}');" reRender="pageid"/>
        <script Language="JavaScript"> 
            //var stringvalue='{!Payment_Id}';
            function openConga(valueFromParam) { 
                //GenerateInvoicePay();  //comment this as you don't need it.
                alert(valueFromParam);
                window.location.href = "/apex/APXTConga4__Conga_Composer?SolMgr=1&"+
                "id="+ valueFromParam + "&serverUrl={!$API.Partner_Server_URL_370}";
            } 
        </script>
    </apex:form>
</apex:page>