0

I am having trouble passing InputField data from a visualforce page into an apex controller. I feel like I have gone through 100 posts but nothing has helped me out. The goal here is to pass a new "Expiration" Date and "PO" attachment into the controller for it to add to existing records. When I bring up the VF page, attach the PO, add the date and click save, the controller yells at me saying the PO and date are null. I have attached my controller and VF Page code here.

Controller (the set Expiration Date part is commented out for debugging purposes):

public class ClosedWonMSAApex {

public Attachment file{get;set;}
public Opportunity opExpDate{get;set;}
Opportunity theOpportunity = new Opportunity();   

public ClosedWonMSAApex(ApexPages.StandardController stdController) {        
    this.theOpportunity = (Opportunity)stdController.getRecord();
} 


List<Opportunity> op = [ 
    SELECT Pricebook2Id, 
    LeadSource, 
    PrimaryContact__c , 
    Type,
    Loss_Reason__c, 
    MSA_Expiration_Date__c,
    CloseDate,
    Name, 
    StageName, 
    AccountId 
    FROM Opportunity 
    WHERE Id = :ApexPages.currentPage().getParameters().get('id')];



public PageReference save(){

    //Get values form VisualForce
    Opportunity opExpDate = new Opportunity();
    Attachment file  = new Attachment();

    //Create the new Op------------------------------------------------------------------------------------
    String newName;
    newName = '' + op[0].Name;
    newName = newName.substring(0,newName.length()-4);
    integer tempDate; 
    tempDate = op[0].CloseDate.year();
    tempDate++;
    newName = newName + tempDate;

    Opportunity newOP = new Opportunity(
        Pricebook2Id = op[0].Pricebook2Id, 
        LeadSource = op[0].LeadSource, 
        PrimaryContact__c = op[0].PrimaryContact__c,
        Type = op[0].Type,
        Loss_Reason__c  = op[0].Loss_Reason__c,
        CloseDate = op[0].CloseDate.addYears(1),
        Name = newName,
        StageName = 'AM - MSA Scheduled',
        AccountId = op[0].AccountId    
    );

    //Set the new Expiration date
    // if(op[0].MSA_Expiration_Date__c == null)
    // {
    //     newOP.MSA_Expiration_Date__c = opExpDate.MSA_Expiration_Date__c;
    // }

    // else
    // {
    //   newOP.MSA_Expiration_Date__c = op[0].MSA_Expiration_Date__c.addYears(1);
    // }       
    insert(newOP);

    //Attach the file-------------------------------------------------------------------------------------
    if(file != null)
    {
        Attachment attach = new Attachment(ParentId = op[0].Id, Name = file.name, Body = file.body);
        insert attach;
    }

    //Update the Old Opportunity--------------------------------------------------------------------------
    op[0].StageName = 'Closed Won';
    update(op[0]);

    return null;
}

}

VisualForce:

<apex:page standardController="Opportunity" extensions="ClosedWonMSAApex" showQuickActionVfHeader="false">  
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection rendered="{!If(Opportunity.MSA_Expiration_Date__c == null,true,false) }" title="What is the new expiration date for this system(s)?">
            <apex:inputField value="{!opExpDate.MSA_Expiration_Date__c}" id="opExpDate"/>   
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Attachment">
            Attach PO: <apex:inputFile value="{!file.body}" fileName="{!file.name}"></apex:inputFile>
        </apex:pageBlockSection>
    </apex:pageBlock>
    <apex:commandButton value="Save and Close" action="{!save}"/>        
</apex:form>  

1

You need to initialize both variable in your controller. As you have didn't initialize them so they are default null and throwing exception.

public ClosedWonMSAApex(ApexPages.StandardController stdController) { this.theOpportunity = (Opportunity)stdController.getRecord();
File = new attachment ();
Opexpdate = new opportunity ();
} 
| improve this answer | |
  • 1
    This worked for me with another little adjustment. I initialized both variables in the constructor but I was also re-creating the variables in the save method. After deleting those lines it worked great. Thank you! – Rory May 4 '18 at 19:11

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.