function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alexander_EAlexander_E 

accessing date / datetime fields

Hello,

 

I have some experience in APEX and Visualforce, but still found issues on developing.

 

Could someone please give me an advice, how to overwrite the close date on Opportunities?

 

I have generated a new field on Opportunity, called NewDate__c (Date, Label: "New Close Date"). and a save-button on my ExtensionController.

everytime, when I access the NewDate__c in the method {!overridenewdatetoclosedate}, I receive this exeption:

Attempt to de-reference a null object
An unexpected error has occurred. Your development organization has been notified. 

 

my VF-PAge is this:

<apex:page standardController="Opportunity"
	extensions="ClsOppChangeCloseDate" action="{!init}">
	<apex:form id="FormChangeCloseDate1">
		<apex:pageBlock mode="inlineEdit" id="pb">
			<apex:pageBlockSection id="pbs1">
				<apex:commandButton action="{!overridenewdatetoclosedate}"
					value="Save all" id="btnSave" />
			</apex:pageBlockSection>
			<apex:pageBlockSection id="pbs2">
				<apex:outputField value="{!opportunity.closeDate}" />
				<apex:outputField value="{!opportunity.NewDate__c}" />
			</apex:pageBlockSection>
			<apex:pageBlockSection id="pbs3">
				<apex:outputText value="{!oldDate}" />
				<apex:outputText value="{!newDate}" />
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>

 

 

my ExtensionController is this:

public class ClsOppChangeCloseDate {
	private ApexPages.StandardController stdCtrl;

	public Datetime oldDate {get; set;}
	public Datetime newDate  {get; set;}
	public Integer difDate {get; set;}

	public Opportunity chOpp {get; set;}

	public ClsOppChangeCloseDate(ApexPages.StandardController std){
		stdCtrl=std;
	}

	public void init(){
		Opportunity chOpp = [SELECT Id, Name, CloseDate, NewDate__c FROM Opportunity WHERE Id = :stdCtrl.getId()];
		oldDate = chOpp.CloseDate;
		newDate = chOpp.NewDate__c;
	} // end init();

	public PageReference overridenewdatetoclosedate(){
	newDate = chOpp.NewDate__c;
	stdCtrl.save();
	return null;
	}

	public PageReference save() {
		stdCtrl.save();
		return null;
	}
}

 

any hints are welcome!

Best Answer chosen by Admin (Salesforce Developers) 
aballardaballard

You have never set the property chOpp to anything.   In the init() method you use a local variable called chOpp instead. 

Change that to use the class property chOpp instead and it should work.

 

 

All Answers

Alexander_EAlexander_E

my wish would be a method like this:

 

public PageReference overridenewdatetoclosedate(){
	newDate = chOpp.NewDate__c ;
	oldDate = newDate;
	chOpp.CloseDate = oldDate;
	update chOpp;

	stdCtrl.save();
	return null;
}

 than I can calculate the difference in Days between the datefields and add it to the oppsLineItems.

aballardaballard

You have never set the property chOpp to anything.   In the init() method you use a local variable called chOpp instead. 

Change that to use the class property chOpp instead and it should work.

 

 

This was selected as the best answer
Alexander_EAlexander_E

Thank you! :robothappy: