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
Mario CMario C 

Component which shows Opportunity Product Details on Case

I managed to create a component which shows a list of opportunitylineitems. it works correctly when the component is placed on an opportunity page. What I now need to do is showing the OpportunityLineItems on a Case page, so I'd need the compinent to refer to the case lookup relationship (Opportunity__c) to show the opportunitylineitems.

Here is what I've done:

APEX Controller
public class MyOLIController {

    @AuraEnabled
    public static List<OpportunityLineItem> getProduct(List<Id> opportunityIds){
        List<OpportunityLineItem> productList = [SELECT Id, Property__c, OpportunityId, Start_Date_Time__c,End_Date_Time__c,Status__c 
                                                  FROM OpportunityLineItem WHERE OpportunityId in :opportunityIds];
        return productList;
    }
    
}

Component
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" controller = "MyOLIController" access="global" >
    
    <aura:handler name="init" action="{!c.getProductList}" value="{!this}" />
    <aura:attribute name="productList" type="List" />
    <lightning:card title="Line Items">
        <p class="slds-p-horizontal_small">
        <aura:iteration items="{!v.productList}" var="product">
            
            <lightning:recordViewForm recordId="{!product.Id}" objectApiName="OpportunityLineItem" > 
                <div class="slds-box slds=theme_default">
                <label for="HotelVilla">Hotel / Villa</label>
                <lightning:outputField aura:id="HotelVilla" fieldName="Property__c" variant="label-hidden"/>
                <label for="CheckIn">Check In Date</label>
                <lightning:outputField aura:id="CheckIn" fieldName="Start_Date_Time__c" variant="label-hidden"/>
                <label for="CheckOut">Check Out Date</label>
                <lightning:outputField aura:id="CheckOut" fieldName="End_Date_Time__c" variant="label-hidden" />
                <label for="BookingStatus">Booking Status</label>
                <lightning:outputField aura:id="BookingStatus" fieldName="Status__c" variant="label-hidden" />
                    </div>
            </lightning:recordViewForm>
            <br/>
            </aura:iteration>
        	</p>
     </lightning:card>
</aura:component>

Helper
({
	fetchProducts : function(component, event, helper) {
        var action = component.get("c.getProduct");
		var opportunityId = component.get("v.recordId");
        action.setParams({
            opportunityIds: opportunityId
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            //State can be success, error or incomplete
            if(state == 'SUCCESS'){
                var productList = response.getReturnValue();
                console.log(productList);
                component.set("v.productList",productList);
            }
            else{
                alert('Error in getting data');
            }
        });
	
        $A.enqueueAction(action);
    }
   
})

 
Best Answer chosen by Mario C
Khan AnasKhan Anas (Salesforce Developers) 
Hi Mario,

I trust you are doing very well.

If you have an Opportunity lookup field on Case object, you can easily complete your requirement. You just have to case related to opportunity and then you can fetch OpportunityLineItem using that case's opportunity. After that, add the component to Case record page using Lightning App Builder.

You need to make some modification to the Apex Controller:
public class MyOLIController {
    
    @AuraEnabled
    public static List<OpportunityLineItem> getProduct(List<Id> opportunityIds){
        Case cs = [Select Opportunity__c from Case where Id=:opportunityIds];
        List<OpportunityLineItem> productList = [SELECT Id, Property__c, OpportunityId, Start_Date_Time__c,End_Date_Time__c,Status__c  
                                                 FROM OpportunityLineItem WHERE OpportunityId=:cs.Opportunity__c];
        return productList;
    }
}


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Mario,

I trust you are doing very well.

If you have an Opportunity lookup field on Case object, you can easily complete your requirement. You just have to case related to opportunity and then you can fetch OpportunityLineItem using that case's opportunity. After that, add the component to Case record page using Lightning App Builder.

You need to make some modification to the Apex Controller:
public class MyOLIController {
    
    @AuraEnabled
    public static List<OpportunityLineItem> getProduct(List<Id> opportunityIds){
        Case cs = [Select Opportunity__c from Case where Id=:opportunityIds];
        List<OpportunityLineItem> productList = [SELECT Id, Property__c, OpportunityId, Start_Date_Time__c,End_Date_Time__c,Status__c  
                                                 FROM OpportunityLineItem WHERE OpportunityId=:cs.Opportunity__c];
        return productList;
    }
}


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Mario CMario C
Thanks Khan. That worked perfectly!!