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
jvandevejvandeve 

Newly created recordid not passed to URL? Help

Hi guys,

I've got a question. I've got a Visualforce page and controller that have to create or a Business Account and Contact  OR a PersonAccount from a custom object populated with fields of that custom object.

It does create the correct records with correct recordtypes, but the redirect after it should just go to the newly created account, but it says Null and an error message is displayed that the URL no longer exists.

Weirdly enough in sidebar I can see the newly created records and when I click on them I get their record detailpage. Weird huh.

Can someone tell me what I'm doing wrong or any advise to better do the code? I have to tell I'm not a spectacular coder ;-)

The VF Page:
<apex:page controller="createAccountOrContactController">
<apex:form id="theForm" >
   
   <apex:pageBlock title="Creating a Business Account and related Contact" rendered="{!createBA}">
      <apex:pageBlockButtons >
          <apex:commandButton action="{!confirmBA}" value="Confirm"/>
          <apex:commandButton action="{!cancel}" value="Cancel"/>
      </apex:pageBlockButtons>
      <apex:pagemessages ></apex:pagemessages>
      <apex:pageBlockSection title="Business Account Details" columns="2">
          <apex:outputField value="{!acct.Name}"/>
          <apex:outputField value="{!acct.C_Street__c}"/>
          <apex:outputField value="{!acct.C_City__c}"/>
          <apex:outputField value="{!acct.C_Zipcode__c}"/>
          <apex:outputField value="{!acct.C_Country__c}"/>
          <apex:outputField value="{!acct.Ondernemingsnummer__c}" />
          <apex:outputField value="{!acct.BTW_Plichtig__c}" />
          <apex:outputField value="{!acct.VAT__c}" />
          <apex:outputField value="{!acct.Phone}" />
          <apex:outputField value="{!acct.Email__c}" />
      </apex:pageBlockSection>
      <apex:pageBlockSection title="Contact Details" columns="2">
          <apex:outputField value="{!con.LastName}"/>
          <apex:outputField value="{!con.FirstName}"/>
          <apex:outputField value="{!con.Street__c}"/>
          <apex:outputField value="{!con.C_City__c}"/>
          <apex:outputField value="{!con.C_Zipcode__c}"/>
          <apex:outputField value="{!con.C_Country__c}"/>
          <apex:outputField value="{!con.Phone}"/>
          <apex:outputField value="{!con.MobilePhone}"/>
          <apex:outputField value="{!con.Email}"/>
          <apex:outputField value="{!con.HasOptedOutOfEmail}"/>
      </apex:pageBlockSection>
  </apex:pageBlock>
  
  <apex:pageBlock title="Creating a Person Account" rendered="{!createPA}">
      <apex:pageBlockButtons >
          <apex:commandButton action="{!confirmPA}" value="Confirm"/>
          <apex:commandButton action="{!cancel}" value="Cancel"/>
      </apex:pageBlockButtons>
      <apex:pagemessages ></apex:pagemessages>
      <apex:pageBlockSection title="Person Account Details" columns="2">
          <apex:outputField value="{!pacct.Street__pc}"/>
          <apex:outputField value="{!pacct.C_City__pc}"/>
          <apex:outputField value="{!pacct.C_Zipcode__pc}"/>
          <apex:outputField value="{!pacct.C_Country__pc}"/>
          <apex:outputField value="{!pacct.Email__c}"/>
          <apex:outputField value="{!pacct.LastName2__c}"/>
          <apex:outputField value="{!pacct.FirstName__c}"/>
          <apex:outputField value="{!pacct.Language__c}"/>
          <apex:outputField value="{!pacct.Phone}"/>
          <apex:outputField value="{!pacct.MobilePhone__c}"/>
          <apex:outputField value="{!pacct.PersonEmail}"/>
      </apex:pageBlockSection>
  </apex:pageBlock>
  
</apex:form>  
</apex:page>
The Controller:
public class createAccountOrContactController {
    public Id idStr = ApexPages.currentPage().getParameters().get('addressId');
    public Address__c theAddress = [Select Id, Name, Name__c, First_Name__c, Last_Name__c,Language_Code__c, Data_Privacy__c, VIP__c, Private_E_Mail__c, Mail_Stop__c, Mobile_Phone_No__c,Home_Phone_No__c, Address__c, City__c, Phone_No__c, E_Mail__c, Vat__c, BTW_Plichtig__c, Enterprise_No__c, Post_Code__c, Postcode__c, Country__c, Customer_Type__c from Address__c where Id = :idStr LIMIT 1];
    public User currentuser = [Select Id, Name, Email, Profile.Name from User where Id =:userinfo.getuserId()]; 
    //public String recordType {get; set;}
    public Boolean createBA {get; set;}
    public Boolean createPA {get; set;}

    
    public createAccountOrContactController(){
        if(theAddress.Customer_Type__c == 'Business'){
            createBA = true;
            createPA = false;
        }else{
            createPA = true;
            createBA = false;
        }
    }
    

    
    public String acctRecordType{
    get {
        if(currentuser.Profile.Name == 'Sales Rep Vriesdonk' && theAddress.Customer_Type__c == 'Business'){
            acctRecordType = '012250000004Rkg';
        }else if(currentuser.Profile.Name == 'Sales Rep Swinnen' && theAddress.Customer_Type__c == 'Business'){
            acctRecordType = '012250000004RkC';
        }else if(currentuser.Profile.Name == 'Sales Rep Euromotors' && theAddress.Customer_Type__c == 'Business'){
            acctRecordType = '012250000004Rkb';
        }else if(currentuser.Profile.Name == 'Sales Rep Joosen' && theAddress.Customer_Type__c == 'Business'){
            acctRecordType = '012250000004Rkl';
        }else if(currentuser.Profile.Name == 'Sales Rep Vriesdonk' && theAddress.Customer_Type__c != 'Business'){
            acctRecordType = '012250000004RkR';
        }else if(currentuser.Profile.Name == 'Sales Rep Swinnen' && theAddress.Customer_Type__c != 'Business'){
            acctRecordType = '012250000004RkH';
        }else if(currentuser.Profile.Name == 'Sales Rep Euromotors' && theAddress.Customer_Type__c != 'Business'){
            acctRecordType = '012250000004RkW';
        }else if(currentuser.Profile.Name == 'Sales Rep Joosen' && theAddress.Customer_Type__c != 'Business'){
            acctRecordType = '012250000004RkM';
        }else if(theAddress.Customer_Type__c == 'Business'){
            acctRecordType = '01224000000bQJa';
        }else{
            acctRecordType = '01224000000bQsc';
        }
        return acctRecordType;
    }
    set;
    }
    
