Lee Fang
Lee Fang

Reputation: 51

commandbutton action

I have a commandbutton, When click I want it to save a phonecall record and redirect to the record page, after that open a new page in a new window which shows the editing page of a new case related to the call.

I am doing it like this:

//Pseudocode
public PageReference saverecord(){
   create a new phone call record;
   insert record;
   if(createnewcase)
      create case;
      insert case;
      create editcaseURL;
   return phonerecord page;
}

on the client side, In the commandbutton, I use <apex:param name="newcase" value="true" assignTo="{!createNewCase}"/> to set the createnewcase to true. and oncomplete javascript to open the popup window. I have test the phonerecord and caserecord seperately and succeeded. but when I put them in one class, the case was never created. and in the visualforce view state window. I can't even find the boolean createnewcase.

please help.

Thanks

Upvotes: 0

Views: 3495

Answers (3)

mast0r
mast0r

Reputation: 820

It is easy to implement with Apex / JavaScript:

The apex code:

// Defining two variables for the both records (case and phone)
public String caseId { get; set; }
public String phoneId { get; set; }

// Defining a boolean to render the JavaScript panel after saving
punlic Boolean redirectNow { get; set; }

public PageReference saverecord(){
    // Create a new phone call record;
    insert record;

    // Now reading a phone record id
    phoneId = record.id;

    if(createnewcase){
        create case;
        insert case;

        // Now reading a case id
        caseId = case.id;
    }

    // If case and phone are OK -> set this flag to true
    redirectNow = true;

    // We do not need to return a real page reference 
    // because we will redirecting with javascript
    return null;
}

And the Visualforce page with JavaScript:

<!-- A command button, that rerenders a panel with JavaScript -->
<apex:commandButton value="Save" action="{!saverecord}" reRender="doAfterSave"/>

<!-- This panel renders only after you will save the new records -->
<apex:outputPanel id="doAfterSave">
    <apex:outputPanel rendered="{!redirectNow}">
        <script>
            // First reading the id's
            var newCaseId = '{!caseId}';
            var newPhoneId = '{!phoneId}';

            if(newCaseId != '' && newPhoneId != '')
            {
                // Now opening a new window with case in edit mode
                window.open('/' + newCaseId + '/e');

                // And then redirecting the main page to the new phone record
                window.location = '/' + phoneId;
            }
        </script>
    </apex:outputPanel>
</apex:outputPanel>

Upvotes: 1

Adam
Adam

Reputation: 2605

Although this article is dated, if you really need to use JavaScript and popups to get this job done (as opposed to simply using Visualforce pages and passing parameters in the url, or using VF pages that share the same controller thereby maintaining the same context, member variable values, etc.), I'd take a look at this tutorial: Tutorial: Modal Dialogs in Visualforce using the Yahoo! User Interface Library

The essential trick is to make the "popup" really just a hidden aspect of the same page to circumvent issues associated with passing around values with JavaScript in Visualforce.

From the tutorial:

Building a link or button to popup a new Visualforce page is actually quite simple, and getting this popup to be above the current page is also quite easy. The problem occurs when you would like to collect information in the popup and pass that information back to the page that launched the popup.

The issue is that the new window is launched as a separate browser request when you use window.open(). Since this is a separate request to the server, the new page does not share the same controller context/session. Even if the two pages both use the name of the same controller! This is due to the fact that these are two different requests at the browser level.

Upvotes: 0

HMP
HMP

Reputation: 31

If you want to open newly created case in edit mode then use following code.

public PageReference saverecord(){
   create a new phone call record;
   insert record;
   if(createnewcase)
   {
      create case;
      insert case;
      return new PageReference('/' + case.Id + '/e');
   }
   else
     do other logic here

   return null;

Upvotes: 0

Related Questions