0

How can I call a method from my controller by using an SLDS VF page. I am using a button icon, that is showing on each record returned from a search. I would like when clicked to call a method from my controller. This is what I have:

<td>
  <button class="slds-button slds-button_icon slds-button_icon-brand" title="Create Contact">
    <svg class="slds-button__icon slds-icon_small" aria-hidden="true">
        <use xlink:href="{!URLFOR($Asset.SLDS, 'assets/icons/standard-sprite/svg/symbols.svg#contact')}"></use>
    </svg>
    <span class="slds-assistive-text">Create Contact</span>
  </button>
</td>

I did try adding action="{!newContactButton}" inside the button class after the title, but I get an error:

Unknown Property controller.method.

Typically with VF pages we define actionfunction, but I wonder with SLDS if I should use a different tag. If I use apex:commandbutton, things seem to work, but I would like to use icon button with button class tag. Thanks

1 Answer 1

0

You can still do it using action function as below:

<a onclick="myActionFunction('{!record.Id}');" class="slds-button slds-button_icon slds-button_icon-brand" title="Create Contact">
        <svg class="slds-button__icon slds-icon_small" aria-hidden="true">
            <use xlink:href="{!URLFOR($Asset.SLDS, 'assets/icons/standard-sprite/svg/symbols.svg#contact')}"></use>
        </svg>
        <span class="slds-assistive-text">Create Contact</span>
    </a>

    <apex:actionFunction name="myActionFunction" action="{!myApexAction}" reRender=""  >
        <apex:param name="recordId" value=""/>
</apex:actionFunction>

In Apex:

public void myApexAction(){

    //either of the next 2 lines will work, 
    string passedParam1 = Apexpages.currentPage().getParameters().get('recordId');
    string passedParam2 = system.CurrentPageReference().getParameters().get('recordId');
    system.debug(passedParam1);
    system.debug(passedParam2);

    // implement pagereference based on recordId
}
5
  • This seems to be working, as I am able to call my method. Unfortunately, my method is set to route the user to another page, but yet, when I click the button, I remain on the same page. Must be missing something. public pageReference newExistingContactButton(){ System.debug('See if method is executing!!!'); pageReference newContact = new PageReference('/apex/NewContactGMS'); return newContact; }
    – Ivo Dimov
    Commented Aug 5, 2019 at 16:10
  • try giving page name directly in return page reference Commented Aug 5, 2019 at 16:14
  • Thank you for the info. It did not work. I think the problem is that each button appears for the row of my returned results. I have the feeling I must pass a value inside the onclick java function so it references that specific record. I am guessing this is the reason why, the page is not being routed.
    – Ivo Dimov
    Commented Aug 5, 2019 at 18:04
  • modified answer for that Commented Aug 5, 2019 at 18:14
  • That is what did it. Thank you so much !!!
    – Ivo Dimov
    Commented Aug 5, 2019 at 19:01

You must log in to answer this question.

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