0

Hello I want to relate a custom object to OpportunityLineItem but OpportunityLineItem cannot have a lookup field pointing to it. I have a custom text field referencing the OpportunityLineItem Id - I want to use this to display the new child object in the OpportunityLineItem page(s).

I can get the id for the Custom Obj, but I don't know how to display this object, page or its fields in a visualforce page. Class:

public class LeafGrabber {


    public OpportunityLineItem record {get;set;}
    public ApexPages.StandardController sc;
    public string OLIid {get;set;}
    public string LeafId {get;set;}

    public LeafGrabber(ApexPages.StandardController controller) {

        record = (OpportunityLineItem)controller.getRecord();
        sc = controller;
        OLIid=record.id;
        LeafId = [select id from CUstom_obj__c where ProductId__c =: OLIid Limit 1].id;


    }

    public PageReference ShowLeaf (){
       PageReference LeafUrl = new PageReference('/'+LeafId );
        return LeafUrl;
            }

}

Page

<apex:page standardController="OpportunityLineItem" title="LeafOnLineItem" extensions="leafgrabber">

   <apex:outputField value="{!OpportunityLineItem.id}" rendered="true"/>

{!LeafId}


</apex:page>

1 Answer 1

1

So the easy way would be to store the object as a variable in your controller. All you will need to do is then render the fields that you want for this object onto your layout like below:

Controller:

    public class LeafGrabber {


        public OpportunityLineItem record       { get; set; }
        public ApexPages.StandardController sc  { get; set; }
        public String lineItemId                { get; set; }
        public Custom_obj__c leaf               { get; set; }

        public LeafGrabber( ApexPages.StandardController controller ) {

            record = ( OpportunityLineItem )controller.getRecord();
            sc = controller;
            lineItemId = controller.getId();

            leaf = getLeafObjectByProductId( lineItemId );

            // if it is null, we can instantiate it just to be sure we don't get a null pointer
            if( leaf == null ){ leaf = new Custom_obj__c(); }

        }

        /**
        *
        *   Get your leaf object by product Id 
        *
        *
        **/
        public Custom_obj__c getLeafObjectByProductId( String productId ){

            Map<String, Schema.SObjectField> leafFields = Custom_obj__c.getSObjectType().getDescribe().fields.getMap();
            String queryString = 'SELECT ' + String.join( new List<String>( leafFields.keySet() ), ',' ) + ' FROM Custom_obj__c WHERE ProductId__c = \'' + productId + '\' LIMIT 1';
            List<SObject> temp;

            try{
                temp = Database.query( queryString );
            }
            catch( Exception ex ){
                System.debug( 'Error fetching Custom_obj__c: ' + ex.getMessage() );
            }

            if( temp != null && !temp.isEmpty() ){
                return ( Custom_obj__c )temp[ 0 ];
            }

            return null;
        }


        public PageReference showLeaf (){
            PageReference result = null;

            if( leaf != null ){
                result = new PageReference( '/' + leaf.Id );
                result.setRedirect( true );
            }
            else {
                ApexPages.addMessage( new ApexPages.Message( ApexPages.Severity.ERROR, 'Leaf not found.' ));
            }

            return result;
        }

    }

Page:

    <apex:page standardController="OpportunityLineItem" title="Leaf On Line Item" extensions="LeafGrabber">

        <apex:pageBlock mode="mainDetail" title="{!record.Name}">
            <apex:pageBlockSection columns="2" collapsible="false" title="Product Information">
                <apex:outputField value="{!OpportunityLineItem.Opportunity.Name}" />
                <apex:outputField value="{!OpportunityLineItem.Name}" />
                <apex:outputField value="{!OpportunityLineItem.ListPrice}" />
                <apex:outputField value="{!OpportunityLineItem.Description}" />
            </apex:pageBlockSection>
            <apex:pageBlockSection collapsible="false" columns="2" title="Leaf">            
                <apex:outputField value="{!leaf.Name}" />
                <apex:outputField value="{!leaf.ProductId__c}" />
            </apex:pageBlockSection>

        </apex:pageBlock>


    </apex:page>
2
  • Thanks, got it working. Is there anyway to pull in the full page of the related customObj without coding every single field AND all the child objects (with more queries)... like <apex:detail relatedList="true" />
    – jaw999
    Commented Jul 27, 2018 at 14:14
  • 1
    got that with <apex:detail inlineEdit="true" subject="{!leaf.id}" relatedList="false" title="false"/>
    – jaw999
    Commented Jul 27, 2018 at 17:51

You must log in to answer this question.

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