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
jcohanallroundsjcohanallrounds 

Debugging help and question about setting the value of a Lookup Relation

Is there a way to get the values of variables in apex either with an email being sent with the values or is there a way to get the values within Eclipse?

 

Also I have question about setting values in Lookup Relational widgets

 

in my Unit test I have the statment

 

 Contact newContact = new Contact(lastname = 'Schwartz', firstname='Jamie', EIN_Tax_id__c = '1111', Investor_Type__c = 'Director' );

 

and I get the error message
System.StringException: Invalid id: Director 

 

Investor_Type__c is a Lookup Relation widget in the Contact object that links to the the InvestorType object and there is a value 'Director' in the object. 

 

Can you set the value of a Lookup Relational field via a new() in Apex?

 

 

slaneslane

When you say get the values of variables -- do you mean for debugging purposes? There are several ways to see debugging info, including the debug logs in the Web UI and the Apex logs for code executed anonymously from the Eclipse IDE.

 

As to your other question, you can set the value of a lookup field, but you have to pass it an ID. I'm not sure what you mean when you say 'there is a value 'Director' in the object' -- you mean you want to link to an Investor_Type__c record where the value of some field is 'Director'? (Sounds like you're normalizing your investor types, which makes sense). If so, you can't really specify it like that, you need to somehow grab the Salesforce ID of that Investor_Type__c record and use that.

 

So it would look more like:

 

 Contact newContact = new Contact(lastname = 'Schwartz', firstname='Jamie', EIN_Tax_id__c = '1111', Investor_Type__c = '01r40000000ECN5 ' );

 

Hope that helps :-)

 

jcohanallroundsjcohanallrounds

Thanks Steve for responding.

 

Yes I mean for debugging is there a way to see the values of variable? If so do you see the values from system.debug() function or some other way?

 

With regard to the second question, I do mean what you thought.  There is a link to an InvestorType__c object through a lookup relation and one of the records has a value of 'Director' and I want to set it via a New() (i.e. I want to set the value of the Lookup Relation to 'Director' or "Individual Investor" etc.) .  So what you are say is I must pass in the Record Id corresponding to 'Director' record in object InvestorType__c, is that correct?

slaneslane

So far as I know, there's no integrated debugger -- System.debug() is pretty much it. You'll want to become familiar with the Debug Logs feature in the Web UI. You can also get debug output by running code from the Execute Anonymous window in the Eclipse IDE.

 

As to your lookup question, your statement is correct. If you can spare the SOQL query you can do something like

 

 

Investor__Type__c = [ select Id from Investor_Type__c where Description = 'Director'];

 

 

I'm sure I'm munging your variable names but you get the idea. Of course it's always good not to spend a query on just a single operation like this, to stay inside of governor limits, but you can best judge how to get your hands on the Id.

 

 

jcohanallroundsjcohanallrounds

How do I reference the 'RecordId' when I am unit testing.  Per your earlier comment I trying to get the record of the lookup relation in object InvestorType where Investor = 'Director'.  I have the following code:

 

List<Investor_Type__c> investorType = new List<Investor_Type__c>() ;

 

investorType = [select Investor__c from Investor_Type__c where Investor__c = 'Director'];

 

 newContact.add(new Contact(lastname = 'Schwartz', firstname='Bob', EIN_Tax_id__c = '1111', Investor_Type__c = investorType.InvestorTypeName) );

 

I get the error message "Save error: Initial term of field expression must be a concrete SOjbect: List:SOBJECT: Investor_Type__c" 

 

but InvestorTypeName is the name of the recordId in object InvestorType

 

How do I reference the recordId for object Investor_Type__c?

slaneslane

Couple of things here. One, it' s better not to rely on database data when unit testing. The unit test will fail if the needed record isn't there. So your tests should create all the data they need. Each test runs in its own transaction, so if I were you I'd first delete the Director record, just in case it's already there, then create it to make SURE it's there. That will all get undone when the test completes.

 

Assuming the Director record is in the database now: the problem in your code is that investorType is a List of sObjects. I imagine you're assuming there will be only one match, i.e. that your query will only pull back one record. If so, you want to grab the first entry in the list. So you can say:

 

investorType = [select Investor__c from Investor_Type__c where Investor__c = 'Director']; firstInvestorTypeMatch = investorType[0]; newContact.add(new Contact(lastname = 'Schwartz', firstname='Bob', EIN_Tax_id__c = '1111', Investor_Type__c = firstInvestorTypeMatch ) );

 OF course,you want some error checking to make sure it pulled back at least one record.

 

Hope that helps.