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
kkaalkkaal 

Reference to a lookup field

I wrote a visualforce page with controller on a custom object.

 

This custom object has a lookup field (Must field - Client__c), which needs a reference to a Contact.

 

Now I write my test object.

 

I create a Contact object:

 

Contact contact = new Contact(FirstName='FN_test', LastName='LN_test');

 

 

I create my custom object:

 

MyCustom myCustom = new MyCustom(Name='CustName');

Now I want to link the 2:

 

myCustom.Client__c = contact [.Id] [.Name]

 When I just use the object itself object creates a compiler Error.

 

When I use the name, there is no exception, But the relation does not work.

 

When I use the Id, I get this error form my test:

 

 

 

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Client: id value of incorrect type: 003T000000SNDhjIAH: [Client__c]

 Any idea why that is?

 

 

 

hisrinuhisrinu

If you use the below statement then it should work fine if the client looup is an lookup to contact else you will get an error.

 

 

myCustom.Client__c = contact.Id;