2

I am trying to write the following piece of code

Contact aContact=[select FirstName, LastName, UserId__r.id,id,email from contact where id=:ContactId limit 1];
                             if(aContact.UserId__r.id == null){
                                User u = new User();
                                u.FirstName = aContact.FirstName;
                                u.LastName = aContact.LastName;
                                u.Email = aContact.Email;
                                u.Contact = aContact.Id;                               
                                aContact.UserId__r.id = u.id;                               
                            }

Here UserId__c is a custom field in Contact which stores the UserId. This code is showing me an error

Illegal assignment from Id to Contact

Please tell me where am I going wrong

2

Issue is in here

u.Contact = aContact.Id;

You have to change this into

u.ContactId = aContact.Id;

But I've no idea about below line

aContact.UserId__r.id = u.id;

as per your code snippet, u.Id is not available at this line since the user instance has not been inserted yet.

0

You have assigned Id to an object, you need to assign it to the Id field. It should be:

u.ContactId = aContact.Id;
0

According to your error message I would say that you problem is on this line:

u.Contact = aContact.Id;

You should use ContactId instead of just Contact here

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.