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

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;
}
  • I am getting an error Invalid ID : 499Error is in expression '{!getscan}' in component <apex:commandButton> – Eugene T Dec 4 '15 at 7:13
  • can you update your complete Vf code and controller code – Himanshu Dec 5 '15 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.

  • 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 Dec 4 '15 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. – Salesforcesmarty Dec 4 '15 at 7:19

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.