You need to sign in to do that
Don't have an account?
Salesforce Test 5
File Uplaoding error in Vf page
Hi all
I have created a Vf Page for file uploading.when i am uploading file and going to save the record its showing error
Visualforce ErrorHelp for this Page
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!save}' in component <apex:commandButton> in page newsalesconfirmation: Class.NewSalesConfirmationExtension.save: line 55, column 1
Class.NewSalesConfirmationExtension.save: line 55, column 1
I have created a Vf Page for file uploading.when i am uploading file and going to save the record its showing error
Visualforce ErrorHelp for this Page
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!save}' in component <apex:commandButton> in page newsalesconfirmation: Class.NewSalesConfirmationExtension.save: line 55, column 1
public class NewSalesConfirmationExtension { public Purchase_Order__c purchaseOrder{get;set;} public Transient Attachment attachment {get;set;} public boolean showC1RecordType {get;set;} public boolean showC2RecordType {get;set;} private Id c1RecordTypeId; private Id c2RecordTypeId; public NewSalesConfirmationExtension(ApexPages.StandardController controller) { c1RecordTypeId = Schema.SObjectType.Purchase_Order__c.getRecordTypeInfosByName().get('C1').getRecordTypeId(); c2RecordTypeId = Schema.SObjectType.Purchase_Order__c.getRecordTypeInfosByName().get('C2').getRecordTypeId(); purchaseOrder = new Purchase_Order__c(); purchaseOrder = (Purchase_Order__c)controller.getRecord(); String isButtonClicked = Apexpages.currentPage().getParameters().get('setDefaultValues'); if(isButtonClicked == 'true'){ setDefaultValues(); } showC1RecordType = false; showC2RecordType = false; attachment = new Attachment(); if(purchaseOrder.RecordTypeId == c1RecordTypeId){ showC1RecordType = true; showC2RecordType = false; } else if(purchaseOrder.RecordTypeId == c2RecordTypeId){ showC1RecordType = false; showC2RecordType = true; } } public void setDefaultValues(){ String quoteId = Apexpages.currentPage().getParameters().get('quoteId'); Quote__c quote = [Select o.id, o.name, o.Quantity_formula__c, o.Opportunity_Product_Detail__r.Opportunity__c, o.Opportunity__c, o.Record_type_name__c,o.Company_Name__c,Opportunity_Product_Detail__r.Opportunity__r.Account__c from Quote__c o WHERE O.id=:quoteId]; //system.assert(false,quote); purchaseOrder.Opportunity__c = quote.Opportunity_Product_Detail__r.Opportunity__c; purchaseOrder.Quote__c = quote.id; purchaseOrder.Company__c = quote.Opportunity_Product_Detail__r.Opportunity__r.Account__c; if(quote.Record_type_name__c == 'C1'){ purchaseOrder.RecordTypeId = c1RecordTypeId; } else { purchaseOrder.RecordTypeId = c2RecordTypeId; } } public PageReference save(){ if(attachment.Body == null){ Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, 'Please upload recent Purchase Order before saving the record')); return null; } else{ try{ insert purchaseOrder; attachment.OwnerId = UserInfo.getUserId(); attachment.ParentId = purchaseOrder.id; insert attachment; return new PageReference('/'+purchaseOrder.id); } catch(DMLException e){ ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,e.getMessage())); return null; } } } } ---------------------------------------------------------------------------------------------- <apex:page standardController="Purchase_Order__c" extensions="NewSalesConfirmationExtension"> <apex:sectionHeader title="Sale Confirmation Orders" subtitle="{!purchaseOrder.Name}" /> <apex:form enctype="multipart/form-data"> <apex:pageMessages ></apex:pageMessages> <apex:pageBlock title="Sale Confirmation Order Edit"> <apex:pageBlockButtons > <apex:commandButton action="{!save}" value="Save"/> <apex:commandButton action="{!cancel}" value="Cancel"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Information" columns="2" collapsible="false" rendered="{!showC1RecordType}"> <apex:inputField value="{!purchaseOrder.Opportunity__c}"/> <apex:inputField value="{!purchaseOrder.Sale_Confirmation_Order_Number__c}" required="true"/> <apex:inputField value="{!purchaseOrder.GRN_needed_for_Bill_Processing__c}"/> <apex:inputField value="{!purchaseOrder.Sale_Confirmation_Order_Date__c}" required="true"/> <apex:inputField value="{!purchaseOrder.No_of_Days_for_collecting_GRN__c}"/> <apex:inputField value="{!purchaseOrder.Pre_shipment_Inspection_required__c}" required="true" /> <apex:inputField value="{!purchaseOrder.Purchase_Frequency__c}" required="true" /> <apex:inputField value="{!purchaseOrder.Customer_Ordered_Quantity__c}" required="true" /> <apex:inputField value="{!purchaseOrder.Credit_Limit__c}"/> <apex:inputField value="{!purchaseOrder.Customer_Price__c}"/> <apex:inputField value="{!purchaseOrder.CurrencyIsoCode}"/> <apex:inputField value="{!purchaseOrder.Bill_Submission_Period__c}"/> <!---<apex:inputField value="{!purchaseOrder.New_Flash_Aidc_Email_sent__c}"/>---> <!--- <apex:inputField value="{!purchaseOrder.New_Flash_Email_sent__c}"/>---> <apex:inputField value="{!purchaseOrder.Committed_Dispatch_Date__c}" required="true"/> <!-- <apex:inputField value="{!purchaseOrder.PO_Uploaded__c}"/> <apex:inputField value="{!purchaseOrder.Try__c}"/> <apex:inputField value="{!purchaseOrder.CountAttachment__c}"/>---> </apex:PageBlockSection> <apex:pageBlockSection title="Information" columns="2" collapsible="false" rendered="{!showC2RecordType}"> <apex:inputField value="{!purchaseOrder.Opportunity__c}"/> <apex:inputField value="{!purchaseOrder.Sale_Confirmation_Order_Number__c}" required="true"/> <apex:inputField value="{!purchaseOrder.GRN_needed_for_Bill_Processing__c}"/> <apex:inputField value="{!purchaseOrder.Sale_Confirmation_Order_Date__c}" required="true"/> <apex:inputField value="{!purchaseOrder.No_of_Days_for_collecting_GRN__c}"/> <apex:inputField value="{!purchaseOrder.Committed_Dispatch_Date__c}" required="true"/> <apex:inputField value="{!purchaseOrder.Credit_Limit__c}"/> <apex:inputField value="{!purchaseOrder.Customer_Ordered_Quantity__c}"/> <apex:inputField value="{!purchaseOrder.CurrencyIsoCode}"/> <apex:inputField value="{!purchaseOrder.Customer_Price__c}"/> <apex:inputField value="{!purchaseOrder.Purchase_Frequency__c}" required="true" /> <apex:inputField value="{!purchaseOrder.Bill_Submission_Period__c}"/> <apex:inputField value="{!purchaseOrder.Application_Area__c}"/> <!---<apex:inputField value="{!purchaseOrder.CountAttachment__c}"/> <apex:inputField value="{!purchaseOrder.Try__c}"/> <apex:inputField value="{!purchaseOrder.New_Flash_Email_sent__c}"/> <apex:inputField value="{!purchaseOrder.New_Flash_Aidc_Email_sent__c}"/>----> </apex:PageBlockSection> <apex:pageBlockSection title="Quote Details" columns="1" collapsible="false"> <apex:inputField value="{!purchaseOrder.Quote__c}"/> <apex:inputField value="{!purchaseOrder.Company__c}"/> <apex:inputField value="{!purchaseOrder.Delivery_Address__c}" required="true" /> </apex:PageBlockSection> <apex:pageBlockSection title="New Flash Details" columns="2" collapsible="false"> <apex:inputField value="{!purchaseOrder.Application_Area__c}" rendered="{!showC1RecordType}"/> <apex:inputField value="{!purchaseOrder.Remarks__c}"/> </apex:PageBlockSection> <apex:pageBlockSection title="Upload Purchase Order" columns="2" collapsible="false"> <apex:pageBlockSectionItem > <apex:outputLabel value="File" for="file" /> <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/> </apex:pageBlockSectionItem> </apex:PageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
Class.NewSalesConfirmationExtension.save: line 55, column 1
public PageReference save(){
if(attachment != NULL && attachment.Body == null){
Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, 'Please upload recent Purchase Order before saving the record'));
return null;
}
else{
try{
insert purchaseOrder;
attachment.OwnerId = UserInfo.getUserId();
attachment.ParentId = purchaseOrder.id;
insert attachment;
return new PageReference('/'+purchaseOrder.id);
}
catch(DMLException e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,e.getMessage()));
return null;
}
}
}
If this solves the answer, mark this as solved.
Visualforce ErrorHelp for this Page
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!save}' in component <apex:commandButton> in page newsalesconfirmation: Class.NewSalesConfirmationExtension.save: line 91, column 1
Class.NewSalesConfirmationExtension.save: line 91, column 1
Try to query the user, then assign the userid to ownerid. as shown below.
User u = [select id,firstname from user where id=:userinfo.getuserid()];
then
attachment.OwnerId = u.id;
also place system.debug();
after query to check wether user is retrieved or not.
Regards
Prad