+ Start a Discussion
manan patel 7manan patel 7 

How to get id after insert data in visualforce page to open new window with that id

LBKLBK
If you are inserting an accout record, here is how you get the ID of the record.
 
Account acc = new Account();
acc.Name = 'My Account';
acc.Country = 'IN';

insert acc;
String accID = (String)acc.Id;
Hope this helps.
 
manan patel 7manan patel 7
i am trying to get id and redirect to other page with that id, i am using pagereference but that is not work for me from backend .
if you understand betterly then please give me a solution as soon as posible.
 
LBKLBK
Hi Manan,
Can you post your code, so that we can help you out?
Typically, a Save method that will redirect to the newly created record will look like this.
public PageReference save() {
	Account acc = new Account();
	acc.Name = 'My Account';
	acc.Country = 'IN';

	insert acc;
	String accID = (String)acc.Id;        
	PageReference page = new PageReference('/' + accID);
	page.setRedirect(true);
	return page;	
}
If you want to open the new page in a separate window, you need to work with ActionFunction and Javascript.

Here is the sample code for that.
 
VF Page
<apex:page controller="clsMyController">
<script>
    function fnSave()
    {
        saveApexMethod() ;
    }
    function OpenPopup(sID){
        window.open('/' + sID);
    }
</script>
<apex:form >
<apex:actionFunction name="saveApexMethod" action="{!save}" immediate="true"  onComplete="OpenPopup('{!sAccID}');"/>
  <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" onclick="fnSave();"/>
        </apex:pageBlockButtons>
  </apex:pageBlock>
</apex:form>
</apex:page>


//APEX Code
public class clsMyController {
    public String sAccID {get; set;}
    
    public PageReference save() {
		Account acc = new Account();
		acc.Name = 'My Account';
		acc.Country = 'IN';

		insert acc;
		sAccID = (String)acc.Id;
		return null;
    }

}
Let me know if this helps.