+ Start a Discussion
sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co 

Explanataion of Code

 

Clientacc =new Account(LastName='ABC',PersonMailingStreet='Street', PersonMailingCity='City',PersonMailingState='state',PersonMailingCountry='country',                    PersonMailingPostalCode='123456',FirstName='DEF',Retirement_Age__c='55', Estimated_Life_Expectancy__c='85',PersonBirthdate=system.today(), RecordTypeId='01220000000DnK3AAK');     	                                               upsert Clientacc;								system.assertNotEquals(null, Clientacc.id);				Apexpages.currentPage().getParameters().put('entityId',Clientacc.id);
Please explain me the entire code bcoz i am not able to get exactly from where it has taken the RecordTypeId. As it sees that it has been given by the programmer itself as dummy value. Is am i right or it is fetching from the my Object. And also if i want to fetch the value of RecordTypeId from my Object & want to put over there in above mentioned code then how will i achieve it...

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I've looked back over some old person account code, and where I've created a person account for test purposes I've had to specify a record type where the IsPersonType field is true.  It may be that is what opens up the FirstName, LastName fields.

All Answers

bob_buzzardbob_buzzard

This is a hard coded record type id, which may well be a valid id, but isn't the best way to do things.

 

You'd do better to retrieve the record type id by name (or API name) via a SOQL query.

 

 

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

can u help me with how can i do the same. fetch the record type id using soql query

bob_buzzardbob_buzzard

Sure:

 

 

String rtName='My Record Type';
RecordType rt =  [select ID FROM RecordType where Name = :rtName];

 

 

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

thanks,

     The prob is dis code is in test class and it is trying to insert values. der are no already exsisting values in my custom object. still the RecordType Id is used. i am not able to figure out how? how do get this RecordType ID for my proj? its a compulsory field so cant do with out it also.

bob_buzzardbob_buzzard

You can create a record type in a test class.  Are you hitting a particular error?

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

d error that i am getting is Invalid_cross_Reference_key,RecordTypeId value not valid for the user profile

 

bob_buzzardbob_buzzard

And is this using the id of the record type that is added during the test, or the hardcoded record type?

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

i have created a custom object called Client_Detail and Clientacc is the instance of the object.

the following code that is used is taking a hardcoded RecordTypeID to insert value into my client object. 

how can we get this recordtypeId even before entering the values?

Is there any way by which i get the same ID dynamically to insert values.

