2

I have a Visualforce page for creating a new record (custom object) from account. I also have a controller extension to pass the URL parameters and other values to pre-populate values on the vf page. Finally a custom button to launch the vf page with the URL parameters.

I am an admin, and this is only my second coding project.

I have everything working except for passing the Account.NumberOfEmployees to the custom object. Both fields are Number (8,0). Not sure why it doesn't like the number for some reason and I am stuck.

Here is the code for my vf page:

<apex:page standardController="MyCustomObject__c" extensions="MyCustomObjectExt">
<apex:form >
    <apex:pageBlock title="SST Project Detail" mode="edit">
        <apex:pageBlockSection title="Information">
            <apex:inputField value="{!MyCustomObject__c.Name}" required="true"/>
            <apex:inputField value="{!MyCustomObject__c.SalesRep__c}"/>
            <apex:inputField value="{!MyCustomObject__c.AccountName__c}"/>
            <apex:inputField value="{!MyCustomObject__c.Field1__c}"/>
            <apex:inputField value="{!MyCustomObject__c.Field2__c}"/>
            <apex:inputField value="{!MyCustomObject__c.AccountType__c}"/>
            <apex:inputField value="{!MyCustomObject__c.Employees__c}"/>
            <apex:outputField value="{!MyCustomObject__c.OwnerId}"/>
        </apex:pageBlockSection>
<apex:pageBlockButtons >
        <apex:commandButton action="{! save}" value="Save"/>
        <apex:commandButton action="{! cancel}" value="Cancel"/>
</apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>

Here is the code for my controller extension (with my different attempts and notes with error details commented out):

public with sharing class MyCustomObjectExt {

    MyCustomObject__c record {get;set;}

    public MyCustomObjectExt(ApexPages.StandardController stdController) {   
        record = (MyCustomObject__c) stdController.getRecord();
        record.AccountName__c = ApexPages.currentPage().getParameters().get('acctId');  
        record.AccountType__c = ApexPages.currentPage().getParameters().get('acctType');

        // I started with the following, but cannot save as I get the following Error: Compile Error: Illegal assignment from String to Decimal at line 11 column 9  
        //record.Employees__c = ApexPages.currentPage().getParameters().get('acctEmp');        

       // I then tried this.  I am able to save the extension. But when I click the button to go to the VF page, I get a Visualforce Error: System.TypeException: Invalid decimal: 1,000 Class.MyCustomObjectExt.<init>: line 14, column 1
      //record.Employees__c = Decimal.valueOf(ApexPages.currentPage().getParameters().get('acctEmp'));        

     // I then tried this. I am able to save the extension. But when I click the button to go to the VF page, I get a Visualforce Error: System.TypeException: Invalid integer: 1,000 Class.MyCustomObjectExt.<init>: line 17, column 1
    //record.Employees__c = Integer.valueOf(ApexPages.currentPage().getParameters().get('acctEmp'));

    // I then tried this, but cannot save as I get the following Error: Compile Error: Illegal assignment from String to Decimal at line 21 column 9
     //string acctEmp = ApexPages.currentPage().getParameters().get('acctEmp');
    //record.Employees__c = String.valueOf(acctEmp);

    // I am stumped everything else works.

    record.SalesRep__c = UserInfo.getUserId();        
    record.OwnerId = Id.valueOf('enterstringforqueueidhere');            
}
}

Here is my custom button:

/apex/TestSSTRichTextForm?retURL={!Account.Id}&acctId={!Account.Id}&acctType={!Account.Type}&acctEmp={!Account.NumberOfEmployees}

I am so close, yet so far. Any help would be greatly appreciated! Michele

0

1 Answer 1

2

You need to strip the comma from the incoming number. Your controller is trying to cast the string '1,000' to a number (with the comma), since you are ripping it right from the page itself.

Try:

// remove commas from string:
String cleanAcctEmp = ApexPages.currentPage().getParameters().get('acctEmp').replaceAll(',','');
// set the value:
record.Employees__c = Integer.valueOf(cleanAcctEmp);
6
  • Or you can use the remove() method of the String class, String accntEmp = ApexPages.currentPage().getParameters().get('acctEmp'); record.Employees__c = Integer.valueOf(accntEmp.remove(','));
    – navD87
    May 14, 2015 at 21:18
  • I actually looked to see if there was a remove method in Apex as I couldn't remember, but the docs don't list it: salesforce.com/us/developer/docs/dbcom_apex250/Content/… May 14, 2015 at 21:20
  • It's listed here : salesforce.com/us/developer/docs/apexcode/Content/… Have I refered an old doc? It worked fine for me though
    – navD87
    May 14, 2015 at 21:38
  • Hmm the "dbcom_apex250" in the URL of the one I posted looks suspect, and yours is definitely more verbose...I'll stick with yours ;) May 14, 2015 at 21:39
  • Thanks Bri! that worked perfect. I'll try navD87's option as well, as it is good to learn. They are still defining requirements for the vf page, so once everything is done I will have to tackle the test class... ugh. Those really make my head spin. I wish SFDC documentation would include a snippet of the appropriate test methods for the different methods you might be using. The testing custom controllers and controller extensions documentation is a bit vague for newbies.
    – Michele
    May 14, 2015 at 21:58

You must log in to answer this question.

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