You need to sign in to do that
Don't have an account?
Denise Crosby
apex commandlink action not working
I am using some simple code to execute an EditAccount method after an apex commandLink is clicked. But, I can't get the method to execute and cannot find a good solution after searching existing questions. Is there something wrong that I'm doing? Thanks
Visualforce page:
Custom Controller:
Thanks so much for helping.
Visualforce page:
<apex:page controller="IMS_CP_Projects_controller1"> <html> <body> <apex:form> <apex:outputPanel id="editaccounts"> <table id="dataTable"> <thead> <tr> <th>Account Name</th> <th>id</th> <th>Edit</th> </tr> </thead> <tbody> <apex:repeat value="{!Accounts}" var="A"> <tr> <td data-label="Name">{!A.Name}</td> <td data-label="id">{!A.id}</td> <td data-label="Edit"> <apex:commandLink action="{!EditAccount}" value="Edit" id="editcmd"> <apex:param assignTo="{!editid}" value="{!A.id}" name="editid" /> {!A.id} </apex:commandLink> </td> </tr> </apex:repeat> </tbody> </table> </apex:outputPanel> </apex:form> </body> </html> </apex:page>
Custom Controller:
public class IMS_CP_Projects_controller1 { public string editid { get; set; } public transient List<Account> Accounts { get; set; } public IMS_CP_Projects_controller1() { Accounts = [SELECT id, name FROM account LIMIT 20]; } public void EditAccount() { System.debug('-------- Edit Account --------'); } }
Thanks so much for helping.
It is one of the trap of the Salesforce VFP that needs a workaround and some tricks.
Best regards
Alain
All Answers
It is one of the trap of the Salesforce VFP that needs a workaround and some tricks.
Best regards
Alain
Why do you need a rerender while you change completely the navigation finally? That is the trap (old bug)
I wish I could like your response two or more times.
Just 1 minor edit to the apex class, in case the intention of the commandlink was to open the record in edit mode then is more appropriate.
Denise used nothing in fact for the redirection in her question and I used a very basic redirection indeed.
You have posted an excellent suggestion with the '/e' for the edition directly.
Best regards
Alain
Thank you for these great suggestions. I will try them tonight. My page is not redirecting to any other page to do the edit. It is using booleans to render different apex outputpanels depending on what link/method you are on within the same page. I don't think it's a great design, but I've inherited it.
Thanks again. Will post again when I know it works.
Denise
One followup question. Is it absolutely necessary to return a pageReference? I want to return null so I can stay on the same page. There are many booleans set for rendering different apex outputpanels on the page so I don't want to go to a new page. Sorry if this is a real basic question.
You need to return a pageReference and the same page is just : return null indeed.
You can just rerender a part of your form (used above with a <apex:panelGroup> ( you can use almost all the apex tags instead of panel group).
You can use: public pageReference EditProject() { return null; }
That is allowed and very common.
If you have a current code that doesn't work, you can post it and we can fix the problem.
You can rerender many panels.
<apex:commandLink action="{!EditAccount}" value="Edit" id="editcmd" reRender="myPanel1,myPanel2">
Tell me, Denise, if you have any new problem.
Best regards
Alain
Thanks for all your great suggestions.
No matter what I try, I cannot get the Edit button to render the page correctly, even though it is now executing the EditProject method. Let me know if I should enter this as a new question. I can mark your answer here as Best Answer. I think it's related to all the outputpanels being rendered based on boolean values, but I'm not sure.
Thank you so much for helping!
You have posted the same controller twice?
The VFP is also interesting.
Here is the VFP
You have got all the traps together. <apex:outputPanel> is very capricious and doesn't respond correctly to the rerender (?).
rerender is stil interesting to be used and necessary
<apex:commandLink rerender="editprojects" <apex:panelGroup id="editprojects"> <apex:panelGroup id="editprojects"> responds correctly. It is a complication that seems useless but that works.
8,000 lines? it is much too big.
And the few lines, you have posted are already tricky with many variables and combinations (+ all the things that should work but don't work).
<apex:outputPanel> is a very common component but combined with rerender and rendered, that doesn't work here for your sample (?) but all your code is correct in principle. That really is quite unnerving.
The following articles are interesting but old now and with the commandlink, that should work without a workaround ... excepted in an iteration (?).
Jeff Douglas: This post is a slight tweak of yesterday's post, Passing Parameters with a CommandLink. In theory you should just be able to switch out the CommandLink component with a CommandButton component and be golden. However, not so fast. There seem to still be a bug with the CommandButton component.
https://blog.jeffdouglas.com/2010/03/04/passing-parameters-with-a-commandbutton/
https://blog.jeffdouglas.com/2010/03/03/passing-parameters-with-a-commandlink/
He talked about the workaround with "rerender" in his blog but I don't retrieve this part.
In brief, there are bugs and when you are trapped, you are losing a lot of time (all the code is correct according the documentation but that doesn't work).
For commandbutton in a loop: it is one solution: https://developer.salesforce.com/forums/?id=9060G000000BiTJQA0
With salesforce, every four months, that could work better or badly as you know so some old articles (more than some months) can be obsolete and fixed (known issues).
Best regards
Alain
I finally got my page done and it's working great. Thanks again for your help. I couldn't have done it without you.
Congrats! It is one of the most dreaded problem with a VFP. I suffered a lot myself with a similar page using some commandbutton and commandlink as well as Jeff Douglas, Trailhead Developer Advocate for modules with Apex and VFP, a great expert who gave the basis for a solution on his blog. That drives all of us nuts until that works finally when all the workarounds and tricks are in place (very time consuming)..
Best regards
Alain