please help on this as this is a part of my test case class and am not able to work ahead.

 

 public static void parentData(){

      Clientacc =new Account(LastName='ABC',PersonMailingStreet='Street',PersonMailingCity='City',

                    PersonMailingState='state',PersonMailingCountry='country',

                    PersonMailingPostalCode='123456',FirstName='DEF',Retirement_Age__c='55',

                    Estimated_Life_Expectancy__c='85',PersonBirthdate=system.today(), RecordTypeId='01220000000DnK3AAK');

upsert Clientacc;

 

 

this is the code on which i am geting the error

bob_buzzardbob_buzzard

If you don't have any record types defined for your custom object, you can simply remove that item from the constructor:

 

 

 

Clientacc =new Account(LastName='ABC',PersonMailingStreet='Street',PersonMailingCity='City',
                    PersonMailingState='state',PersonMailingCountry='country',
                PersonMailingPostalCode='123456',FirstName='DEF',Retirement_Age__c='55',
                    Estimated_Life_Expectancy__c='85',PersonBirthdate=system.today());

 

 

 

 

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

I am getting the following error when i am trying to upsert these values into Account

 

16:17:11:044 FATAL_ERROR System.DmlException: Upsert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, record type missing for: Account: []

I am trying to figure out where am i missing out .....

 

there are custom settings settings defined on Account is this field related to it.....

 

 

 

 

 

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

This is the error msg that i am getting when i try to execute the code with out RecordTypeId

 

16:17:11:044 FATAL_ERROR System.DmlException: Upsert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, record type missing for: Account: []

 

I am trying to missing out where am i missing out....

 

there are custom setting also defined on object Account is der any realtion between them...

bob_buzzardbob_buzzard

I'm a bit confused now.  I thought we were talking about custom objects, but I see your code refers to accounts.

 

Do you have record types defined for accounts?  And do you care which record type you use for your test, or are you just trying to get an account created?

 

 

 

 

 

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

I am Sry if i am confusing u.... 

In my account Object der are custom fields defined but no record types. there are custom settings created for Account as 

Account Client

Account Family

that is all it contains.....

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

I also have a Client Detail custom object whis has a look-up field to Account.....

my earlier insert code is in test class so i am not able to move ahead in th project

bob_buzzardbob_buzzard

So it appears that you have a validation rule or similar set up to make record type a required field on account, but no actual record types.  Plus there are some custom settings that appear to be record type related.

 

Is this a system that you have inherited?  It seems odd that record types are configured up in some ways but not in the way that actually matters.

 

I think you'll need to create some record types for account.  If you do that, then you can look them up by name in the code when inserting the account record.

 

 

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

thanks alot....

If i create my own record type den wont it create problems.......

ya Account has Custom Settings defined.....

 

This is a line of Code that i have come accross in the DML query test class....

 

       Account acc= new Account(LastName='ABC',RecordTypeId=RecTypes__c.getInstance('AccountClient').Record_Type_Value__c);

 

       Here Account Client is my custom setting and Record_Record_Type_Value__c is the field

 

bob_buzzardbob_buzzard

In that case you'll need to:

 

(1) Create a record type for client accounts

(2) Store the record id in the AccountClient custom setting

 

and your code should work.

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

thanks a lot.. ill just try it hopefully it wil work....

looking fwd for ur help in future also...

 

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

i have a custom object with name as Client Account. shld i create Record type in that...

how do i save the record type Id in the custom setting of account

bob_buzzardbob_buzzard

I don't know whether you should create a record type for your custom object - that should be something specified in your design.

 

You can find the record type id using the schema explorer in the Force.com IDE, or by executing anonymous apex, or probably by accessing the record type in configuration and looking at the URL.  Then just paste this value in to the appropriate custom setting field.

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

I have tried all but nothign seems to work.

I used the following query in Schema Browser

Select RecordTypeId from Account;

 but it is not accepting the query...

  Can u suggets what can i try....

 

i came accross jeff douglas blog which is showing that RecordType Id is one of the fields in Account when v are  workign with person account.

but in my list of feilds its not being showed.......

bob_buzzardbob_buzzard

Record types are stored seperately to the account.

 

You'll need to use something like:

 

 

select id from RecordType where sobjectype='Account'

 

 

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

its not accepting the query......

bob_buzzardbob_buzzard

Helps if you provide an error message.

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

Can U help me as to From where is the hardcode RecordTypeId coming from i am not getting it......

Am stuck on this point since yest and am all confused now.....

bob_buzzardbob_buzzard

The hardcoded record type id is something one of your developers has added.  I've no idea where it came from, it may have been from another org, a developer edition or a sandbox.  It really doesn't matter. You will need to use a valid record id or you won't be able to create an account.

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

its giving me the error as Malformed Query RecordType where sobject ='Account'

bob_buzzardbob_buzzard

Can you post your exact query.

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

I posted the same query that u had written........

Select Id from RecordType where sobject='Account'

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

In System Log its giving me the following error

    loop did not match anything at input select

bob_buzzardbob_buzzard

That's not the query I posted.  You have changed the field name 'sobjectype' to 'sobject'.

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

no i executed the same query. while postign here i made d mistake.... sry

tried it again not giving me d same error

 

bob_buzzardbob_buzzard
Here's the query that runs correctly in my schema browser, even if I don't have any record types defined for account: select id from RecordType where sobjecttype='Account'
sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

thanx a ton i executed the same code n i got few id.........

i can replace these id's in my test class now and try it?

bob_buzzardbob_buzzard

Yes, these ids will be valid when creating an account.

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

I executed the following query

Account Clientacc =new Account(LastName='ABC',PersonMailingStreet='Street',PersonMailingCity='City',

                    PersonMailingState='state',PersonMailingCountry='country',

                    PersonMailingPostalCode='123456',FirstName='DEF',Retirement_Age__c=55,

                    Estimated_Life_Expectancy__c=85,PersonBirthdate=system.today(), RecordTypeId='012N000000007aIIAQ');

 

      upsert Clientacc;

 

The RecordType Id is the one which i had got from my query

Still it is giving me the following error on my system log

 

16:40:55:039 FATAL_ERROR System.DmlException: Upsert failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Account: bad field names on insert/update call: FirstName, LastName: [FirstName, LastName]

bob_buzzardbob_buzzard

You appear to be specifying the contact specific fields when creating the account.  A person account under the hood is a combination of account and contact, so I reckon you'll have to create them separately.

 

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

My Account Object has Fields as First Name and Last Name.

Since they are staandard fields in Account dey are being used as it is.........

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

I tried to insert values with out these fields but its not accepting them query

bob_buzzardbob_buzzard

Your account can't have the fields FirstName, LastName as the insert is failing because those field names aren't recognised.

 

While you may see these fields in the page layout, I think you'll find that they are stored on a contact record rather than an account.

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

I can see both the fields in the Standard field list of Account and Contact.

I that can how can i insert the vaues in Account firstname and Lastname

sangeeta.marathe@Futurewise.cosangeeta.marathe@Futurewise.co

I am not able to insert values into my Account Object... Is it some where Related to the fact that i am working with person account

 

bob_buzzardbob_buzzard

I've looked back over some old person account code, and where I've created a person account for test purposes I've had to specify a record type where the IsPersonType field is true.  It may be that is what opens up the FirstName, LastName fields.

This was selected as the best answer