Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I'm attempting to build a custom related list from scratch, with a whole set of new functionality, and in doing so, am replicating the way the default related list looks and feels. Part of this includes the Del hyperlink, which pops up with a "Are you sure?" message, and then deletes, and refreshes the page, or does nothing, based on user input.

My related list is located inside a Component, which is then added to a visualforce page, with the Projects id value handed to it. The related list takes this id, and finds objects related to it, (In this case, an Invoice), and pulls those values out, depending on RecordType, which is part of the parameters for the component.

What I am trying to do, is have the user be able to click on a link on the component, and have that link run a javascript function, which creates the pop up window, and then either runs a Controller function, or does nothing. Right now, I have it set up akin to this:

<component header.. >
<script>
  function FuncName()
  {
       CallApexControllerMethod();
  }
</script>
//.. we start looping through the invoices.. 
<apex:commandLink action="javascript:if (window.confirm('Are you sure? You should usually close an invoice, instead of deleting it, as all data will be lost when deleting an invoice.')) FuncName();">
<apex:param name="idVal" value="{!i.Id}" assignTo="{!idVal}"/>
Del
</apex:commandLink>
//Continue looping.. 
<apex:actionFunction name="CallApexControllerMethod" action="{!DeleteSchedule}" />
//End table.. 

And in my controller, I have the following method:

public String idVal { get; set; }

public PageReference DeleteSchedule()
{
    String id = idVal;

    if (id != null)
    {
        Invoice_Schedule__c tobeDeleted = null;
        for(Invoice_Schedule__c i : invoices_not_gen) 
          if (i.Id == id) {
            tobeDeleted = i;
            break;
          }

       //if account record found delete it
       if (tobeDeleted != null) {
         Delete tobeDeleted;
  }

The problem that keeps coming up, is that FuncName is never found, I keep getting errors where its saying "JavaScript Function Not Defined" in my Chrome. I've tried a whole set of solutions, from changing the syntax and how I call the function, removing the script from the component and putting it on the page instead (so I avoid having three functions with the same name), making the function a variable to try to get it to have a global scope, to removing as much as I can, and just calling a method or two, and nothing seems to have worked- in all cases, I cannot get the function to be "defined". Any ideas how I would go about doing this? Am I missing some obvious flaw?

I come from a c# background, and my web experience mostly comes from ruby on rails and asp.net, and I have avoided javascript like the plague after a set of experiences while in high school, when I was doing a lot of commissioned work, and I would continuously run into errors, and due to a lack of a debugger or other such tool, I would have to dig through code by hand. I've already done all of that with this, and I still can't figure out why its not working.

Thanks to everyone for the replies- I got a few ideas from them, and Im going to give them a shot on Monday- will post back here, and mark best answer when I find a solution then.

share|improve this question
add comment

4 Answers 4

Have a look at JavaScript Remoting for Apex Controllers

https://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm

share|improve this answer
    
This will most likely be my backup plan if I cannot get the ActionFunction to work, I would prefer an ActionFunction as it is easier and more reliable to use. –  Sean Healy Jul 11 at 16:13
1  
Actually, JavaScript remoting is best practice for this - it's just as reliable as an action function, and more efficient, since the viewstate is not passed back and forth. The main tradeoff is that the remote function needs to be static, so the JavaScript will need to pass some context, rather than picking it up from controller instance variables. –  metadaddy Jul 11 at 16:26
add comment

I believe you are looking for this logic Apex:ActionFunction

I'll try to provide an example later if i can but the doc is pretty good and i'm heading to a long meeting.

share|improve this answer
    
I used the following link to try to get this code to work, which follows this method of calling a JavaScript function. link The code in it is nearly identical to mine, and mine does not work. The reference material was another source I did use to try to get it to work, to no avail. –  Sean Healy Jul 11 at 16:11
add comment

Please add the following code in your component/page :

<apex:actionFunction name="CallApexControllerMethod" action="{!DeleteSchedule}">

</apex:actionFunction>

If "DeleteSchedule" method is in your parent page controller then add the above code in your parent VF page where you are calling the component under tag or if "DeleteSchedule" method is in component controller then add the above code in component.

If you want to refresh a particular section then you can use "rerender" attribute in "". For further knowledge on how to use apex:actionFunction you can refer VisualForce developer guide.

I hope this will help you.

share|improve this answer
    
Missed adding this line after I pulled all the code out of the page to get it to render properly for my boss, my bad! –  Sean Healy Jul 11 at 16:47
    
It happens @Sean Healy –  tandonprateek Jul 11 at 16:53
add comment
up vote 0 down vote accepted

Despite a large amount of playing with the javascript code, I was unable to get to get it to work, and therefore, made my own dialog that appears when the user clicks on the link, and preforms the logic I need.

Thanks for the help anyway!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.