• dboonepes
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 5
    Replies

I would like to be able to edit specific fields in the Assets records related to an Account. Below you will find the visualforce page I wrote to do this. When I click the save button, it doesn't actually save the values I have put in the input Fields. What am I doing wrong here?

 

<apex:page standardController="Account">
    <apex:Form >
        <apex:pageBlock title="Check in Stations for {!Account.Nickname__c}">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!account.assets}" var="item">
                <apex:column value="{!item.Name}"/>
                <apex:column headerValue="Serial Number">
                    <apex:inputField value="{!item.SerialNumber}"/>
                </apex:column>
                <apex:column headerValue="Ethernet MAC Address">
                    <apex:inputField value="{!item.Ethernet_MAC_Address__c}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:Form>
</apex:page>

 

I have created a join/junction object to allow many-to-many relationships between cases and assets. I am currently trying to develop a simplified UI to add assets to cases. The basic idea is that one would click on a button on the case layout and be presented a page with all of the assets related to the account related to the case.  The user will check next to all of the assets to be added to the case and then click a button add them to the case. I have just started writing the code for it and have hit a road block. 

 

I have created a wrapper class.

 

public class accountasset {

    /* Properties of the class */
    public ID assetID { get; private set; }
    public boolean selected   { get; private set; }
    public string name   { get; private set; }
    public string room { get; private set; }
    
    /* Class constructor */
    public AccountAsset(ID i, boolean s, String n, String r) {
        assetID   = i;
        selected = s;
        name    = n;
        room   = r;
    }
    
}

  And the class directly accessed by the VF page

public class AddAssetsToCase {
    
    /* Property value that holds the current Case */
    public Case myCase { get; set; }

    /* Property value that holds the current Account */
    public Account myAccount  { get; set; }

    /* Constructor of the class where we default the above property values */
    public AddAssetsToCase(){
        mycase = [SELECT id, description, subject, accountid FROM Case WHERE id = :ApexPages.currentPage().getParameters().get('id')];
        
        myaccount = [SELECT id, name,
            (SELECT Id, Name, Room__c          FROM Assets) 
        FROM account 
        WHERE id = :mycase.accountid];    
    }   
    
    /* Action method for navigating the user back to the case page. */
    public PageReference backToCase() {
        return new ApexPages.StandardController(mycase).view();
    }

    /* Accessor for retrieving the case object.*/
    public Case getCase() { 
        return mycase; 
    }
    
    /* Accessor for retrieving the case object and its related items.*/
    public Account getAccount() { 
        return myaccount; 
    }
    
    /* This accessor provides the page with the ordered collection of history (apex) objects for display in the page. 
       it also processes the truncation of case comments as specified by the fullComments property value.*/
    public AccountAsset[] getAccountAssets() {
        AccountAsset[] accountassets = new accountasset[]{};
        for (Asset aa:a.assets) {
        accountasset naa = new accountasset(aa.id,False,aa.name,aa.room__c);
                accountassets.add(naa); 
        }
        
        return accountassets;
    }
    

    /* The case object set by the getCase method and used by the getHistories method to acquire
       the related records.  */
    private Account a { 
        get { return getAccount(); }
        set; 
    }
     
}

 and finally a VF page

<apex:page controller="AddAssetsToCase">
    <apex:sectionHeader title="Add Assets to Case" subtitle="Case: {!Case.subject} for Account: {!Account.Name}"/>
    <apex:pageBlock >
       <apex:Form >
            <apex:PageBlockTable value="{!accountassets}" var="a">
                <apex:column headerValue="Select?">
                    <apex:inputCheckbox value="{!a.selected}"/>
                </apex:column>
                <apex:Column value="{!a.name}" headerValue="Name"/>
                <apex:Column value="{!a.room}" headerValue="Room"/>
            </apex:PageBlockTable>
        </apex:Form>
    </apex:pageBlock>
</apex:page>

 When I try and save the VF page as above I get the following error.

Error: Read only property 'accountasset.selected'

 

What am I doing wrong here?

I have created a join object for Cases and Asset for a many-to-many relationship between these objects. I would like to create a process for adding assets to cases that would be simpler and easier than the default in SF. The user exerience I have in mind is this... Click on button on the Asset related list in the Case layout. A window pops up listing the assets related to the account related to the case. The user check boxes next to the appropriate assets, clicks a button, the appropriate case asset join records are created and the window closes.

 

