How To: Create a Quick Action Lightning Component for Super Clone Pro
January 14, 2019
How To: Add CSS Style to the Clone, Edit, and Copy pages
June 2, 2019

How To: Show a list of records to select from with the Super Clone Pro Copy page

The Super Clone Pro copy page allows you to copy related lists from one parent record to another parent record. The page has lookup field that lets you select the new parent record. This uses Salesforce’s default popup window to select a new record. This default search may show too many records.

The following code is an example of how to display a filtered list of possible records to choose from. The page allows the user to click on the row to make their selection.

The required custom components include a Visualforce page, Apex controller extension, and Apex test class. The Apex controller extension contains logic to select the appropriate records. The Visualforce page displays the list, and it uses jQuery to set the value for the lookup field in the Super Clone Pro component.

oppCopyController – This is the controller extension that will be modified for your object type and filtering requirements.

public with sharing class oppCopyController {
    public oppCopyController(ApexPages.StandardController std) {}

    /**
    * return a list of opportunities for the selection table
    */
    public list<Opportunity> getOppList() {
        list<Opportunity> oppList;

        String rid = ApexPages.currentPage().getParameters().get('rid');
        if (String.isNotBlank(rid)) {
            // get the current opportunity to find the Account Id for the next select statement
            list<Opportunity> currentOppList = [SELECT AccountId 
                                                FROM Opportunity 
                                                WHERE Id = :rid LIMIT 1];
            
            // get Opportunities related to the same account, and exclude the current opportunity
            oppList = [SELECT Name, CloseDate
                       FROM Opportunity
                       WHERE AccountId = :currentOppList[0].AccountId
                             and Id != :rid];
        } else {
            // initialize so it's not null
            oppList = new list<Opportunity>();
        }
        
        return oppList;
    }
}

oppCopyControllerTest – This is the test class for the controller extension. It wasn’t built out because each environment will have unique data creation requirements. However, the basic concept of creating the data, instantiating the controller, and testing the list method will be common.

@isTest
public with sharing class oppCopyControllerTest {
    @TestSetup
    static void makeData(){
        // create test data
    }

    @isTest
    static void testListCreation(){
        // query for the parent record
        Opportunity opp = [SELECT Id FROM Opportunity LIMIT 1];

        // instantiate the page controller 
        ApexPages.currentPage().getParameters().put('rid', opp.Id);
        ApexPages.StandardController std = new ApexPages.StandardController(opp);
        oppCopyController con = new oppCopyController (std);

        // confirm records were retruned for the list
        system.assertEquals(2, con.getOppList().size());
    }
}

oppCopy – This is the Visualforce page that contains the list, jQuery logic, and Super Clone Pro copy component. The page loads the list of related records, and then the standard Lookup section is hidden. The list is monitored for a selection, and then the values for the lookup are set using Javascript.

<apex:page standardController="Opportunity" extensions="oppCopyController" lightningStylesheets="true">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <style>
        .col-name, .show-link {
             font-weight: bold;
        }
        .radio-cell {
            text-align: center;
        }
        .lookup-hide {
            display:none;
        }
        .lookup-show {
            display:block;
        }
    </style>

    <div id="select-table">
        <apex:pageBlock >
            <apex:pageBlockSection columns="1" collapsible="false" title="Select a related Opportunity to copy related lists to:">
                <apex:pageBlockTable var="opp" value="{!oppList}" styleClass="selectTable">
                    <apex:column styleClass="radio-cell">
                        <input class="radio-sel" type="radio" name="select" />
                    </apex:column>
                    <apex:column styleClass="col-name" value="{!opp.Name}" html-data-id="{!opp.Id}" />
                    <apex:column value="{!opp.CloseDate}" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:facet name="footer">
                <a href="#" class="show-link">Show Lookup</a>
            </apex:facet>
        </apex:pageBlock>
    </div>

    <lcrm_scp:ScpCopy />

    <script type="text/javascript">
        j$ = jQuery.noConflict();

        j$(window).on("load", function() {
            // set lookup field data for a selected table row
            j$("#select-table .selectTable tbody tr").on("click", function(){
                // retrieve the selected record Id and name
                let recName = j$(this).find(".col-name").first().text();
                let recId = j$(this).find(".col-name").first().data('id');
                if (!recId) {
                    alert('Record Id could not be found.');
                    return;
                }
                if (!recName) {
                    alert('Record Name could not be found.');
                    return;
                }

                // set values of hidden name and id fields on the page
                j$("input[id$='_lkid']").first().val(recId);
                j$("input[id$='_lkold']").first().val(recName);

                // set value of visible lookup text field
                j$(".lookupInput").find("input").first().val(recName);

                // set value of radio button when click was used to select the record
                j$(this).find("input.radio-sel").prop("checked", true);
            });

            // show the lookup field on 'show lookup' click
            j$(".show-link").on('click', function(event) {
                j$(this).addClass("is-clicked");
                j$(".lookup-hide").addClass("lookup-show");
                j$(".lookup-hide").removeClass("lookup-hide");
            });
        });

        // check if the Show Lookup was clicked, and set the lookup section visibility 
        // use interval check because rerender will re-display the section
        setInterval(checkLookupShowHide, 500);
        function checkLookupShowHide () {
            // get the lookup section div
            let crlDiv = j$("h3:contains('Copy Related Lists')").parent().parent().parent().parent();
            console.log(crlDiv);
            if (crlDiv && !crlDiv.hasClass("lookup-show") && !crlDiv.hasClass("lookup-hide")) {
                // check if the show link was clicked
                if (j$(".show-link").hasClass("is-clicked")) {
                    crlDiv.addClass("lookup-show");
                } else {
                    crlDiv.addClass("lookup-hide");
                }
            }
        }
    </script>
</apex:page>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.