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
theitdeptrockstheitdeptrocks 

How to create a record and grab the new record ID

I am creating a questionnaire in which a record is created after the completion of page one.  Upon clicking "Next" the record is checked for validation errors, if none it saves and if there are then the page displays those erros.  On multiple page questionnaires, I'm not sure how to grab the ID of the record created on page one so it can be updated with the responses from questions located on page 2.

 

Here is the code I am using:

public PageReference doSave() {

    try {

        this.ctr.Save();

    }  catch(DmlException ex){

        //this catches the errors and ensures they register on the page

        ApexPages.addMessages(ex);

    }

 

 

    //if there's an error message, perform a refresh

    // if not, redirect to google.com

    if (ApexPages.hasMessages()) {

        return null;

    } else {

        return new PageReference('url of page 2');   

    }

}

 

 

Any ideas?

Best Answer chosen by Admin (Salesforce Developers) 
ahab1372ahab1372

 


blombardis wrote:

After insert the object you can grab the ID with object.Id

 

 

 insert o;
 id = o.Id;

 Ant then you can give that Id to the second page for example with GET

 

 

Hope this helps,

Bruno


 

using the above example, your page Refrence would look like this:

 

return new PageReference('/' + o.Id);   

 

 

 

All Answers

blombardisblombardis

After insert the object you can grab the ID with object.Id

 

 

 insert o;
 id = o.Id;

 Ant then you can give that Id to the second page for example with GET

 

 

Hope this helps,

Bruno

ahab1372ahab1372

 


blombardis wrote:

After insert the object you can grab the ID with object.Id

 

 

 insert o;
 id = o.Id;

 Ant then you can give that Id to the second page for example with GET

 

 

Hope this helps,

Bruno


 

using the above example, your page Refrence would look like this:

 

return new PageReference('/' + o.Id);   

 

 

 

This was selected as the best answer
theitdeptrockstheitdeptrocks

Thanks to both of you.  It is working for me.

giribabu gedagiribabu geda

Hi all,

 

  i get the following exception

10:44:53.185|EXCEPTION_THROWN|[103,9]|System.SObjectException: Field is not writeable: Bureau_Product__c.Job__c

 

below is the code that i am writing

Public Static String ClaimOrder(String OrderXML)
  {
    Account job =new Account(); 
    Testing__c reportuser = new Testing__c();   
    //List<Bureau_Product__c> products = new List<Bureau_Product__c>();
    //List<Report_User__c> reportUsers = new List<Report_User__c>();
    //Bureau_Invoice__c invoice =new Bureau_Invoice__c();        
    String  candidates=null;
    String tests=null;        
    String Response;
    XmlStreamReader reader =new XmlStreamReader(OrderXML);
    Savepoint sp =null;
    try
    {
      while(reader.hasNext())
      {   
        if(reader.geteventtype()==Xmltag.START_ELEMENT && reader.getLocalName()=='row' )
        {     
          //job.Trained_User_Email__c=reader.getAttributeValue(null,'ows_TrainedUserEmail');
          //job.Online_Bureau_Ref__c=reader.getAttributeValue(null,'ows_Title');
          //job.D_Number__c=reader.getAttributeValue(null,'ows_CustomerNo');
          //job.Job_Type__c=reader.getAttributeValue(null,'');
          //job.System_Used__c=reader.getAttributeValue(null,'');
          //job.Job_OLB_Status__c=reader.getAttributeValue(null,'ows_Status');
          //job.Account__c=reader.getAttributeValue(null,'');
          //job.Currency_to_be_Billed__c=reader.getAttributeValue(null,'');                
        }
        String strFirstName=null;
        String strLastName=null;
        if(reader.geteventtype()==Xmltag.START_ELEMENT && reader.getLocalName()=='Candidates')
        {
          while(reader.nexttag())
          {           
          if(reader.geteventtype()==Xmltag.END_ELEMENT)
          {
           break;
          }
          else if(reader.geteventtype()==Xmltag.START_ELEMENT && reader.getLocalName()=='Item')
          {                                
            strFirstName=reader.getAttributeValue(Null,'FirstName');                           
            strLastName=reader.getAttributeValue(Null,'LastName'); 
            candidates=strFirstName+strLastName;            
            candidates +=';'+candidates;
            System.Debug(candidates);     
          }
          }
          reader.next();
        }
        job.Name=candidates;
        if(reader.geteventtype()==Xmltag.START_ELEMENT && reader.getLocalName()=='ReportUsers')
        {          
          reader.nexttag();           
          if(reader.geteventtype()==Xmltag.START_ELEMENT && reader.getLocalName()=='Item')          
          {            
            //Report_User__c reportuser =new  Report_User__c();               
            string strfname= reader.getAttributeValue(Null,'FirstName');                                       
            string strlname=reader.getAttributeValue(Null,'LastName');             
            //reportuser.Email__c=reader.getAttributeValue(Null,'Email');            
            //reportUsers.add(reportuser);
            tests=strfname+strlname;          
          }        
        }
        reportuser.Name=tests;     
        reader.next();   
      }
      sp = Database.setSavepoint();
      insert job;
      reportuser.Account__c=job.Id;
      System.Debug(reportuser.Account__c);
      insert reportuser;
      Response=job.Id + '    '+job.Name +'   '+reportuser.Id +'   '+reportuser.Name;
      System.Debug(Response);
     }
    catch(Exception ex)
    {
      Database.rollback(sp);
    }        
    return Response;       
  }

 Please help me solve the issue.

 

 

blombardisblombardis

Hi Giribabu,

The exception thrown has to do with that field level-security.

You have to access through your org to that objects field properties

 

"9. Go to Setup > Customize, select a tab, click Fields, select a field, and click View FieldAccessibility to check the field accessibility to verify that all field access settings are correct."

 

https://na1.salesforce.com/help/doc/en/salesforce_pagelayouts_cheatsheet.pdf

There you can find some more information.

 

Hope this helps,

Bruno