I get this error after clicking a button:

The name can only contain underscores and alphanumeric characters. It must begin with a letter and be unique, and must not include spaces, end with an underscore, or contain two consecutive underscores.

I need to create new record for my custom object. Here is my code:

public class addVehicle {

public String caryear {get;set;}
public String carmake {get;set;}
public String carmodel {get;set;}
public addVehicle () {

public String getResponse() {
    Vehicle__c vehicleObject = new Vehicle__c (Year__c = caryear, Make__c = carmake, Model__c = carmodel);

    try {
      insert vehicleObject; // inserts the new record into the database
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new vehicle.'));
      return null;

    return '{"success":"true"}';


I get values from custom selects with JS. Help please :)

Kind regards, Nick

  • Do you have a workflow rule setting the name to the concatenation of the make model and year? If so then it would be setting the name to "" based on the code you posted – Eric Sep 6 '17 at 14:47
  • this looks like a problem with the page/JS code – cropredy Sep 9 '17 at 22:30

I decided to delete apex class and use Visualforce page only. I did like this:

<apex:form >

<apex:inputHidden value="{!Vehicle__c.Name}" id="carname"/>
<apex:inputHidden value="{!Vehicle__c.Year}" id="caryear"/>
<apex:commandButton action="{!save}" value="send" id="save" />


$('#findBtn').click(function() {

$('.apexp .bPageBlock.apexDefaultPageBlock .pbBody input:nth-child(1)').val(thisName);
$('.apexp .bPageBlock.apexDefaultPageBlock .pbBody input:nth-child(2)').val(thisYear);
$('.form-group-sm input[type=submit]').trigger('click');


You have to provide some name. Change your below line as,

Vehicle__c vehicleObject = new Vehicle__c (Name = 'Some String', Year__c = caryear, Make__c = carmake, Model__c = carmodel);
  • Yes but I don't want to edit code every time I create a record. It needs to generate automatically. For example "a2Rc00000013Hnj". It do this way for now but display error. P.S. even if I write "Name = 'Some String'" I get this error, although I didn't have record with this name. – Nick567 Sep 6 '17 at 10:23
  • You can either provide default value to it. – Amol Jadhav Sep 6 '17 at 10:27
  • When I set Autonumber to Name it doesn't help... I try VH{0000000} format – Nick567 Sep 6 '17 at 10:48
  • You have to type cast the number to text(String), like String.valueOf(num). – Amol Jadhav Sep 6 '17 at 11:24
  • Sorry, I don't understand what should I do. Can you provide more info? – Nick567 Sep 6 '17 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.