I am struggling with how to create the checkboxes next to the list of assets and how to pass that information to the Apex class I will need to build. Any suggestions or thoughts?

I am really new to Apex and would like some direction in testing a class. Could somebody give me some pointers? I have read through the documentation and am having a mental block. The class is below.

public class StageStations {

    Integer numberOfAssets;
    Account myaccount {get; private set;}
    Asset myasset;
    Decimal hsid;
    
    
    public StageStations(){ 
        myaccount = [select id, name, Contracted_Stations__c from Account 
        where id = :ApexPages.currentPage().getParameters().get('id')];
        for( Asset a : [Select Station_ID__c from Asset Where AccountID = :ApexPages.currentPage().getParameters().get('id')]) hsid = ( hsid > a.Station_ID__c ? hsid : a.Station_ID__c);
    }
    
    public Integer getNumberOfAssets(){
     Integer noa = [Select Count() from Asset Where AccountID = :ApexPages.currentPage().getParameters().get('id')];
     return noa;
    }
    
    public Account getAccount() {
        return myaccount;
    } 
    
    public Asset getAsset() {
        if(myasset == null) myasset = new asset();
        myasset.Station_ID__c = hsid+1;
        return myasset;
    }    

    public PageReference cancel() {
        pageReference accountpage = new ApexPages.StandardController(myaccount).view();
        accountpage.setRedirect(true);
        return accountpage; 
    }


    public PageReference save() {
        myasset.AccountID = myaccount.id;
        myasset.Name = 'PES Station '+myasset.Station_ID__c;
        insert myasset;
        
        if (myaccount.Contracted_stations__c==[Select Count() from Asset Where AccountID = :ApexPages.currentPage().getParameters().get('id')]){
             PageReference acctPage = new ApexPages.StandardController(myaccount).view();
             acctPage.setRedirect(true);
             return acctPage;
        } else { 
             PageReference newAssetPage = new PageReference(ApexPages.currentPage().getURL());
             newAssetPage.setRedirect(true);
             return newAssetPage;
        }
    }

}

 

I keep getting the following error message when I try to insert a new record

 

System.NullPointerException: Attempt to de-reference a null object 

 

Class.StageStations.save: line 27, column 1

 

Here is the visualforce code

 

<apex:page controller="StageStations">
 <script>
  function confirmCancel() {
      var isCancel = confirm("Are you sure you wish to cancel?");
      if (isCancel) return true;
  
     return false;
  }  
  </script>
  <apex:sectionHeader title="Add Station for {!account.name}"/>
    <apex:form >
      <apex:pageBlock title="Customer Information" mode="edit">    
        <apex:pageBlockButtons >
          <apex:commandButton action="{!save}" value="Save"/>
          <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true"/>
        </apex:pageBlockButtons>
      <apex:pageBlockSection title="Asset Information for {!asset.id}">

        <!-- Within a pageBlockSection, inputFields always display with their
             corresponding output label. --> 
    
        <apex:inputField id="assetid" value="{!asset.Station_ID__c}"/>
        <apex:inputField id="assetsn" value="{!asset.SerialNumber}"/>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

 Here is the Apex Class

public class StageStations {


    Account account;
    Asset asset;
    
    public Account getAccount() {
        return [select id, name,Contracted_Stations__c from Account 
                 where id = :ApexPages.currentPage().getParameters().get('id')]; 
    } 
    
    public Asset getAsset() {
      if(asset == null) asset = new asset();
      return asset;
   }


    public PageReference cancel() {
        pageReference accountpage = new ApexPages.StandardController(account).view();
        accountpage.setRedirect(true);
        return accountpage; 
    }


    public PageReference save() {
    
      asset.AccountID = account.id;
      asset.Name = 'PES Station';
      insert asset;
        
        
      PageReference acctPage = new ApexPages.StandardController(account).view();
      acctPage.setRedirect(true);

      return acctPage;
    }
}

 I am pretty much a neophyte to Apex coding and I based this code on tutorials I found on the force.com site. Any insight would be appreciated.


I would like to be able to edit specific fields in the Assets records related to an Account. Below you will find the visualforce page I wrote to do this. When I click the save button, it doesn't actually save the values I have put in the input Fields. What am I doing wrong here?

 

