0

Im trying to set the RecordTypeId of a contact to a certain value if the contact was created by a specific user. Im getting the error:

A value cannot be stored to RecordTypeId in type Contact.

How can i set the RecordType without getting an error?

public void OnBeforeInsert(Contact[] newContacts){

    ID id=[SELECT Id FROM User WHERE Name='Niki Müller'].ID;
    ID B2C=[Select RecordTypeId FROM Contact WHERE Name='Aamer Ayoob' limit 1].ID;

    for(Contact newContact : newContacts){
        if(newContact.createdById==id){                
            contact.RecordTypeId=B2C;                
            contact.AccountId=contact.Email;
        }
    }
}
2
  • ID B2C=[Select RecordTypeId FROM Contact WHERE Name='Aamer Ayoob' limit 1].ID; should be changed to ID B2C=[Select RecordTypeId FROM Contact WHERE Name='Aamer Ayoob' limit 1].RecordTypeId; Commented Oct 17, 2018 at 10:03
  • But then i get the error: Invalid type: RecordTypeId Commented Oct 17, 2018 at 10:12

2 Answers 2

2

ID B2C=[Select RecordTypeId FROM Contact WHERE Name='Aamer Ayoob' limit 1].ID;

here B2C will give you ContactID and not RecordTypeId.

Check below query to get RecordTypeId for Contact object -

ID B2C=[SELECT Id, Name, SobjectType FROM RecordType WHERE SobjectType = 'Contact' AND Name = '<RecordTypeName>' limit 1].ID

Instead of querying on Contact you can query on RecordType. Please refer below link- https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_recordtype.htm

1

Notice you are pulling Contact.Id and trying to save it elsewhere to Contact.RecordTypeId. These fields reference objects which are not compatible:

ID B2C = [Select RecordTypeId FROM Contact WHERE Name='Aamer Ayoob' limit 1].ID;
//                                                   should be RecordTypeId ^^^
... contact.RecordTypeId = B2C;

So your B2C attribute should instead pull RecordTypeId from some other Contact.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .