3

I have a html input button which calls a javascript function in VF page. After execution of javascript function i want to open a detail page of an account in a new apex window. for that i am writing like below

<input type="button" value="Save" onclick="saveImage()"/>
<apex:actionFunction name="savefn" action="{!saveData}" rerender="">
        <apex:param name="x" value="" assignTo="{!saveImageValue}" />
</apex:actionFunction>

public void saveData()
{
String accid,accName;
public String account {get;set;}
for(Account a:[select id,name from Account where id=:account])
{
    accid=a.Id;
    accName=a.Name;
}
//doing my stuff-----
openDetailPage(account);
}
public PageReference openDetailPage(String acc) 
{ 
 /**Acc contains the id of the account in string format**/
 pageReference ref = new PageReference('/'+acc); 
 ref.setRedirect(true); 
 return ref; 
}

I am able to debug out opendetailpage logs but i am unable to open the page. could any one point me where i am doing wrong ?

6
  • is your method in the controller really named saveDataData ? That won't match the actionfn. And, I don't see a reference to variable declaration for acc - I do see accid
    – cropredy
    Commented Aug 18, 2014 at 13:45
  • @crop1645 sorry typo mistake. I updated the code. acc is the parameter sent in openDetailPage Commented Aug 18, 2014 at 13:50
  • ok, and where is account declared ? and how is it related to the actionFunction? You would be well-served by using better names - account should represent an Sobject, acctId should represent an accountId
    – cropredy
    Commented Aug 18, 2014 at 13:56
  • @crop1645 i didnot pasted all the class. i just posted the snippet where the issue is . As you said account is SObject and accid is accountid Commented Aug 18, 2014 at 14:00
  • Try to pack you saveData functionallity to the openDetailPage method and call it directly from the action function. Commented Aug 18, 2014 at 14:08

1 Answer 1

1

The code you've pasted above doesn't seem to be connected together correctly. The HTML button calls a function saveImage() but your action function is called savefn. Is there something happening that calls the one from the other?

If the javascript is working, I don't think the Apex is going to work as you aren't returning a PageReference.

the action function calls the Apex method saveData which is void - it doesn't return anything. Although saveData calls openDetailPage, which does return a PageReference, the PageReference it returns is ignored in the Apex and not returned from the action.

If you want the Apex to calculate and PageReference and return it, you should define saveData as an action method:

public PageReference saveData()
{
    //do things
    //call processing method...
    //do more things

    return new PageReference('/' + accId);
}

You don't have to set redirect on it. It's definitely going to redirect anyway.

You say you want to open the Account detail page in a new window. Do you mean like a popup window, so the account detail opens and the page you were on is also still there? You can't directly do this from Apex, but it's possible to do it by having the reloaded VF open the window for you. Let me know if that's what you mean.

As a style note, SOQL returns objects so you don't need to put values into variables.

Account acc;
acc = [select id,name from Account where id=:account];

Is much neater than

String accid,accName;
public String account {get;set;}
for(Account a:[select id,name from Account where id=:account])
{
    accid=a.Id;
    accName=a.Name;
}

and it gives you a single pointer to the entire Account, no matter how many fields you bring back from the database.

You must log in to answer this question.

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