function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zoom_VZoom_V 

How can I get the record id of a new record ?

I have been trying to use pagreferences but I am getting null values in the debug. I'm guessing it's because there is no id before the record is actually saved. I have put pagereferences before and after the insert. 

 

The whole reason I am trying to do it is because I would like to re-direct the user back to the Saved record in viewing state (not edit) after hitting saved. I'll be able to do it if I can just get the record id of the new record.

 

Anybody know how to do this ? 

 

Thank you very much.

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

Well are you saving the record and than returning a page reference in the same method?

 

I assume this is a follow up to you're other post... after you insert the record, it should have the Id already populated... so you don't have to go back to the page parameters to get it. Something like this should work:

 

public PageReference save()
{
    //logic...
    insert contract;    
    PageReference pr = new PageReference('/'+ contract.Id);        
    pr.setRedirect(true);
    return pr;
}

 

All Answers

Sean TanSean Tan

Well are you saving the record and than returning a page reference in the same method?

 

I assume this is a follow up to you're other post... after you insert the record, it should have the Id already populated... so you don't have to go back to the page parameters to get it. Something like this should work:

 

public PageReference save()
{
    //logic...
    insert contract;    
    PageReference pr = new PageReference('/'+ contract.Id);        
    pr.setRedirect(true);
    return pr;
}

 

This was selected as the best answer
Zoom_VZoom_V

That worked - thank you very much Sean !