I'm receiving the following error:

Illegal assignment from List<OpportunityContactRole> to List<String>

When I try to query from OpportunityContactRole and store it into a new list. I'm pretty new to apex, so I'm kind of confused where I'm going wrong. Here's my code:

public class testOCR {

    public static void testOCRmethod(){

        // Create a list that stores the query data        
        List<string> ContactList = [SELECT OpportunityId,Contact.Referral_ID__c FROM OpportunityContactRole WHERE ContactId IN (SELECT Id FROM Contact WHERE Referral_ID__c != null) AND OpportunityId IN (SELECT Id FROM Opportunity WHERE Referral_ID__c = null) AND CreatedDate <= TODAY];

        Integer ListSize = ContactList.size();

        if( ListSize < 1 ) {

            system.debug('List is too small');

        } else {

            // list is a proper size
            system.debug('List is big enough');

            for(string x : ContactList) {






Originally I had it set as List<OpportunityContactRole> which worked, sort of. The problem was that when I would check my debug log, it wasn't saving the Referral_ID__c value, which is why I was trying to change it to a string instead.

Any ideas where I'm going wrong?

2 Answers 2


You should be able to iterate the list and add the field to a separate collection:

List<String> referralIds = new List<String>();
for (OpportunityContactRole ocr : [/*query*/])

I'd like to post this as a comment, but I think it became too big for it.

I think that your real question might be Why can't I see some fields (Referral_ID__c) with system.debug(...).

The reason why you don't see Referral_ID__c in your logs it's because your query is getting this field from a parent relationship (Right Outer Join). If you directly debug your record results:

for(OpportunityContactRole x : ContactList) {

the debug log will only display your sObject's own fields, not the ones that come from relationships (Contact.Referral_ID__c), but that doesn't mean that such field was not retrieved inside your records results, therefore if you do something like:

for(OpportunityContactRole x : ContactList) {

then you'll see the value that you're looking for.

Going back to original question's title, @Adrian 's answer is the fix for it.

You must log in to answer this question.

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