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
Cris9931Cris9931 

how to get dynamically the id of an Account when I create a record?

Hi! I want to create a class that will fetch the ID of an Account dynamic when I create a new record. We have a custom object named Account Change request. This object can be found in the related lists from Account. When user press to create a new Account Change Request I want to override it with my visualforce page, thus I would need to display the informations on my VF page from that Account. I tried to create by myself but it's not working..
 
public class MasterDataChange{


 public Account parentAccount{get; set;}
 public Account_Change_Request__c accChangeRequest;


 public MasterDataChange(ApexPages.StandardController controller){
    
  String parentAccId = ApexPages.currentPage().getParameters().get('id');
     if(parentAccId != null){
            parentAccount= [SELECT Id, Name FROM Account WHERE Id = :parentAccId limit 1];
                accChangeRequest.Account__c = parentAccount.Id;
            }
         
        } 

  
 public PageReference save(){
   insert accountChangeRequest;
   
    PageReference pg = new PageReference('/'+parentAccount.Id);
          pg.setRedirect(true);
         return pg;
 }
}

Account__c is my master-detail with the Account object.. please help.
Ashish Singh SFDCAshish Singh SFDC
Hello Sarah,

May be you can use retURL instead of ID.

For an example you write something like this in constructor:
 
public AccountChangeRequest(ApexPages.StandardController controller){
        parentAccId = ApexPages.currentPage().getParameters().get('retURL');
        parentAccId=parentAccId.removeStart('/');
        acc = [Select Id,name from Account where id=:parentAccId];
    }

Thanks,
Ashish Singh.
Cris9931Cris9931

Uff.. it's not working...

I think my code is not good at all.. I mean my approach.

Ashish Singh SFDCAshish Singh SFDC
What is the URL when you click on New button ?
Cris9931Cris9931
I have an error...

User-added image

my class:
public class myControllerExtension {

    public Account parentid{get;set;}
    
    
   
    public myControllerExtension(ApexPages.StandardController controller){
       String parentAccId = ApexPages.currentPage().getParameters().get('retURL');
        parentAccId=parentAccId.removeStart('/');
        parentid= [Select Id,name from Account where id=:parentAccId];
    }
    
  

}


my vf page:

 

<apex:page standardController="Account" extensions="myControllerExtension">
    
    <apex:form >
        <apex:inputField value="{!account.name}"/> <p/>
        <apex:commandButton value="Save" action="{!save}"/>
    </apex:form>
</apex:page>
Ashish Singh SFDCAshish Singh SFDC
This means you're clicking on preview button. First override your new button from Custom object and select this VF page as new action. Then open any account and go to new button of Account Change request. Only then URL will generate which will have id retURL.

For example it should have a URL created like this once you click on new button:

https://c.ap17.visual.force.com/apex/AccountChangeRequest?CF00N2x000000j7pQ=Dickenson+plc&CF00N2x000000j7pQ_lkid=0012x000002RLD4&scontrolCaching=1&retURL=%2F0012x000002RLD4&sfdc.override=1

here as you can see we have retURL so we can fetch it.

Thanks
Ashish Singh.
Cris9931Cris9931

Ok, I did exactly the same steps you mentioned. This is my url:

 

https://sig-cb--full--c.cs88.visual.force.com/apex/testtest1?CF00N5800000Chfhg=PT+SARANA+KARYA+UTAMA&CF00N5800000Chfhg_lkid=0015800000F42aZ&scontrolCaching=1&retURL=%2F0015800000F42aZ&RecordType=01258000000AiQu&ent=01I58000000umn8&save_new=1&sfdc.override=1

Cris9931Cris9931

it works, I have the ID now stored:

I just added this in my visualforce page

<apex:inputField value="{!parentid.id}"/> <p/>
 

and I can see the ID for the account

User-added image

Ashish Singh SFDCAshish Singh SFDC
Here what I have return for you:

Extension:
public class AccountChangeRequest {
    public Account acc {get;set;}
    public String parentAccId {get;set;}
    public AccountChangeRequest(ApexPages.StandardController controller){
        parentAccId = ApexPages.currentPage().getParameters().get('retURL');
        parentAccId=parentAccId.removeStart('/');
        acc = [Select Id,name from Account where id=:parentAccId];
    }
}

VF:
<apex:page standardController="Account_Change_Request__c" extensions="AccountChangeRequest">
    {!parentAccId}
    <apex:form>
        <apex:inputField  value="{!acc.name}"/>
	</apex:form>
</apex:page>

Thanks,
Ashish Singh.
Cris9931Cris9931

However, now my last step is to associate the new record - Account Change Request with the Account and display the fields(prepopulate the fields with the values from Account).... I still have this error:

List has no rows for assignment to SObject
An unexpected error has occurred. Your development organization has been notified.

I modified a bit my class and vf page:

public class MasterDataChange{

 public Account acct{get; set;}
 public String currentRecordId {get;set;}
 public Account_Change_Request__c accChangeRequest{get;set;}


