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
hector.asp2hector.asp2 

RecordType only saves Account

Hi ,

 

I have a custom object in which I save the record type (it can be accounts, contacts, leads).

Here is my code

 

 

String obj='Contacts'; 
RecordType rt = [select id, name from recordtype where name =: obj limit 1];
System.Debug(rt.name); //Gives Contacts
savedGrp.RecordType = rt;
insert savedGrp;  

it always saves accounts...

 

 

 

 

Please help me with this.

 

Thanks

Hector.

Best Answer chosen by Admin (Salesforce Developers) 
garybgaryb

What happens if you try changing this line:

 

savedGrp.RecordType = rt;

 

to this:

 

savedGrp.RecordTypeID = rt.ID;

 

I don't see why the latter should work and the former shouldn't, but the latter is how I set Record Types on objects.

 

I think a problem is occuring when you get/set the record type and so the default record type is applied (is Accounts the standard record type for the profile & object).

 

All Answers

bob_buzzardbob_buzzard

Can you post the rest of your code - e.g. what is savedGrp declared as?

Pradeep_NavatarPradeep_Navatar

If you want to get recordtype for specific object like account,object then in this case you should add sobjecttype = 'contact' in SOQL Query :

 

RecordType rtype = [Select r.SobjectType, r.Name, r.Id From RecordType r where name='individual' and SobjectType='contact']

 

Hope this helps.

hector.asp2hector.asp2

Hi,

 

I tried to fire different queries and see what type of record types do I have

 

select id, name, sobjecttype from recordtype

gave me result

012A0000000gIaeIAE->Contacts->Stratus_Duplicate_Group__c
012A0000000gIajIAE->Leads->Stratus_Duplicate_Group__c
012A0000000gIaPIAU->Accounts->Stratus_Duplicate_Group__c

 

Then I did this

 

 

RecordType rt = [Select id, name, sobjecttype  From RecordType where sobjecttype  = 'Stratus_Duplicate_Group__c' and name = 'Contacts' and isActive = true limit 1];

Stratus_Duplicate_Group__c savedGrp = new Stratus_Duplicate_Group__c();
savedGrp.Group_Match_Name__c = sKey;
savedGrp.RecordType = rt;
insert savedGrp;

Always stores Accounts

 

 

 

Then I tried

 

RecordType rr = [Select id, name, sobjecttype  From RecordType where sobjecttype  = 'Stratus_Duplicate_Group__c' and name = 'Contacts' and isActive = true limit 1];
            RecordType rt = new RecordType();
;
RecordType rt = new RecordType(); rt.id = rr.id; ....... Stratus_Duplicate_Group__c savedGrp = new Stratus_Duplicate_Group__c(); savedGrp.Group_Match_Name__c = sKey; savedGrp.RecordType = rr; insert savedGrp; Again always stores Accounts

 

 

Please guide

 

 

Thanks. 

Hector

bob_buzzardbob_buzzard

What do you mean by "it always stores accounts"?  Have you checked the objects in the database and found that the id doesn't match the id you specified when you created the record?  Or are you experiencing problems elsewhere that lead you to this conclusion?

 

 

hector.asp2hector.asp2

Hi,

 

Yes , I did checked the IDs and found that all for all cases only accounts id (the Master record type is Account ), is getting saved in recordtypeid for my custom object, though system.debug prints correct id before I do the insert.

 

Regards

Bikram...

 

garybgaryb

What happens if you try changing this line:

 

savedGrp.RecordType = rt;

 

to this:

 

savedGrp.RecordTypeID = rt.ID;

 

I don't see why the latter should work and the former shouldn't, but the latter is how I set Record Types on objects.

 

I think a problem is occuring when you get/set the record type and so the default record type is applied (is Accounts the standard record type for the profile & object).

 

This was selected as the best answer
bob_buzzardbob_buzzard

Does the profile running the code have access to the different record types?  If not, it may be that account is the default and you are picking that up.