0

I think I am taking the long way around to getting my record. Can someone tell me if there is an easier way of doing this?

public Id empId {get; set;}
Private Employees__c oldEmployee {get;set;}

public class EmpMerge_Controller {
    empId = ApexPages.CurrentPage().getparameters().get('hbsId');
    oldEmployee = [Select Id,Name FROM Employees__c WHERE ID = :empId];
}
5
  • This is totally fine. You can't avoid using a query. – Brian Mansfield Jun 15 '16 at 19:47
  • @BrianMansfield Not true if StandardController is an option. The getRecord method does not consume a query (though I think it technically performs one and it just doesn't count). – Adrian Larson Jun 15 '16 at 20:06
  • He is clearly not using a standard controller, but yes you're right as usual – Brian Mansfield Jun 15 '16 at 20:07
  • Thanks guys! Happy to know that I'm not way off the mark or anything. To clarify, I am using a custom controller and I don't need to be concerned with checking for null on my param as I am launching from a page where this value is actually the Name field for the record, hence required. – Matt M Jun 15 '16 at 20:11
  • @MattM - NOW you may not need to but you never know what will happen in the future or how the link may be constructed / hit in the future. Best bet is to account for it now rather than when it causes a problem. If you always keep that in mind (no assumptions) when writing your code your code will live longer and cause you less frustration – Eric Jun 15 '16 at 23:58
1

Could be simplified to this, but without context cannot say if there is a better way to do it to meet your business needs

Private Employees__c oldEmployee {get;set;}

public class EmpMerge_Controller {
    Id empId = ApexPages.CurrentPage().getparameters().get('hbsId');
    if(!string.isBlank(empId))
        oldEmployee = [Select Id,Name FROM Employees__c WHERE ID = :empId];
}

Either way you will have to query and you should be checking for null or you will receive an error if the hbsid is not present

0

You can get the id from hyperlink and query directly,

Private Employees__c oldEmployee {get;set;}

public class EmpMerge_Controller {

  if(ApexPages.CurrentPage().getparameters().get('hbsId')!= null){        

        oldEmployee = [Select Id,Name FROM Employees__c WHERE ID = :ApexPages.CurrentPage().getparameters().get('hbsId')];
  }
}
3
  • Then how do you handle the null value in the param? Not a good pattern to just assume it is present and valid. – Eric Jun 15 '16 at 20:18
  • oops.yes you are right, there should be a null check which i forgot to add. – Anurag Jun 15 '16 at 20:29
  • You are now making two calls to get the param which is arguably no better that getting it the first time and storing it. In this case execution time is not really an issue. I guess the pattern here is up to the user – Eric Jun 15 '16 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.