<apex:page standardController="Account">
    <apex:Form >
        <apex:pageBlock title="Check in Stations for {!Account.Nickname__c}">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!account.assets}" var="item">
                <apex:column value="{!item.Name}"/>
                <apex:column headerValue="Serial Number">
                    <apex:inputField value="{!item.SerialNumber}"/>
                </apex:column>
                <apex:column headerValue="Ethernet MAC Address">
                    <apex:inputField value="{!item.Ethernet_MAC_Address__c}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:Form>
</apex:page>

 

I have created a join/junction object to allow many-to-many relationships between cases and assets. I am currently trying to develop a simplified UI to add assets to cases. The basic idea is that one would click on a button on the case layout and be presented a page with all of the assets related to the account related to the case.  The user will check next to all of the assets to be added to the case and then click a button add them to the case. I have just started writing the code for it and have hit a road block. 

 

I have created a wrapper class.

 

public class accountasset {

    /* Properties of the class */
    public ID assetID { get; private set; }
    public boolean selected   { get; private set; }
    public string name   { get; private set; }
    public string room { get; private set; }
    
    /* Class constructor */
    public AccountAsset(ID i, boolean s, String n, String r) {
        assetID   = i;
        selected = s;
        name    = n;
        room   = r;
    }
    
}

  And the class directly accessed by the VF page

public class AddAssetsToCase {
    
    /* Property value that holds the current Case */
    public Case myCase { get; set; }

    /* Property value that holds the current Account */
    public Account myAccount  { get; set; }

    /* Constructor of the class where we default the above property values */
    public AddAssetsToCase(){
        mycase = [SELECT id, description, subject, accountid FROM Case WHERE id = :ApexPages.currentPage().getParameters().get('id')];
        
        myaccount = [SELECT id, name,
            (SELECT Id, Name, Room__c          FROM Assets) 
        FROM account 
        WHERE id = :mycase.accountid];    
    }   
    
    /* Action method for navigating the user back to the case page. */
    public PageReference backToCase() {
        return new ApexPages.StandardController(mycase).view();
    }

    /* Accessor for retrieving the case object.*/
    public Case getCase() { 
        return mycase; 
    }
    
    /* Accessor for retrieving the case object and its related items.*/
    public Account getAccount() { 
        return myaccount; 
    }
    
    /* This accessor provides the page with the ordered collection of history (apex) objects for display in the page. 
       it also processes the truncation of case comments as specified by the fullComments property value.*/
    public AccountAsset[] getAccountAssets() {
        AccountAsset[] accountassets = new accountasset[]{};
        for (Asset aa:a.assets) {
        accountasset naa = new accountasset(aa.id,False,aa.name,aa.room__c);
                accountassets.add(naa); 
        }
        
        return accountassets;
    }
    

    /* The case object set by the getCase method and used by the getHistories method to acquire
       the related records.  */
    private Account a { 
        get { return getAccount(); }
        set; 
    }
     
}

 and finally a VF page

<apex:page controller="AddAssetsToCase">
    <apex:sectionHeader title="Add Assets to Case" subtitle="Case: {!Case.subject} for Account: {!Account.Name}"/>
    <apex:pageBlock >
       <apex:Form >
            <apex:PageBlockTable value="{!accountassets}" var="a">
                <apex:column headerValue="Select?">
                    <apex:inputCheckbox value="{!a.selected}"/>
                </apex:column>
                <apex:Column value="{!a.name}" headerValue="Name"/>
                <apex:Column value="{!a.room}" headerValue="Room"/>
            </apex:PageBlockTable>
        </apex:Form>
    </apex:pageBlock>
</apex:page>

 When I try and save the VF page as above I get the following error.

Error: Read only property 'accountasset.selected'

 

What am I doing wrong here?

I am really new to Apex and would like some direction in testing a class. Could somebody give me some pointers? I have read through the documentation and am having a mental block. The class is below.

public class StageStations {

    Integer numberOfAssets;
    Account myaccount {get; private set;}
    Asset myasset;
    Decimal hsid;
    
    
    public StageStations(){ 
        myaccount = [select id, name, Contracted_Stations__c from Account 
        where id = :ApexPages.currentPage().getParameters().get('id')];
        for( Asset a : [Select Station_ID__c from Asset Where AccountID = :ApexPages.currentPage().getParameters().get('id')]) hsid = ( hsid > a.Station_ID__c ? hsid : a.Station_ID__c);
    }
    
