1

I'm trying to build a test class for a controller extension on a Visualforce page and I need a more in depth description of what certain lines mean. Help on this would be appreciated.

1)

 String msg=e.getMessage();
 integer pos;

 // if its field validation, this will be added to the messages by default
 if (-1==(pos=msg.indexOf('FIELD_CUSTOM_VALIDATION_EXCEPTION, ')))
 {
     ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, msg));
 }

 result=false;

2)

 pr.setRedirect(true);

3) The null here

public void newSkid()
{
   if (updateSkids())
   {
      Skid__c skid=new Skid__c(Name=newSkidName, Dimension__c=newSkidDimension, Number_of_Pieces__c=newSkidNumberofPieces, UOM__c=newskiduom, Weight__c=newskidweight, Bill_of_Lading__c=getBillofLading().id);
      insert skid;

      newSkidName=null;
      newSkidDimension=null;
      newSkidNumberofPieces=null;
      newskidbilloflading=null; 
      newskiduom=null;
      newskidweight=null;
      skids=null;

4) The null!= here

public void deleteSkid()
{
   if (updateSkids())
   {
      if (null!=chosenskidId)
      {
         Skid__c skid=new Skid__c(Id=chosenskidId);
          delete skid;

          skids=null;
          chosenskidId=null;
      }
   }
}

5) The null statement again here

public List<Skid__c> getSkids()
{
   if ( (null!=getBillofLading().id) && (skids == null) )
   {
       skids=[SELECT Id, Name, Bill_of_Lading__c,  
                    Dimension__c, Number_of_Pieces__c, UOM__c, Weight__c
                    FROM Skid__c 
                    WHERE Id =: getBillofLading().id
                    ORDER BY CreatedDate];

6) These functions

public String getChooserender() {
  if (ApexPages.currentPage().getParameters().get('p') != null)
    return 'pdf';
  else
    return null;
}

public PageReference deliverAsPDF() {
  PageReference pdf =  Page.Billofladingtest05222013;
  pdf.getParameters().put('p','p');
  return pdf;
}

1 Answer 1

5

When trying to understand some new programming concepts or the specifics of a new language or API, the documentation is invaluable. Here's a couple of resources for you.

Here are some details about the specific code snippets you had questions about.

1) Check out the Exception documentation for e.getMessage();, String Methods documentation for indexOf(), and ApexPages documentation.

 String msg=e.getMessage(); // get the error message
 integer pos;

 // if its field validation, this will be added to the messages by default
 if (-1==(pos=msg.indexOf('FIELD_CUSTOM_VALIDATION_EXCEPTION, ')))
 {
     ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, msg));
 }

 result=false;

2) PageReference documentation

 pr.setRedirect(true);

3) The null here is assigning "no value" (or a blank value) to all of those variables. I'm not entirely sure what that's doing. It could be resetting the values so that they can be used to insert another skid__c.

public void newSkid()
{
   if (updateSkids())
   {
      Skid__c skid=new Skid__c(Name=newSkidName, Dimension__c=newSkidDimension, Number_of_Pieces__c=newSkidNumberofPieces, UOM__c=newskiduom, Weight__c=newskidweight, Bill_of_Lading__c=getBillofLading().id);
      insert skid;

      newSkidName=null;
      newSkidDimension=null;
      newSkidNumberofPieces=null;
      newskidbilloflading=null; 
      newskiduom=null;
      newskidweight=null;
      skids=null;

4) The comparison with null!= here is checking to see if the chosenskidId has a value. It might be better understood as if(chosenskidId != null) which is an equivalent statement.

public void deleteSkid()
{
   if (updateSkids())
   {
      if (null!=chosenskidId)
      {
         Skid__c skid=new Skid__c(Id=chosenskidId);
          delete skid;

          skids=null;
          chosenskidId=null;
      }
   }
}

5) The null statement again here is basically saying: If the resulting sObject (or variable) Id from the function, getBillofLading(), has a value and skids is does not have a value, do the following statements between the brackets { and }.

public List<Skid__c> getSkids()
{
   if ( (null!=getBillofLading().id) && (skids == null) )
   {
       skids=[SELECT Id, Name, Bill_of_Lading__c,  
                    Dimension__c, Number_of_Pieces__c, UOM__c, Weight__c
                    FROM Skid__c 
                    WHERE Id =: getBillofLading().id
                    ORDER BY CreatedDate];

6) See the inline comments for these functions. Also, check out the ApexPages documentation and the PageReference documentation.

public String getChooserender() {
  // if the current page has a 'p' QueryString Parameter
  if (ApexPages.currentPage().getParameters().get('p') != null)
    return 'pdf'; // return 'pdf'
  else
    return null;
}

public PageReference deliverAsPDF() {
  // create a new PageReference to the Page named 'Billofladingtest05222013'
  PageReference pdf = Page.Billofladingtest05222013;
  // add the 'p' QueryString Parameter
  pdf.getParameters().put('p','p');
  // return the PageReference
  return pdf;
}
3
  • Nice answer, I thought I'd just add a comment... The part about what "null" is described quite well on wikipedia: en.wikipedia.org/wiki/Pointer_(computer_programming) I'd avoid the usage of the word "blank" when describing null values. Some people think of blank as "" or 0. Instead, it literally means "there is no value (it is void of meaning)," or more figuratively, "the value is unknown."
    – sfdcfox
    Commented Jul 16, 2013 at 20:00
  • Thx; updated removing the "blank" terminology.
    – Matt K
    Commented Jul 16, 2013 at 21:25
  • Thanks Matthew, will check on those. I'm just not sure with the usage of 'null' in those expressions. Commented Jul 17, 2013 at 2:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .