+ Start a Discussion
yvk431yvk431 

Need help on the command button

Hi ,

 

I am trying to implement the custom dependency for a couple of picklists and wanted to have standard for the remaining picklists: I am able to achieve it partially but the major concern was when I hit Save command button, the required validations are not working, i am calling action as well as oncomplete in which I am refreshing the parent page and closing the current page. Every time the page was closing ofcourse the save was not happening so the validation was working but before it gets displayed the page was getting closed.

 

I am able to view validation only when I remove the oncomplete action on command button, tried using immidiate="false" , still no luck. Tried using action function too.

 

I am using standard controller with an extension.

 

<apex:commandButton value="Save" action="{!SaveOppty}" oncomplete="javascript&colon;savepage();" immediate="false" />

<script>

function savepage()
       {
         
           window.opener.location.href="/{!$CurrentPage.parameters.id}";
           self.close();
       }

</script>

 

--yvk

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You aren't giving the page a chance to rendered the errors - you are simply closing the page as soon as the action completes regardless of the outcome.

 

I'd move the close to some javascript that is only rendered when the action has completed successfully.  Something like:

 

<apex:outputPanel rendered="{!doClose}>
   window.opener.location.href="/{!$CurrentPage.parameters.id}";
   self.close();
</apex:outputPanel>

You'll need a custom/extension controller to handle the save:

 

public boolean doClose {get; set;}
private ApexPages.StandardController stdCtrl;

public MyController(ApexPages.StandardController std)
{
   stdCtrl=std;
   doClose=false;
}

public PageReference save()
{
   try
   {
      stdCtrl.save();
      doClose=true;
   }
   catch (Exception e)
   {
      doClose=false;
   }
}

 The try/catch may not be necessary, but hopefully you get the idea.

 

 

All Answers

bob_buzzardbob_buzzard

You aren't giving the page a chance to rendered the errors - you are simply closing the page as soon as the action completes regardless of the outcome.

 

I'd move the close to some javascript that is only rendered when the action has completed successfully.  Something like:

 

<apex:outputPanel rendered="{!doClose}>
   window.opener.location.href="/{!$CurrentPage.parameters.id}";
   self.close();
</apex:outputPanel>

You'll need a custom/extension controller to handle the save:

 

public boolean doClose {get; set;}
private ApexPages.StandardController stdCtrl;

public MyController(ApexPages.StandardController std)
{
   stdCtrl=std;
   doClose=false;
}

public PageReference save()
{
   try
   {
      stdCtrl.save();
      doClose=true;
   }
   catch (Exception e)
   {
      doClose=false;
   }
}

 The try/catch may not be necessary, but hopefully you get the idea.

 

 

This was selected as the best answer
yvk431yvk431

thanks allot, i knew that I am doing it wrong but didnt find the solutions any where, it worked and saved my day.

 

Just needed to put the script tag inside the outputPanel though :)  thanks again.

 

--yvk

bob_buzzardbob_buzzard

I always leave a typo in as an exercise for the avid student ;)

yvk431yvk431

Hi bob,

 

sorry to bother you again, can you provide me any info on refresh parent page from all the browsers window.opener.location.href is not working in IE8. Let me know if its not possible as I went through so many posts but didnt find any.

 

--yvk

 

 

bob_buzzardbob_buzzard

I've checked back to the last time that I did this sort of thing and I had the following code - can't remember if it was IE specific though:

 

var winMain=window.opener;
if (null==winMain)
{
   winMain=window.parent.opener;
}

 

yvk431yvk431

no luck Bob,

 

the thing is the window.opener is not coming null. I tried to alert it and its same as in firefox. Gone through other javascript communities some said its not possible to make it compatible on all browsers some gave there own versions. But finally the solutions i went through is refreshing the parent from the parent itself which will be called from the unload event of the child, but in my case the parent is an standard detail page, any thoughts ?

 

--yvk

 

bob_buzzardbob_buzzard

There's probably some sensitivity on the browser's part to the fact that your page is opened from a salesforce domain, but the page itself is from a visualforce domain.  When I was doing this sort of thing both the parent and popup page were from visualforce, and the child asked the parent to close it and took any other action. Can you put your button into an embedded visualforce page in the record view?  Then the child can notify the parent that it should be closed and the main page refreshed.

yvk431yvk431

I will check with my clients, well we may need to say them that they have to do a manual refresh atleast from IE(if it didnt happens). Latest update is it was working fine on some IE versions. Donno when(my life time) I will get to know the IE behaviour :)

 

Thanks, for your valuable suggestions Bob.

 

 

--yvk