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
Sudhir_MeruSudhir_Meru 

How pass AccountId to Controller from Asset

Hi, 

  I added a button on Asset tab which will call visualforce page. Inside visualforce page i am calling a apex controller which has a SOQL query based on AccountId

  Assets are associted to Account. I am using below method to pass AccountId from Asset Page but its not working. Please suggest me how to change. 

public class renewal_Asset
{
    public renewal_Asset(ApexPages.StandardController Ast ) {
     this();
    }

  
    public List<Asset> Ast {get;set;}
    public renewal_Asset()
    {
    String AstId = ApexPages.currentPage().getParameters().get('AccountId');
   
        Ast = [SELECT Name,SerialNumber,Product2Id,SLA_End_Date__c,AccountId
               FROM Asset
               WHERE Id = :AstId ];
    }   
   
  
}

Thanks

Sudhir

Best Answer chosen by Sudhir_Meru
Pablo_RoldanPablo_Roldan
And forget the following line about the previous code:
String AstId = ApexPages.currentPage().getParameters().get('AccountId');

If that works, please don't forget click on 'Best answer' to know that is solved.

Thanks,
Pablo

All Answers

Pablo_RoldanPablo_Roldan
Try this:
public class renewal_Asset
{
    public ApexPages.StandardController Ast { get; set; }
    public renewal_Asset(ApexPages.StandardController Ast ) {
     this.Ast = Ast;
    }

 
    public List<Asset> listAst {get;set;}
    public renewal_Asset()
    {
    String AstId = ApexPages.currentPage().getParameters().get('AccountId');
  
        listAst= [SELECT Name,SerialNumber,Product2Id,SLA_End_Date__c,AccountId
               FROM Asset
               WHERE Id = :Ast.getId()];
    }  
  
 
}
Pablo_RoldanPablo_Roldan
And forget the following line about the previous code:
String AstId = ApexPages.currentPage().getParameters().get('AccountId');

If that works, please don't forget click on 'Best answer' to know that is solved.

Thanks,
Pablo
This was selected as the best answer
Sudhir_MeruSudhir_Meru
Hi Pablo, 

 I tried below code to fix its working Please check and let me know. 

public class renewal_Asset
{
    public renewal_Asset(ApexPages.StandardController Ast ) {
     this();
    }
  
    public List<Asset> Ast {get;set;}
    public renewal_Asset()
    {
    String AstId = ApexPages.currentPage().getParameters().get('Id');
    Asset A;
   
    A = [SELECT AccountId FROM Asset WHERE ID = :AstId Limit 1];
    
        Ast = [SELECT Name,SerialNumber,Product2Id,SLA_End_Date__c,AccountId
               FROM Asset
               WHERE AccountId = :A.AccountId ];
    }   
   
  
}



Pablo_RoldanPablo_Roldan
That code should work, but I have written some improvements to avoid errors.
public class renewal_Asset
{
    public renewal_Asset(ApexPages.StandardController Ast ) {
     this();
    }
 
    public List<Asset> lstAst {get;set;}
    public renewal_Asset()
    {
    String AstId = ApexPages.currentPage().getParameters().get('Id');
    Asset A;
  
    A = [SELECT AccountId FROM Asset WHERE ID = :AstId Limit 1];
  if(A != null){
   Ast = [SELECT Name,SerialNumber,Product2Id,SLA_End_Date__c,AccountId
       FROM Asset
       WHERE AccountId = :A.AccountId ];
   }
    }  
  
 
}