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
Andy Kallio 7Andy Kallio 7 

Admin: trying to extend lightning component to other sobjects

Hello, I've made some lightning components and vfpage that will allow my users to select select, preview and save pdf templates from the Quote object. I now have the need to use the same functionality on other objects in my org. However, I don't quite see how I can do it without making new lightning components. The main challenge is in how to use the <force:recordData> dynamically....I think that is what I want to do. For example, if the component could recognize that it was on a Quote or an Invoice or some other custom object then my server side controller could get the appropriate template options and the rest will be easy. The part that I don't see is how to make force:recordData work with multiple objects. 

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId" access="global" controller="pdfDI_SelectTemplate2_Controller">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="selectedTemplate" type="String"/>
    <aura:attribute name="templateOptions" type="List"/>
    <aura:attribute name="record_Q" type="Quote"/> 
    <force:recordData aura:id="recordLoader_Q"
        recordId="{!v.recordId}"
        fields="Name"
        targetFields="{!v.record_Q}"              
    />
    <lightning:layout multipleRows="false">
        <lightning:layoutItem size="6">
        	<div class="row">
        		<!--<p class="title">Select Template:</p>-->
    			<ui:inputSelect class="single" aura:id="InputSelectSingle" change="{!c.onSingleSelectChange}">
        			<option value="">...Select a Template...</option>
            		<aura:iteration items="{!v.templateOptions}" var="t_opt" indexVar="key">
                		<ui:inputSelectOption text="{!t_opt.value}" label="{!t_opt.key}"/>
            		</aura:iteration>
        		</ui:inputSelect>  
    		</div>
        </lightning:layoutItem>
    	<lightning:layoutItem size="6">
        <div class="slds-p-left_large">
    	 	<lightning:button label="Save PDF" title="Save PDF" onclick="{! c.savePDF }"/>
    	</div>
        </lightning:layoutItem>  
    </lightning:layout>
    
	<div class="slds-p-top_large">
        <p>PDF Preview:</p>
    	<c:pdfDI_DisplayTemplate selectedTemplate="{!v.selectedTemplate}" recordId2="{!v.recordId}"/>
    </div>  
</aura:component>
 
({
	
        // Load template options from Salesforce
    doInit: function(cmp, event, helper) {
        
        // Create the action
        var action = cmp.get("c.getTemplateOptions");
        // Add callback behavior for when response is received
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var custs = [];
                var conts = response.getReturnValue();
                for(var key in conts) {
                    custs.push({value:conts[key], key:key});
                }
                cmp.set("v.templateOptions",custs);
            }
            else {
                console.log("Failed with state: " + state);
            }
        });
        // Send action off to be executed
        $A.enqueueAction(action);
    },
    
    onSingleSelectChange: function(cmp) {
        var selectCmp = cmp.find("InputSelectSingle");
        cmp.set("v.selectedTemplate", selectCmp.get("v.value"));
	},
    
    savePDF : function(cmp, event, helper) {
        var recordId = cmp.get("v.recordId");
        var selectedTemplate = cmp.get("v.selectedTemplate");
        var qName = cmp.get("v.record_Q.Name");
        var pdfName = selectedTemplate;
        var vfPage = '/apex/pdfDI_QuoteStandard2'; //really should pull this from child component.
        var action = cmp.get("c.savePDF_Quote");
        action.setParams({
            "vfPage": vfPage, 
            "recordId" : recordId, 
            "selectedTemplate" : selectedTemplate, 
            "pdfName" : qName
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                alert('Attachment saved successfully');
                
            }
            else if (state === "INCOMPLETE") {
                // do something
            }
                else if (state === "ERROR") {
                    var errors = response.getError();
                    if (errors) {
                        if (errors[0] && errors[0].message) {
                            console.log("Error message: " + 
                                        errors[0].message);
                        }
                    } else {
                        console.log("Unknown error");
                    }
                }
        });
        $A.enqueueAction(action);
    }
    
    
    
})
Andy Kallio 7Andy Kallio 7
Looks like this is going to help me: https://github.com/aheber/ObjectAgnosticLightingComponent
HeberHeber
Always happy to help. If I remember right there is somewhere in there where the step-by-step is broken and you have to reset to the full example. Been a while since I reviewed it.