0

I am trying to pass a String field from VFPage to an Apex custom controller class and use this field to add to a list . Can you help with the problem (string value is not passing to the list, it is always null) What am I missing?

Visualforce code

<apex:pageBlockSectionItem>
<apex:outputLabel Value="Scan Item">
<apex:inputtext Value="{!scans}">
</apex:pageBlockSectionItem>
<apex:commandButton value="Go" action="{!getscan}">
<apex:pageBlockSection>

Apex Class Code

public string scans {get; set;}
public string namestr = Apexpages.currentPage().getParameters().get('scans');
public PageReference getscan() {TAList.add(New Document_Detail__c(Document__c = cID,apply_tax__c=TRUE, sitem__c = namestr, item_qty__c=1, Transaction_Type__c = 'Regular')); 
return null;}

sitem__c is always null (namestr is not getting value from scans)

Any pointers , greatly appreciated

thanks,

Eug

2 Answers 2

0

Try using below code. Controller Code :

public string scans {get; set;}
public PageReference getscan() {
    TAList.add(new Document_Detail__c(Document__c = cID,apply_tax__c=TRUE, sitem__c = scans, item_qty__c=1, Transaction_Type__c = 'Regular')); 
    return null;
}
2
  • I am getting an error Invalid ID : 499Error is in expression '{!getscan}' in component <apex:commandButton>
    – Eugene T
    Commented Dec 4, 2015 at 7:13
  • can you update your complete Vf code and controller code
    – Himanshu
    Commented Dec 5, 2015 at 4:46
0

Update your controller to as

public string scans {get; set;}
public string namestr ;

public PageReference getscan() {
    //I agree with another post, this is a stupid line.
    //You use this method, when you are accessing page parameters or parameters passed through action methods.
    namestr = Apexpages.currentPage().getParameters().get('scans');

    // You can directly refer sitem__c=scans
    TAList.add(New Document_Detail__c(Document__c = cID,apply_tax__c=TRUE, sitem__c = scans, item_qty__c=1,                     Transaction_Type__c = 'Regular')); 
    return null;
}

Class variables are initialised at the page load, and at that moment 'Scans' was empty. That's why you were getting a blank string.

However this method 'getScan' is called after you have updated the value Scans value in your page.

2
  • the original suggestion did not throw any run time errors , but again null value was passed. The revised suggestions throws Invalid Id exception (499)
    – Eugene T
    Commented Dec 4, 2015 at 7:15
  • Obviously it will throw an error, as there is no cID parameter. Either post your complete code or post relevant code where you are populating all variables like TAList, cID etc. Commented Dec 4, 2015 at 7:19

You must log in to answer this question.

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