    public Account acct{
    get {
        acct = new Account();
        acct.Name = theAddress.Name__c;
        acct.RecordTypeId = acctRecordType;
        acct.C_Street__c = theAddress.Address__c;
        acct.C_City__c = theAddress.City__c;
        acct.C_Zipcode__c = theAddress.Post_Code__c;
        acct.C_Country__c = theAddress.Country__c;
        acct.Address__c = theAddress.Id;
        acct.Ondernemingsnummer__c = theAddress.Enterprise_No__c;
        acct.Vat__c = theAddress.Vat__c;
        acct.BTW_Plichtig__c = theAddress.BTW_Plichtig__c;
        acct.Phone = theAddress.Phone_No__c;
        acct.Email__c = theAddress.E_Mail__c;
        acct.LastName2__c = theAddress.Last_Name__c;
        acct.FirstName__c = theAddress.First_Name__c;
        acct.Language__c = theAddress.Language_Code__c;
        acct.MobilePhone__c = theAddress.Mobile_Phone_No__c;
       return acct;
        
    }
    set;
    }
    
    public Account pacct{
    get {
        pacct = new Account();
        pacct.RecordTypeId = acctRecordType;
        pacct.C_Street__c = theAddress.Address__c;
        pacct.C_City__c = theAddress.City__c;
        pacct.C_Zipcode__c = theAddress.Post_Code__c;
        pacct.C_Country__c = theAddress.Country__c;
        pacct.Street__pc = theAddress.Address__c;
        pacct.C_City__pc = theAddress.City__c;
        pacct.C_Zipcode__pc = theAddress.Post_Code__c;
        pacct.C_Country__pc = theAddress.Country__c;
        pacct.Postcode_lk__pc = theAddress.Postcode__c;
        pacct.Address__c = theAddress.Id;
        pacct.Phone = theAddress.Phone_No__c;
        pacct.Email__c = theAddress.E_Mail__c;
        pacct.LastName = theAddress.Last_Name__c;
        pacct.LastName2__c = theAddress.Last_Name__c;
        pacct.FirstName = theAddress.First_Name__c;
        pacct.FirstName__c = theAddress.First_Name__c;
        pacct.Language__c = theAddress.Language_Code__c;
        pacct.MobilePhone__c = theAddress.Mobile_Phone_No__c;
        pacct.PersonEmail = theAddress.Private_E_mail__c;
        return pacct;
        
    }
    set;
    }
    
    public Contact con{
    get {
        if (con == null)
        con = new Contact();
        con.LastName = theAddress.Last_Name__c;
        con.FirstName = theAddress.First_Name__c;
        con.Street__c = theAddress.Address__c;
        con.C_City__c = theAddress.City__c;
        con.C_Zipcode__c = theAddress.Post_Code__c;
        con.C_Country__c = theAddress.Country__c;
        con.Address_Contact__c = theAddress.Id;
        con.Language__c = theAddress.Language_Code__c;
        con.VIP__c = theAddress.VIP__c;
        con.DataPrivacy__c = theAddress.Data_Privacy__c;
        con.Phone = theAddress.Home_Phone_No__c;
        con.MobilePhone = theAddress.Mobile_Phone_No__c;
        con.Email = theAddress.Private_E_Mail__c;
        con.HasOptedOutOfEmail = theAddress.Mail_Stop__c;
       
      return con;
    }
    set;
    }
    
    public PageReference confirmBA(){
        insert acct;
        con.AccountId = acct.Id;
        insert con;
        
        PageReference success = new PageReference('/' + acct.Id);
        success.setRedirect(true);
        return success;
        
        //return new PageReference('/' + acct.Id);
    
    }
    
    public PageReference confirmPA(){
        insert pacct;
        
        PageReference success = new PageReference('/' + pacct.Id);
        success.setRedirect(true);
        return success;
        
        //return new PageReference('/' + pacct.id);
    
    }
    
    
    public PageReference cancel(){
    
        return new PageReference('/' + theAddress.Id);
    }
    
}

User-added image

When I hit confirm whether is does the createPA or createBA method, I get the following error for URL, but you can see the records are created.
User-added image

You can see in URL after slash / there is written null, but why? Oh why?!
jvandevejvandeve
Thanks for fast reply.

That was what I tried first and why it's commented out in my code. Doesn't work either
Amit Chaudhary 8Amit Chaudhary 8
Its should work. Please try to debug you code. check any exeception is comming or not