 public MasterDataChange(ApexPages.StandardController controller){
       String parentAccId = ApexPages.currentPage().getParameters().get('retURL');
        parentAccId=parentAccId.removeStart('/');
        acct= [Select Id,name from Account where id=:parentAccId];
        
         accChangeRequest=  [ Select ID, RecordTypeId, Account__c, AccountOwner_new__c, AccountName_old__c, AccountName_new__c, AccountOwner_old__c,
                                     ParentAccount_old__c, ParentAccount_new__c, Nickname_new__c, Nickname_old__c
                                     from  Account_Change_Request__c where ID =: acct.id ];
    }
  
}


 
<apex:page standardController="Account_Change_Request__c" extensions="MasterDataChange">
    
    <apex:form >
        <apex:inputField value="{!acct.name}"/> <p/>
         <apex:inputField value="{!acct.id}"/> <p/>
         <apex:outputField value="{!accChangeRequest.Account__c}"/>
        <apex:commandButton value="Save" action="{!save}"/>
    </apex:form>
</apex:page>

Any idea why I still have that error message? I'm sure I'm clicking from an ACCOUNT and not from anywhere else.
Cris9931Cris9931

What I'm trying to do here is to display the fields in my visualforce page with the fields prepopulated.

Exactly like this:

 

User-added image

Ashish Singh SFDCAshish Singh SFDC
Yes, because you're trying to query accChangeRequest for acct.ID which is in retURL. That means from the account which you're opening this VF has no Change request yet created. Therefore its a null and won't display you any value.

And the other thing is where clause of query. Instead of Account__c, its ID.
Cris9931Cris9931

Oh..Do you know why I cant see my page on the list buttons?

User-added image

my vf page:

<apex:page standardController="Account_Change_Request__c" extensions="MasterDataChange">
    
  <apex:form >
                 
    <apex:pageBlock >
    
        <apex:pageBlockSection title="Account Change Request Information" columns="2" >
           <apex:outputField value="{!acct.name}"/> <p/>
            <apex:outputField value="{!accChangeRequest.Account__c}"/>
            <apex:outputField value="{!accChangeRequest.RecordTypeId}" />
          <p>  <apex:inputText value="{!accChangeRequest.AccountName_new__c}" /></p>
        </apex:pageBlockSection>
        
         
    </apex:pageBlock>
</apex:form>

</apex:page>
Ashish Singh SFDCAshish Singh SFDC
Yes because its a standardcontroller not a standardcontroller with recordsetVar attribute(standardsetcontroller or standardlistcontroller). In your case you're just using Detail page, you should go for that.

Just for your info when we try to create a list view then we user standardcontroller and recordsetvar, then it will be available in list option.
Cris9931Cris9931

ah, this is not good.. :( i would need to have the button on my related lists when I access an Account.

With Link and Button how can I display these buttons on my Account page if I create the buttons on my Account Change Request?

Cris9931Cris9931

take a look here:

User-added image

These buttons are only visible for LIST Buttons and not Detail Page Button and Detail Page Link....

Cris9931Cris9931

Okay... I managed to transform my button into a List and i added to related list on my Account.
 

However now when I press the button I encounter another error:

User-added image

Why I have this error?

Ashish Singh SFDCAshish Singh SFDC
You're trying to insert a record and have specified wrong recordtypeID. You ACR object is having recordtype and you need to specify recordtypeID while saving as its mandatory.
Cris9931Cris9931

so what changes do I have to make in my class?

I need to query the recordtypeid as well?

Ashish Singh SFDCAshish Singh SFDC
Are you trying to save a record in your controller? If yes then add either add a select list option in VF page by displaying recordtype name using schema programming, or else hardcode your record type if recordtype is not an issue.
Cris9931Cris9931

User-added image

These are my record types for the ACR. So when I press my button and visualforce shows up I want to always be 'Master Data Change'.

Cris9931Cris9931
public class MasterDataChange{

 public Account acct{get; set;}
 public String currentRecordId {get;set;}
 public Account_Change_Request__c accChangeRequest{get;set;}
 public RecordType recordTypeId{get;set;}

 public MasterDataChange(ApexPages.StandardSetController controller){
       String parentAccId = ApexPages.currentPage().getParameters().get('retURL');
        parentAccId=parentAccId.removeStart('/');
        acct= [Select Id,name, RecordTypeId from Account where id=:parentAccId];
        
        recordTypeId = [SELECT Id FROM RecordType WHERE DeveloperName = 'Master_Data_Change'];
        System.debug(recordTypeId);
    }
   
}
I managed to query the record type and now i store it in my recordTypeID variable. How can I set my ACR record type with this recordTypeID variable?
 
Cris9931Cris9931
or something like this?
 
public class MasterDataChange{

 public Account acct{get; set;}
 public String currentRecordId {get;set;}
 public Account_Change_Request__c accChangeRequest{get;set;}
 public RecordType recordTypeId{get;set;}

 public MasterDataChange(ApexPages.StandardSetController controller){
       String parentAccId = ApexPages.currentPage().getParameters().get('retURL');
        parentAccId=parentAccId.removeStart('/');
        acct= [Select Id,name, RecordTypeId from Account where id=:parentAccId];
        
        recordTypeId = [SELECT Id FROM RecordType WHERE DeveloperName = 'Master_Data_Change'];
        System.debug(recordTypeId);
        
        accChangeRequest.RecordTypeId = RecordTypeId.id;
        System.debug(accChangeRequest.RecordTypeId);
    }
   
}