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
RatherGeekyRatherGeeky 

Visualforce page inputfield not accepting close date value

Am a newbie trying to add an inputfield to a visualforce page that allows the user to change the Opp'ty Close Date. (I've only pasted the part of my code that is problematic.)

 

var changeCloseDate = document.getElementById("{!$Component.f.pb.pbs.si4.closeDate}");


oppToClose.CloseDate changeCloseDate;

<apex:pageBlockSectionItem id="si4">
<apex:outputLabel value="Close Date"/>
<apex:inputField id="closeDate" value="{!Opportunity.CloseDate}"/>
</apex:pageBlockSectionItem>

Getting error: {faultcode:'soapenv:Client',faultstring:"4/1/2009' is not a valid value for the type xsd:date',}

 

Do I have to format the date a different way?

Tried a few changes, but then started getting error: {faultcode:'soapenv:Client',faultstring:"[object HTMLInputElement]" is not a valid value for the type xsd:date',}

Suggestions welcome!

 

 

Best Answer chosen by Admin (Salesforce Developers) 
RatherGeekyRatherGeeky

Okay, so I totally understand that no one replied to that because it drives me nuts when people post a bunch of code and then ask sheepishly "Can you fix this for me?" I did the exact same thing.

 

Incidentally, I figured out a solution for this on my own. It was complicated by the fact that much of our code on this page is written in javascript.

var new_amount = document.getElementById("{!$Component.f.pb2.pbs2.si5.edit_Amount}"); var won = (wonElement.value == "Yes"); // Close the current opportunity var oppToClose = new sforce.SObject("Opportunity"); oppToClose.Id = "{!Opportunity.Id}"; oppToClose.StageName = reasonElement.value; // format date in a way that can be used by SF var dt = new Array(); dt = new_closedate.value; dt = dt.split('/'); var d = dt[0]; // return day var m = dt[1]; // return month var y = dt[2]; // return year

// if day or month is only two digits, pad with a 0 if(d.length = 1) { d = "0" + d; } if(m.length = 1){ m = "0" + m; } oppToClose.CloseDate = y + "-" + d + "-" + m;

 

 

Hopefully someone else out there can benefit from my mistakes!

All Answers

Srinivas_V2Srinivas_V2

you have to use the following code in your controller class

considering you set the CloseDate as a visualforce property oppcloseDate here. You have to use custom Save

String[] strdatearr = oppCloseDate.split('/');
Integer dt = Integer.valueOf(strdatearr[0]);
Integer month = Integer.valueOf(strdatearr[1]);
Integer year = Integer.valueOf(strdatearr[2]);
date oppClosedDate = date.newInstance(year, month, dt);
Opportunity newOpp = new Opportunity();
newOpp.Name = oppName;
newOpp.CloseDate = oppClosedDate;

 

MeghaRaheja1MeghaRaheja1

Try this.Its working...

 

<apex:page standardController="Opportunity">
<apex:form>
 
 
  <apex:pageblock>
      <apex:pageBlockSection>
          <apex:inputField value="{!Opportunity.CloseDate}" />
      </apex:pageBlockSection>
  </apex:pageblock>
 
  </apex:form>
</apex:page>

RatherGeekyRatherGeeky

I already have all that information. Only difference is that I am specifying id within apex:inputfield tag so that I can pass it back to the Opportunity.Perhaps that's the wrong method for me to use?

RatherGeekyRatherGeeky

I appreciate the help, but I don't quite understand. The logic is implemented in Javascript instead of an

APEX controller, so I haven't defined a controller class besides the standard Opportunity. Could this be the issue?

Again, thanks for the help. I am customizing a page that a consultant designed. It has been working just
fine so far.

 

Srinivas_V2Srinivas_V2
Even you can do that in javascript also if u really need it. But when you use visualforce instead of scontrols this is recommended to implement in controllers
Ron HessRon Hess

you dont' need any javascript to bind the input field to the opportunity close date.

 

it looks like your javascript is changing the value in the input field, which you don't need to do.

 

you haven't included enough of your javascript to understand why you need javascript, so it's hard to guess.

 

 The key concept is data binding, input fields are bound ( passed) to values in the contrller, standard or custom so that you wont have to write any javascript.

RatherGeekyRatherGeeky

Ron: Thank you for telling me that input fields are passed to the controller. I didn't realize that. That clears up a lot in my head.

We are using javascript on a visualforce page. I think the main reason is because the consultant who helped us set it up wanted it to be easier to maintain. 

... Okay, so I've tried a few different ways of doing this, but the value I type on my visualforce page is still not binding to the value on my Opportunity.

 

Found this on the sf page developers guide: "The only fields that the <apex:inputField> tag cannot display are those defined as member variables of a custom
controller class written in Apex."

 

I've tried playing around with a it, but so far no luck. Here's all of the code, just in case. Have highlighted the applicable code in blue.

 

<apex:page standardController="Opportunity" title="Close Opportunity">

<apex:includeScript value="/soap/ajax/12.0/connection.js"/>
<apex:includeScript value="/soap/ajax/12.0/apex.js"/>

<script type="text/javascript">

/*
** onClickClose
**
** Click handler for "Close Opportunity" button
*/
function onClickClose() {



// Close the opportunity
var status = document.getElementById("{!$Component.f.status}");
status.innerHTML = "Closing opportunity. Please wait...";
document.body.style.cursor = "wait";
setTimeout("closeOpp();", 100);
}


/*
** toggleReason
**
** Toggle UI elements depending on whether the
** Oppportunity is won or lost
*/
function toggleReason() {

// Inspect DOM for UI elements
var wonElement = document.getElementById("{!$Component.f.pb.pbs.si2.isWon}");
var reasonElement = document.getElementById("{!$Component.f.pb.pbs.si3.closeReason}");
var isWon = (wonElement.value == "Yes");


// Remove existing items from reason list
while (reasonElement.options.length > 0) {
reasonElement.remove(0);
}

// Add reasons
if (isWon) {
addReason(reasonElement, "Selected");
addReason(reasonElement, "SOW Signed");
} else {
addReason(reasonElement, "Lost - Competitor");
addReason(reasonElement, "Lost - No Decision");
addReason(reasonElement, "Lost - Insufficient Budget");
addReason(reasonElement, "Lost - Land Survey Selected Instead");
addReason(reasonElement, "Lost - Client Not Selected");
addReason(reasonElement, "Lost - Reason Unknown");
addReason(reasonElement, "Lost - Cancelled");
addReason(reasonElement, "Lost - Change in Scope");
addReason(reasonElement, "Unqualified");
}
reasonElement.selectedIndex = 0;

}


/*
** addReason
**
** Dynamically add items to the reason won/lost
** select list
*/
function addReason(list, reason) {
var opt = document.createElement("option");
opt.text = reason;
opt.value = reason;

if(document.all && !window.opera) {
list.add(opt);
} else {
list.add(opt, null);
}
}


/*
** closeOpp
**
** Closes opportunities and creates
** follow-on projects and opportunities as appropriate
**
** This logic is implemented in Javascript instead of an APEX controller
** extension to avoid the need to maintain and deploy APEX code.
*/
function closeOpp() {

// Set session header
sforce.connection.sessionId = '{!$Api.Session_ID}';

// Get DOM elements
var wonElement = document.getElementById("{!$Component.f.pb.pbs.si2.isWon}");
var reasonElement = document.getElementById("{!$Component.f.pb.pbs.si3.closeReason}");
var won = (wonElement.value == "Yes");
var newDate = document.getElementById("{!$Component.f.pb.pbs.si4.closeDate}");


// Close the current opportunity -- TODO: Flip record type
var oppToClose = new sforce.SObject("Opportunity");
oppToClose.Id = "{!Opportunity.Id}";
oppToClose.StageName = reasonElement.value;

oppToClose.CloseDate = newDate;
//
try {
res = sforce.connection.update([oppToClose]);
} catch (err) {
alert(err);
}

// Retrieve this opp from the database
var thisOpp = retrieveOpp();


// Create recurring Opportunity
if("true" == "{!Opportunity.Recurring__c}") {
createRecurrence(thisOpp);
}

// Create a new project or contract
if (won) {
if ("{!Opportunity.Type}" == "Master Contract / IDIQ") {
var contractId = createContract(thisOpp);
window.location.href="/" + contractId;
} else {
var projectId = createProject(thisOpp);
window.location.href="/" + projectId;
}
} else {
window.location.href="/{!Opportunity.Id}";
}
}


/*
** retrieveOpp
**
** Retrieves the current opportunity as an SObject that can be copied in memory
*/
function retrieveOpp() {
var fields = "";

// Generate list of fields we expect to copy to either an opportunity or project
var dResult = sforce.connection.describeSObject("Opportunity");
for (var i=0; i < dResult.fields.length; i++) {
var f = dResult.fields[i];
fields += (i > 0 ? "," : "") + f.name;
}

// Retrieve the Opportunity
opp = sforce.connection.retrieve(fields, "Opportunity", ["{!Opportunity.Id}"]);
return opp[0];
}


/*
** createRecurrence()
**
** Creates a new recurring opportunity and links it
** to a newly closed opportunity

function createRecurrence(sourceOpp) {
var ret = "";
var res = "";

// Calcuate close date for next occurence
var recurDate = sourceOpp.getDate("CloseDate");
var intervalMonths = sourceOpp.getInt("Recur_Interval_Months__c");
if (intervalMonths) {
recurDate.setMonth(recurDate.getMonth() + intervalMonths);
}

// Initialize new opportunity from closed opportunity
var opp = new sforce.SObject("Opportunity");
opp.AccountId = sourceOpp.AccountId;
opp.Name = sourceOpp.Name;
opp.Amount = sourceOpp.Amount;
opp.StageName = "Needs Analysis"; // TODO -- confirm stage
opp.CloseDate = recurDate;


// TODO -- map additional fields as needed

// Create the Opportunity
try {
res = sforce.connection.create([opp]);
} catch (err) {
alert(err);
}
if (res[0].getBoolean("success")) {
ret = res[0].id;
}

return (ret);
}


/*
** createContract()
** Creates a new contract for a closed opportunity
**
** This logic is implemented in Javascript instead of an APEX controller
** extension to avoid the need to maintain
** and deploy APEX code.
*/
function createContract(sourceOpp) {
var ret = "";
var res = "";

// Initialize contract from Opportunity data
var contract = new sforce.SObject("Contract");
contract.AccountId = sourceOpp.AccountId;
contract.Name = sourceOpp.Name;
contract.Parent_Opportunity__c = sourceOpp.Id;


// Create the contract
try {
res = sforce.connection.create([contract]);
} catch (err) {
alert(err);
}
if (res[0].getBoolean("success")) {
ret = res[0].id;
}

return (ret);
}


/*
** createProject()
**
** Creates a new project and links it to a newly
** closed oppportunity
**
** This logic is implemented in Javascript instead of an APEX controller
** extension to avoid the need for Aero-Metric to maintain
** and deploy APEX code.
*/
function createProject(sourceOpp) {
var ret = "";
var res = "";

// Initialize project from Opportunity data
var project = new sforce.SObject("Summary__c");
project.Name = " - ";
project.projName__c = sourceOpp.Name;
project.projAccount__c = sourceOpp.AccountId;

 

// TODO -- map additional fields as needed

// Create the project
try {
res = sforce.connection.create([project]);
} catch (err) {
alert(err);
}
if (res[0].getBoolean("success")) {
ret = res[0].id;
}

return (ret);
}


</script>



<apex:sectionHeader title="Close Opportunity" subtitle="{!Opportunity.Name} - Resulting in a {!Opportunity.Type}"/>
<apex:form id="f">
<apex:pageBlock title="Opportunity Win/Loss Information" id="pb">
<apex:pageBlockSection columns="1" title="Why is this Opportunity Closing?" id="pbs">

<apex:outputField value="{!Opportunity.Account.Name}"/>
<apex:outputField value="{!Opportunity.Region_Client_ID__c}"/>
<apex:outputField value="{!Opportunity.Type}"/>

<apex:pageBlockSectionItem id="si1">
<apex:outputLabel value="Current Sales Stage"/>
<apex:outputText value="{!Opportunity.StageName}"/>
</apex:pageBlockSectionItem>


<apex:pageBlockSectionItem id="si2">
<apex:outputLabel value="Did we win this deal?"/>
<apex:selectList id="isWon" size="1" onChange="toggleReason()">
<apex:selectOption itemLabel="Yes" itemValue="Yes"/>
<apex:selectOption itemLabel="No" itemValue="No"/>
</apex:selectList>
</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem id="si3">
<apex:outputLabel value="What was the reason?"/>
<apex:selectList id="closeReason" size="1">
<apex:selectOption itemLabel="Selected" itemValue="Selected"/>
<apex:selectOption itemLabel="SOW Signed" itemValue="SOW Signed"/>
</apex:selectList>
</apex:pageBlockSectionItem>

<apex:pageBlockSection >
<apex:outputLabel value="Close Date"/>
<apex:inputField id="closeDate" value="{!Opportunity.CloseDate}"/>
</apex:pageBlockSection>




</apex:pageBlockSection>
</apex:pageBlock>

<input class="btn" type="button" title="Close Opportunity" name="close" value=" Close Opportunity " onClick="onClickClose()"/>
<apex:commandButton action="{!cancel}" value="Cancel" id="cancelButton"/>
<apex:outputText value="" id="status"/>

</apex:form>

</apex:page>

 

 

 

RatherGeekyRatherGeeky

Okay, so I totally understand that no one replied to that because it drives me nuts when people post a bunch of code and then ask sheepishly "Can you fix this for me?" I did the exact same thing.

 

Incidentally, I figured out a solution for this on my own. It was complicated by the fact that much of our code on this page is written in javascript.

var new_amount = document.getElementById("{!$Component.f.pb2.pbs2.si5.edit_Amount}"); var won = (wonElement.value == "Yes"); // Close the current opportunity var oppToClose = new sforce.SObject("Opportunity"); oppToClose.Id = "{!Opportunity.Id}"; oppToClose.StageName = reasonElement.value; // format date in a way that can be used by SF var dt = new Array(); dt = new_closedate.value; dt = dt.split('/'); var d = dt[0]; // return day var m = dt[1]; // return month var y = dt[2]; // return year

// if day or month is only two digits, pad with a 0 if(d.length = 1) { d = "0" + d; } if(m.length = 1){ m = "0" + m; } oppToClose.CloseDate = y + "-" + d + "-" + m;

 

 

Hopefully someone else out there can benefit from my mistakes!

This was selected as the best answer