• Chris Tquila
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies

I don't have much credit finding this weird thing but I wanted to know if it's a bug or if it's really intended, which I cannot imagine. 

copy paste from this blog: http://blog.tquila.com/2012/06/25/grokking-force-dot-com-apex/

 

"Unit Tests increment your Objects unique Id

This is not covered in the documentation, or atleast I haven’t found it in the Apex docs. When you run unit tests that insert/create records for a particular object(standard or otherwise), it increments the value of the Name field(of type Auto Number) outside of the test context.

Let’s say you have a custom object “Invoice Statement” (Invoice_Statement__c) that has a Name field called “Invoice Number”, and is of type “Auto Number”. The format of the field is: INV-{0000}, and let’s assume that the latest record number is INV-2058.

If you insert 10 records in the test class as follows:

 

1// Let's assume that this runs inside of a test method
2List<Invoice_Statement__c> invStatementList = new List<Invoice_Statement__c>();
3for(Integer i = 0; i < 10; i++) {
4  invStatementList.add(new Invoice_Statement__c(company='acme ltd');
5}
6insert invStatementList;

 

Now, when you insert a new Invoice Statement record outside of the test method via a trigger, or batch, or the User Interface, the next inserted record will have the id INV-2069. So don’t rely on the unique name field if you have strict rules around the auto-increment feature in your application. Instead add a custom field which is auto-incremented in a workflow or trigger to have more granular control over how the value is incremented."

 

I verified that by myself, and this indeed happen. I remember having used these numbers for customer who want to have exactly one unique number per record (especially for bills), so this could be a huge issue.

 

Anyone aware of that? I didn't find it in the Salesforce known bug ....

I don't have much credit finding this weird thing but I wanted to know if it's a bug or if it's really intended, which I cannot imagine. 

copy paste from this blog: http://blog.tquila.com/2012/06/25/grokking-force-dot-com-apex/

 

"Unit Tests increment your Objects unique Id

This is not covered in the documentation, or atleast I haven’t found it in the Apex docs. When you run unit tests that insert/create records for a particular object(standard or otherwise), it increments the value of the Name field(of type Auto Number) outside of the test context.

Let’s say you have a custom object “Invoice Statement” (Invoice_Statement__c) that has a Name field called “Invoice Number”, and is of type “Auto Number”. The format of the field is: INV-{0000}, and let’s assume that the latest record number is INV-2058.

If you insert 10 records in the test class as follows:

 

1// Let's assume that this runs inside of a test method
2List<Invoice_Statement__c> invStatementList = new List<Invoice_Statement__c>();
3for(Integer i = 0; i < 10; i++) {
4  invStatementList.add(new Invoice_Statement__c(company='acme ltd');
5}
6insert invStatementList;

 

Now, when you insert a new Invoice Statement record outside of the test method via a trigger, or batch, or the User Interface, the next inserted record will have the id INV-2069. So don’t rely on the unique name field if you have strict rules around the auto-increment feature in your application. Instead add a custom field which is auto-incremented in a workflow or trigger to have more granular control over how the value is incremented."

 

I verified that by myself, and this indeed happen. I remember having used these numbers for customer who want to have exactly one unique number per record (especially for bills), so this could be a huge issue.

 

Anyone aware of that? I didn't find it in the Salesforce known bug ....

Hi,

 

I have a problem with my visualforce page.

 

This the kind of page/result I want to (in a simplified version):

 

<apex:page controller="Test1" >
	<apex:form >
		<apex:pageBlock>
			<apex:pageBlockButtons location="top">
				<apex:commandButton value="Save" action="{!saveNewRecord}" rerender="newRecordForm" />
			</apex:pageBlockButtons>
			
			<apex:pageBlockSection id="newRecordForm" >
				<apex:inputField value="{!newRecord['Name']}"/>
				<apex:inputField value="{!newRecord['Date__c']}" required="true"/>
			</apex:pageBlockSection>			
		</apex:pageBlock>
	</apex:form>
</apex:page>

 

public with sharing class Test1 {
	
	public sobject newRecord{get;set;}
	
	public Test1(){
		 this.newRecord = Schema.getGlobalDescribe().get('MyObject__c').newSObject();
	}
	
	public void saveNewRecord(){
		//Save Here
	}
}

 When I just fill the "Name" field and save, it works fine:

 

Name:
{My Value}
Date:
{empty}
  [ 18/06/2012 ]
Error: You must enter a value

 

 

Now I try do the same but with a dynamicComponent (I need to for my non-simplified page):

 

<apex:page controller="Test2" >
	<apex:form >
		<apex:pageBlock>
			<apex:pageBlockButtons location="top">
				<apex:commandButton value="Save" action="{!saveNewRecord}" rerender="newRecordForm" />
			</apex:pageBlockButtons>
			
			<apex:dynamicComponent componentValue="{!newRecordForm}"/>		
		</apex:pageBlock>
	</apex:form>
</apex:page>

 

public with sharing class Test2{
	
	public sobject newRecord{get;set;}
	
	public Test2(){
		 this.newRecord = Schema.getGlobalDescribe().get('MyObject__c').newSObject();
	}
	
	public Component.Apex.PageBlockSection getNewRecordForm(){
		
		Component.Apex.PageBlockSection form = new Component.Apex.PageBlockSection(id = 'newRecordForm');
		
		Component.Apex.inputField nameField = new Component.Apex.inputField();
		nameField.expressions.value = '{!newRecord[\'Name\']}';
			
		Component.Apex.inputField dateField = new Component.Apex.inputField(required = true);
		dateField.expressions.value = '{!newRecord[\'Date__c\']}';
		
		form.childComponents.add(nameField);
		form.childComponents.add(dateField);
		return form; 
	}
	
	public void saveNewRecord(){
		//Save Here
	}
}

 

 When I just fill the "Name" field and save, the "Name" value disappears on rerender:


Name:
{EMPTY}
Date:
{empty}
  [ 18/06/2012 ]
Error: You must enter a value

 

The newRecord controller variable doesn't seems to be updated here. But if I fill both fields, there is no error and the saveNewRecord() method retrieves both values...


I hope I was clear and that someone could help me.

 

Thanks