    public Integer getNumberOfAssets(){
     Integer noa = [Select Count() from Asset Where AccountID = :ApexPages.currentPage().getParameters().get('id')];
     return noa;
    }
    
    public Account getAccount() {
        return myaccount;
    } 
    
    public Asset getAsset() {
        if(myasset == null) myasset = new asset();
        myasset.Station_ID__c = hsid+1;
        return myasset;
    }    

    public PageReference cancel() {
        pageReference accountpage = new ApexPages.StandardController(myaccount).view();
        accountpage.setRedirect(true);
        return accountpage; 
    }


    public PageReference save() {
        myasset.AccountID = myaccount.id;
        myasset.Name = 'PES Station '+myasset.Station_ID__c;
        insert myasset;
        
        if (myaccount.Contracted_stations__c==[Select Count() from Asset Where AccountID = :ApexPages.currentPage().getParameters().get('id')]){
             PageReference acctPage = new ApexPages.StandardController(myaccount).view();
             acctPage.setRedirect(true);
             return acctPage;
        } else { 
             PageReference newAssetPage = new PageReference(ApexPages.currentPage().getURL());
             newAssetPage.setRedirect(true);
             return newAssetPage;
        }
    }

}

 

I keep getting the following error message when I try to insert a new record

 

System.NullPointerException: Attempt to de-reference a null object 

 

Class.StageStations.save: line 27, column 1

 

Here is the visualforce code

 

<apex:page controller="StageStations">
 <script>
  function confirmCancel() {
      var isCancel = confirm("Are you sure you wish to cancel?");
      if (isCancel) return true;
  
     return false;
  }  
  </script>
  <apex:sectionHeader title="Add Station for {!account.name}"/>
    <apex:form >
      <apex:pageBlock title="Customer Information" mode="edit">    
        <apex:pageBlockButtons >
          <apex:commandButton action="{!save}" value="Save"/>
          <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true"/>
        </apex:pageBlockButtons>
      <apex:pageBlockSection title="Asset Information for {!asset.id}">

        <!-- Within a pageBlockSection, inputFields always display with their
             corresponding output label. --> 
    
        <apex:inputField id="assetid" value="{!asset.Station_ID__c}"/>
        <apex:inputField id="assetsn" value="{!asset.SerialNumber}"/>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

 Here is the Apex Class

public class StageStations {


    Account account;
    Asset asset;
    
    public Account getAccount() {
        return [select id, name,Contracted_Stations__c from Account 
                 where id = :ApexPages.currentPage().getParameters().get('id')]; 
    } 
    
    public Asset getAsset() {
      if(asset == null) asset = new asset();
      return asset;
   }


    public PageReference cancel() {
        pageReference accountpage = new ApexPages.StandardController(account).view();
        accountpage.setRedirect(true);
        return accountpage; 
    }


    public PageReference save() {
    
      asset.AccountID = account.id;
      asset.Name = 'PES Station';
      insert asset;
        
        
      PageReference acctPage = new ApexPages.StandardController(account).view();
      acctPage.setRedirect(true);

      return acctPage;
    }
}

 I am pretty much a neophyte to Apex coding and I based this code on tutorials I found on the force.com site. Any insight would be appreciated.


Given the Start Date and No. of Business days  find ending date, if you pass a negative amount of days then should get Current Date less those business days,

 

Already test it and currently using it, feel free to correct or improve it for sharing

 

    public static Boolean IsWeekendDay(Datetime dateParam)
    {
       boolean result     = false;
        
       //Recover the day of the week
       Date startOfWeek   = dateParam.date().toStartOfWeek();
       Integer dayOfWeek  = dateParam.day() - startOfWeek.day();
           
       result = dayOfWeek == 0 || dayOfWeek == 6 ? true : false;
        
       return result;
    } 
    
    
    public static Datetime AddBusinessDays(Datetime StartDate, integer BusinessDaysToAdd )
    {
       //Add or decrease in BusinessDaysToAdd days 
       Datetime finalDate = StartDate;
       
       integer direction = BusinessDaysToAdd < 0 ? -1 : 1;

        while(BusinessDaysToAdd != 0)
        {
            finalDate = finalDate.AddDays(direction);            
            
            if (!isWeekendDay(finalDate))
            {
                BusinessDaysToAdd -= direction;
            }
        }

        return finalDate;
    }