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
Ron Mccrerey 20Ron Mccrerey 20 

Product2 SOQL Query

public static String getProjectNumber( String id ) {
List<Opportunity> values = new List<Opportunity>();
values =
// return
[SELECT Id, Name, (SELECT PricebookEntry.Product2.Serial_Number__c,
PricebookEntry.Product2.Stock_ID__c,
PricebookEntry.Product2.RecordType.Name
FROM OpportunityLineItems
where PricebookEntry.Product2.RecordType.Name = 'Service Body' )
FROM Opportunity WHERE Id = :id limit 1];
return values.PricebookEntry.Product2.Serial_Number__c;
}

Get error Variable does not exist: PricebookEntry

Here is the data:
PricebookEntry.Product2.Serial_Number__c             STETEST
PricebookEntry.Product2.Stock_ID__c                             PricebookEntry.Product2.RecordType.Name             Service Body
Best Answer chosen by Ron Mccrerey 20
Deepak GulianDeepak Gulian

Hi Ron,

Following are the 2 approaches to get the desired result and you can modify the code accordingly further.

To access data from "List" either use a loop or index to get the value of it.

//APPROACH 1
public static String getProjectNumber( String id ) {
    String serialNum = '';

    List<Opportunity> values = [SELECT Id, Name, 
              (SELECT PricebookEntry.Product2.Serial_Number__c, PricebookEntry.Product2.Stock_ID__c,
               PricebookEntry.Product2.RecordType.Name FROM OpportunityLineItems 
               WHERE PricebookEntry.Product2.RecordType.Name = 'Service Body') 
              FROM Opportunity WHERE Id = :id LIMIT 1];

    if(!values.isEmpty()) {      
        for(OpportunityLineItem oli : values[0].OpportunityLineItems) {
            serialNum = oli.PricebookEntry.Product2.Serial_Number__c;
            break;
        }
    }

    return serialNum;
}

//APPROACH 2
public static String getProjectNumber( String id ) {
    List<Opportunity> values = new List<Opportunity>();
    values = [SELECT Id, Name, 
              (SELECT PricebookEntry.Product2.Serial_Number__c, PricebookEntry.Product2.Stock_ID__c,
               PricebookEntry.Product2.RecordType.Name FROM OpportunityLineItems 
               WHERE PricebookEntry.Product2.RecordType.Name = 'Service Body') 
              FROM Opportunity WHERE Id = :id LIMIT 1];

    return values[0].OpportunityLineItems[0].PricebookEntry.Product2.Serial_Number__c;
}

Thanks
DG

All Answers

Maharajan CMaharajan C
Try like below:

[SELECT Id, Name, (SELECT .Product2.Serial_Number__c,
Product2.Stock_ID__c,
Product2.RecordType.Name
FROM OpportunityLineItems
where Product2.RecordType.Name = 'Service Body' )
FROM Opportunity WHERE Id = :id limit 1];

return values.Product2.Serial_Number__c;

Thanks,
Maharajan.C
Deepak GulianDeepak Gulian

Hi Ron,

Following are the 2 approaches to get the desired result and you can modify the code accordingly further.

To access data from "List" either use a loop or index to get the value of it.

//APPROACH 1
public static String getProjectNumber( String id ) {
    String serialNum = '';

    List<Opportunity> values = [SELECT Id, Name, 
              (SELECT PricebookEntry.Product2.Serial_Number__c, PricebookEntry.Product2.Stock_ID__c,
               PricebookEntry.Product2.RecordType.Name FROM OpportunityLineItems 
               WHERE PricebookEntry.Product2.RecordType.Name = 'Service Body') 
              FROM Opportunity WHERE Id = :id LIMIT 1];

    if(!values.isEmpty()) {      
        for(OpportunityLineItem oli : values[0].OpportunityLineItems) {
            serialNum = oli.PricebookEntry.Product2.Serial_Number__c;
            break;
        }
    }

    return serialNum;
}

//APPROACH 2
public static String getProjectNumber( String id ) {
    List<Opportunity> values = new List<Opportunity>();
    values = [SELECT Id, Name, 
              (SELECT PricebookEntry.Product2.Serial_Number__c, PricebookEntry.Product2.Stock_ID__c,
               PricebookEntry.Product2.RecordType.Name FROM OpportunityLineItems 
               WHERE PricebookEntry.Product2.RecordType.Name = 'Service Body') 
              FROM Opportunity WHERE Id = :id LIMIT 1];

    return values[0].OpportunityLineItems[0].PricebookEntry.Product2.Serial_Number__c;
}

Thanks
DG

This was selected as the best answer
Ron Mccrerey 20Ron Mccrerey 20
Thank you Deepak. Just what I needed and I appreciate the clear concise example and quickness of your response.