+ Start a Discussion
robertcw777robertcw777 

Invalid Type on Custom Objects

Getting started with force.com and ran into a roadblock right away.  I defined a custom object, and then used it in an Apex class definition.  However when I used the object name in the class definition (page 29 of developer guide) (with the _c), I get aninvalid type compile error. 

 

Went back to basics from the developers guide to replicate their simple example.  Followed instructions to define a "Book" object as in the guide, and then copied the code:

 

public class MyHelloWorld {

 

    public static void applyDiscount(Book__c[] books) {    

        for (Book__c b : books)  {    

           b.Price__c *= 0.9;    

        }

     }

 }

 

I get similar "Error: Compile Error: Invalid type: Book__c at line 4 column 10".  Is there some setting I need to make to make the custom object visible to Apex?  I've searched everywhere and can't find any reference to it.

 

Can anyone point me in the right direction to get moving?

 

robertcw7777

 

 

Best Answer chosen by Admin (Salesforce Developers) 
ericsdawsonericsdawson

it looks like you are mixing up "Book_c" and "Book__c" (one underscore vs. two)

All Answers

Aar3538Aar3538

Hello,

 

From my perspective everything looks to be correct for your code.

 

Could you do me a favor and login into your salesforce org and go to Setup->Create->Objects.  Find your Book object and click on the link.  Check the field API Name and make sure it reads Book__c and not Book_c__c or something invalid.  Also confirm if you have more then one org, that you're logged into the same org you created the object in (a dumb statement, but I've done that before!)

 

Hope this helps

robertcw777robertcw777

Thanks for the quick reply.  It is definitely "Book_c" in the API name.  And, this happens for any custom object I have created.  Very strange.  I also have only one login and organization.

Chris DaviesChris Davies

Have a look in the Schema, is the sObject available to query?

robertcw777robertcw777

I used a test query in, and got the error message below.

 

test = [select price from Book_c];

 

 

Error: Compile Error: sObject type 'Book__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 6 column 16

RovRov

After reading the posts it appears that you have named your object as Book_c hence the API name will be Book_c__c

So when you query it should be

 

[SELECT Price__c FROM Book_c__c]; 

 

Let me know how you go.

robertcw777robertcw777

Thanks for suggestion, but the object name is "Book" and the API name shown in the object definition is "Book_c".

ericsdawsonericsdawson

it looks like you are mixing up "Book_c" and "Book__c" (one underscore vs. two)

This was selected as the best answer
Satish12Satish12

HI

 

i am getting the error that Book__c is invalid indentifier, i checked in API it is correct but it throws the same error

 

public class MyHelloWorld {
public static void applyDiscount(Book__c[] books)
{
    for (Book__c b :books)
    {
    b.Price__c *= 0.9;
    }
}
}

 

Please helpout from this.

robertcw777robertcw777

Yes, that was the problem. I figured that out and got by it. Thanks.

Prasanna anjaneyuluPrasanna anjaneyulu

Here the issue is with underscore. We are using single '_' but it's accepting only double '__' hence the issue. So, Use __ to get pass this error as follows...

 

public class MyBooks
{
   public static void applyDiscount(Book__c[] books){
       for (Book__c b:books){
           b.Price__c *= 0.9;
       }
   }
}

Vikas Pandey 12Vikas Pandey 12
Actually, this one is the correct code:

public class MyHelloWorld {
   public static void applyDiscount(Books__c[] books) {
      for (Books__c b :books){
         b.Price__c *= 0.9;
      }
   }
}