function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jina ChetiaJina Chetia 

apex:actionFunction not working when called from Javascript

Hi

I have written this following piece of code for invoking controller method from Javascript but it does not work. I am not sure what I am missing in it,
Code:
<apex:outputPanel rendered="{!contractSummary}">
            <p><span style="padding:100px;"></span><apex:commandButton action="{!viewContractSummary}" value="View Contract Summary" styleClass="btn"/>
            <span style="padding:5px;"/><apex:commandButton value="Remove Term" onclick="validateRemove()"></apex:commandButton></p>
              <apex:actionFunction name="removeContractTerms" action="{!removeContractTerms}"></apex:actionFunction>
            <script>
                function validateRemove()
                {
                    var answer = confirm("Would you like to remove this Term from the Contract—")
                    if(answer)
                    {
                      var contractTermsId = document.getElementById('{!$Component.contractTermsId}')
                      removeContractTerms()
                    
                    }
                    else
                    {
                        alert("No"+contractTermsId)
                    }
                
                }
            </script>
       </apex:outputPanel>


Controller
 public PageReference removeContractTerms()
       {     
           delete contract_terms;
           return viewContractSummary();
           
       
       }  

  /* This method takes the user to the contract summary page */
       public PageReference viewContractSummary()
       {
        
            PageReference contSumPr = Page.contract_summary;
            contSumPr.getParameters().put('contractId', System.currentPageReference().getParameters().get('contractId'));
            contSumPr.setredirect(true);
            return contSumPr;
        
       }

 I tried both ways to call the Controller method - using the apex:actionFunction Name directly and also by using setTimeout(removeContractTerms,1000)

Can  anyone please look into this?

Thanks
Jina

dchasmandchasman
Using actionFunction in your page does not appear to be the best way to handle this - way more complicated than you need to make it. Try this out instead (w/out using actionFunction at all):

Code:
<apex:commandButton value="Remove Term" onclick="return confirm('Would you like to remove this Term from the Contract?')" 
action="{!removeContractTerms}"/>

 

Jina ChetiaJina Chetia
Thanks that worked. But in another page I am calling a controller function using onchange but even that is not working since I am using apex:actionFunction. Is there any alternative for this?

Code:
 <apex:actionFunction name="calEndDate" action="{!calEndDate}" />
<apex:inputField id="duration" value="{!contract.ContractTerm}" onchange="calEndDate()"/>
<p> <span style="padding :21px;"/> Contract End Date       :<span style="padding :14px;"/> <apex:outputField id="endDt" value="{!contract.END_DT__c}"/></p>

Controller
 
   /* This method calculates the End Date depending on the Contract Effective Date 
     and Contract Duration */
    public PageReference calEndDate() 
    {
        Date dat = getContract().StartDate;
        Integer months = getContract().ContractTerm;  
        if(dat != null && months != null)
        {
            contract.END_DT__c = dat.addMonths(months);
           
        }
        
        return null;
    }

 

jwetzlerjwetzler
Have you verified that your action method is being called?  Because even if it is, you're not rerendering anything so I don't think you'd see your outputField being updated even if the value is changing.  You could add rerender="endDt" to your actionFunction and see if that works.

But also, there's not really a reason to use actionFunction here, I would probably opt for actionSupport instead.  actionFunction is mostly for when you have to be able to call an action method from a block of javascript, and that's not really the case here.  actionSupport should work just fine (you'll still have to rerender the outputField).
SirishaMSirishaM
Hi,

I have a very similar problem ..so thought if one of you cud help me on this ..

My problem is

I have a command button which takes me to some page and there I add some records and when I return to the first page again ..I want to display only those records which satisfy some condition.

Below is my code for that


Code:
<apex:commandButton id="attach" value="Attach File" action="{!attach}" rerender="rl">
<apex:actionSupport action="{!checkdate}" event="oncomplete" rerender="rl"  />
</apex:commandbutton>

 

My controller is

Code:
public PageReference attach() {
asub= [select Assignment__c,name from Assignment_Submission__c where id= :ApexPages.currentPage().getParameters().get('id')];
Assignment__c asst=[select Due_Date__c ,name from Assignment__c where id= :asub.Assignment__c];
PageReference secondPage = new PageReference('https://na5.salesforce.com/p/attach/NoteAttach');

if(System.now()< asst.Due_Date__c)
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Due date passed');
ApexPages.addmessage(myMsg);
return null;
}
else{
secondPage.getParameters().put('pid',asub.id);
secondPage.setredirect(true);
secondPage.getParameters().put('retURL',Page.assignmentsubmissionpage.getURL()+'—id='+ApexPages.currentPage().getParameters().get('id'));
return secondPage;
}
}

 
And my checkdate function which checks the condition is

Code:
public PageReference checkdate(){
asub= [select Assignment__c,name from Assignment_Submission__c where id= :ApexPages.currentPage().getParameters().get('id')];
Assignment__c asst=[select Due_Date__c ,name from Assignment__c where id= :asub.Assignment__c];
for(Attachment[] attms: [select Body,Name,LastModifiedDate from Attachment where parentId= :ApexPages.currentPage().getParameters().get('id')])
if(attms.LastModifiedDate > asst.Due_Date__c)
delete attms;
return null;
}

 
My action support does not delete the latest attachments. Can you tell me where I am going wrong ?

Thanks,
Sirisha