0

I'm trying to reference the product2 name from an opportunity record. However it's giving me this error and not allowing me to save the code. "Invalid foreign key relationship: Opportunity.opportunitylineitem"

How should I be referencing this field?

global class OpOwnerOldService_Scheduled Implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
        sendEmailtoOppOwner();
    }

    public void sendEmailtoOppOwner()
    {
        List<Opportunity> listOpportunity = new List<Opportunity>();
        listOpportunity = [SELECT Id, OwnerId FROM Opportunity WHERE Id In (SELECT OpportunityId FROM OpportunityLineItem WHERE Product2.Make_unavailable_for_opps_and_proposals__c = TRUE)];

        for(Opportunity opp : listOpportunity)
        {
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            mail.setTargetObjectId(opp.OwnerId);
            mail.setReplyTo('glenn.daly@live.com');
            mail.setSenderDisplayName('Salesforce Support');
            mail.setSubject('Historic Service attached to current opportunity : ' + opp.opportunitylineitem.Product2.Name );
            mail.setPlainTextBody(opportunity.Id +' has been created.');
            mail.setSaveAsActivity(false);

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }

        update listOpportunity;
    }

}

3

You cannot access the child record using a dot operator, as there can be multiple child records the dot operator will never find which record to access even if there is only one record. Instead you do a inner query and use it.

listOpportunity = [SELECT Id, OwnerId,(SELECT Product2.Name FROM OpportunityLineItems LIMIT 1) FROM Opportunity WHERE Id In (SELECT OpportunityId FROM OpportunityLineItem WHERE Product2.Make_unavailable_for_opps_and_proposals__c = TRUE)];


 for(Opportunity opp : listOpportunity)
        {
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            mail.setTargetObjectId(opp.OwnerId);
            mail.setReplyTo('glenn.daly@live.com');
            mail.setSenderDisplayName('Salesforce Support');
 for(opportunitylineitem oppLineItem :opp.opportunitylineitems)
            mail.setSubject('Historic Service attached to current opportunity : ' + oppLineItem.Name );
}
            mail.setPlainTextBody(opportunity.Id +' has been created.');
            mail.setSaveAsActivity(false);

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }

Please try this

  • Thanks @maruf but I received the following error "Didn't understand relationship 'OpportunityLineItem' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names." – Glenn Daly Oct 10 '16 at 11:48
  • Yeah! My bad there will be "OpportunityLineItems" in the inner query. I have updated the query please try now – Maruf Bagwan Oct 10 '16 at 12:24
  • The only change I needed to make to the code was adding the Id field when querying the opportunity line items, apart from that it worked perfectly. Thanks for your help! – Glenn Daly Oct 10 '16 at 13:53
  • That was good to know. Always happy to help :) – Maruf Bagwan Oct 10 '16 at 17:15

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.