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
eliotstock2eliotstock2 

DateTime field on custom object not saved by DML insert

Hi there,

 

I have a VF page with a table of fields on it that allows me to create mulitple custom objects at once. In a custom controller I iterate over the custom objects, set some more stuff on them, add them to a list and then call insert() on that list. Every other field on the object is saved except for the DateTime fields. Another page on which I create these objects one by one works. The unit test for by controller also works, which sets up thenew object instance programmatically.

 

Is this a known issue or a bug or am I doing something wrong?

 

Here's a cut-down version of the VF page:

 

 

<apex:page controller="AddLineItems" tabStyle="Opportunity" id="line_items_page" sidebar="false"> <apex:sectionHeader title="Add Line Items to Opportunity" /> <apex:messages /> <apex:form id="line_items_form"> <div id="ins"></div> <apex:dataTable value="{!LineItems}" var="lic" id="line_items_table" border="0" cellPadding="2" cellSpacing="2"> <apex:column > <apex:facet name="header">Start Time</apex:facet> <apex:inputField value="{!lic.Start_Time__c}" id="start_time"/> </apex:column> <apex:column > <apex:facet name="header">End Time</apex:facet> <apex:inputField value="{!lic.End_Time__c}" id="end_time" /> </apex:column> </apex:dataTable> <apex:commandButton value="Save" action="{!saveLineItems}"/> <apex:commandButton value="Cancel" onclick="window.history.back();"/> </apex:form> </apex:page>

 and here's the controller:

 

 

public class AddLineItems { private Id opportunityId; private static final Map<String, Schema.RecordTypeInfo> opportunityTypeMap = Opportunity.SObjectType.getDescribe().getRecordTypeInfosByName(); public static final Id subContractTypeId = opportunityTypeMap.get('Sub-Contract').RecordTypeId; private List<Line_Item__c> lineItems = new Line_Item__c[] { new Line_Item__c(), new Line_Item__c(), new Line_Item__c(), new Line_Item__c(), new Line_Item__c(), new Line_Item__c(), new Line_Item__c(), new Line_Item__c()}; public AddLineItems() { this.opportunityId = ApexPages.currentPage().getParameters().get('opportunity_id'); System.assert(this.opportunityId != null, 'AddLineItems controller requires an opportunity_id' + ' parameter on the query string.'); } public Line_Item__c[] getLineItems() { return this.lineItems; } public PageReference cancelLineItems() { PageReference ref = new PageReference('/' + this.opportunityId); ref.setRedirect(true); return ref; } // 1 SOQL, 1 DML public PageReference saveLineItems() { Opportunity opp = [ SELECT RecordTypeId, Master_Contract__c FROM Opportunity WHERE Id = :this.opportunityId]; List<Line_Item__c> lineItemsToInsert = new List<Line_Item__c>(); for (Line_Item__c lineItem : this.lineItems) { // The type will be null for a row in the table that's not filled out if (lineItem.Line_Item_Type__c == null) continue; lineItem.Opportunity__c = this.opportunityId; if (opp.RecordTypeId == subContractTypeId) { lineItem.Block_Booking_Master__c = opp.Master_Contract__c; } // System.assert(false, '################### start time: ' + lineItem.Start_Time__c); lineItemsToInsert.add(lineItem); } insert lineItemsToInsert; PageReference ref = new PageReference('/' + this.opportunityId); ref.setRedirect(true); return ref; } public static testMethod void canSaveSevenLineItems() { Program__c program = new Program__c(Name = 'Test Program'); insert program; Opportunity opp = new Opportunity( RecordTypeId = subContractTypeId, Name = 'Test Opp', StageName = 'New', CloseDate = Date.today()); insert opp; Test.setCurrentPageReference(new PageReference('Page.AddLineItems')); System.currentPageReference().getParameters().put('opportunity_id', opp.Id); DateTime now = DateTime.now(); AddLineItems controller = new AddLineItems(); List<Line_Item__c> lineItems = controller.getLineItems(); Integer i = 0; for (Line_Item__c lineItem : lineItems) { i++; // Leave the last one blank if (i == 8) continue; lineItem.Program__c = program.Id; lineItem.Line_Item_Type__c = 'Performance'; lineItem.Fee_Charged__c = 1.00; lineItem.Start_Time__c = now; lineItem.End_Time__c = now; lineItem.MSAC_Allowable_Amount__c = i; lineItem.Audience_Age__c = i.format(); lineItem.Audience_Size__c = i; } controller.saveLineItems(); List<Line_Item__c> lineItemsAfterSave = [ SELECT Id, Program__c, Line_Item_Type__c, Fee_Charged__c, Start_Time__c, End_Time__c, MSAC_Allowable_Amount__c, Audience_Age__c, Audience_Size__c FROM Line_Item__c WHERE Program__c = :program.Id]; System.assertEquals(7, lineItemsAfterSave.size()); i = 0; for (Line_Item__c lineItem : lineItemsAfterSave) { i++; System.assertEquals(program.Id, lineItem.Program__c); System.assertEquals('Performance', lineItem.Line_Item_Type__c); System.assertEquals(1.00, lineItem.Fee_Charged__c); System.assertEquals(now, lineItem.Start_Time__c); System.assertEquals(now, lineItem.End_Time__c); System.assertEquals(i, lineItem.MSAC_Allowable_Amount__c); System.assertEquals(i.format(), lineItem.Audience_Age__c); System.assertEquals(i, lineItem.Audience_Size__c); } } }

 

Any help would be greatly appreciated.

 

Cheers,

 

Eliot Stock

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
eliotstock2eliotstock2
Please disregard! Have found the problem. Wasn't using the fields on the object that I thought I was.