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
LearnerrrLearnerrr 

Below code is correct to avoid hardcode Ids?? Here I need to create constructor ??

public class abc{
	private final Id AccountId; 
	
	 public abc(ApexPages.StandardController stdController) {
       Account acc = (Account)stdController.getRecord();
       accountId = acc.Id;
   }
	
	@AuraEnabled
	public static Account getAccount(){
		 Account acc = [select Name,Phone,Id from Account where Id= :accountId];
		System.debug(acc.Id);
		return acc;
	}
}

 
Nik shNik sh
Hi,
Seems correct,what error you are getting?
Deepali KulshresthaDeepali Kulshrestha
Hi Sweta,

Yes, the code is correct to avoid hardcoded id's. But there is no need to create another constructor.
Because you have already a constructor in your code:
 public abc(ApexPages.StandardController stdController) {
       Account acc = (Account)stdController.getRecord();
       accountId = acc.Id;
   }
If you want to initialize any code you can initialize in this constructor.   

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 
LearnerrrLearnerrr
Hi @Nik sh
Error is :Variable does not exist : accountId 
Deepali KulshresthaDeepali Kulshrestha
Hi Sweta,

If you are getting error Variable does not exist : accountId  make your variable to static to use in static method.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Nik shNik sh
Hi Sweta G,

Change your code from private final Id AccountId to
private  static Id AccountId;


Please mark as Best Answer to help others too.

Thank you

 
Raj VakatiRaj Vakati
This code may not wokr  .. you have to change the code as below 

When you invoke the Aura Contrilleri dnt think so you are doing to initialize the StandardController  constructor 

Below is the correct code 
 
public class abc{
	private static final Id AccountId; 
	
	 public abc(ApexPages.StandardController stdController) {
       Account acc = (Account)stdController.getRecord();
       accountId = acc.Id;
   }
	
	@AuraEnabled
	public static Account getAccount(String accountId){
		 Account acc = [select Name,Phone,Id from Account where Id= :accountId];
		System.debug(acc.Id);
		return acc;
	}
}