• kashish choudhary
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
Hi,

I try to make a quik action and a lightning component to create a quote :

Component.CMP
<aura:component controller="QuickQuoteController" 
                implements="force:lightningQuickActionWithoutHeader,force:hasRecordId">

    <aura:attribute name="opportunity" type="Opportunity" />
    <aura:attribute name="newQuote" type="Quote"
        default="{ 'sobjectType': 'Quote' }" />
    
    <!-- default to empty record -->
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <!-- Display a header with details about the opportunity -->
    <div class="slds-page-header" role="banner">
        <p class="slds-text-heading_label">{!v.opportunity.Name}</p>
        <h1 class="slds-page-header__title slds-m-right_small
            slds-truncate slds-align-left">Créer un nouveau devis</h1>
    </div>

    <!-- Display the new quote form -->
     <lightning:input aura:id="quoteField" name="name" label="Name"
                      value="{!v.newQuote.Name}" required="true"/>

    <lightning:input aura:id="quoteField" type="date" name="date" label="Date"
                     value="{!v.newQuote.Date}" required="true"/>
    
    <lightning:button label="Cancel" onclick="{!c.handleCancel}" class="slds-m-top_medium" />
    <lightning:button label="Save Quote" onclick="{!c.handleSaveQuote}"
               variant="brand" class="slds-m-top_medium"/>
    
</aura:component>

Controller.JS
({
    doInit : function(component, event, helper) {

        // Prepare the action to load opportunity record
        var action = component.get("c.getOpportunity");
        action.setParams({"opportunityId": component.get("v.recordId")});

        // Configure response handler
        action.setCallback(this, function(response) {
            var state = response.getState();
            if(state === "SUCCESS") {
                component.set("v.opportunity", response.getReturnValue());
            } else {
                console.log('Problem getting opportunity, response state: ' + state);
            }
        });
        $A.enqueueAction(action);
    },

    handleSaveQuote: function(component, event, helper) {
        if(helper.validateQuoteForm(component)) {
            
            // Prepare the action to create the new quote
            var saveQuoteAction = component.get("c.saveQuoteWithOpportunity");
            saveQuoteAction.setParams({
                "quote": component.get("v.newQuote"),
                "opportunityId": component.get("v.recordId")
            });

            // Configure the response handler for the action
            saveQuoteAction.setCallback(this, function(response) {
                var state = response.getState();
                if(state === "SUCCESS") {

                    // Prepare a toast UI message
                    var resultsToast = $A.get("e.force:showToast");
                    resultsToast.setParams({
                        "title": "Quote Saved",
                        "message": "The new quote was created."
                    });

                    // Update the UI: close panel, show toast, refresh quote page
                    $A.get("e.force:closeQuickAction").fire();
                    resultsToast.fire();
                    $A.get("e.force:refreshView").fire();
                }
                else if (state === "ERROR") {
                    console.log('Problem saving quote, response state: ' + state);
                }
                else {
                    console.log('Unknown problem, response state: ' + state);
                }
            });

            // Send the request to create the new quote
            $A.enqueueAction(saveQuoteAction);
        }
        
    },

	handleCancel: function(component, event, helper) {
	    $A.get("e.force:closeQuickAction").fire();
    }

})

Helper.JS
({
    validateQuoteForm: function(component) {
        var validQuote = true;

        
        // Show error messages if required fields are blank
        var allValid = component.find('quoteField').reduce(function (validFields, inputCmp) {
            inputCmp.showHelpMessageIfInvalid();
            return validFields && inputCmp.get('v.validity').valid;
        }, true);

        if (allValid) {
        // Verify we have an opportunity to attach it to
        var opportunity = component.get("v.opportunity");
        if($A.util.isEmpty(opportunity)) {
            validOpportunity = false;
            console.log("Quick action context doesn't have a valid opportunity.");
        }

        return(validOpportunity);
	}
    }
})

Controller.APXC
public with sharing class QuickQuoteController {

    @AuraEnabled
    public static Opportunity getOpportunity(Id opportunityId) {
        // Perform isAccessible() checks here
        return [SELECT Name FROM Opportunity WHERE Id = :opportunityId];
    }
    
    @AuraEnabled
    public static Quote saveQuoteWithOpportunity(Quote quote, Id opportunityId) {
        // Perform isAccessible() and isUpdateable() checks here
        quote.OpportunityId = opportunityId;
        upsert quote;
        return quote;
    }

}
I have an issue and I can't find where it comes :
 
Uncaught Action failed: c:quickQuote$controller$handleSaveQuote [validOpportunity is not defined]

markup://c:quickQuote

Object.validateQuoteForm()@https://org--dev.lightning.force.com/one/components/c/quickQuote.js:89:9
handleSaveQuote()@https://org--dev.lightning.force.com/one/components/c/quickQuote.js:27:19
handleClick()@https://org--dev.lightning.force.com/components/lightning/button.js:1:470

Any idea ?
 
I have a requirement to prepopulate some fields (Address Fields) when standard "New" button is clicked from the related list (Contact) of Account. This was being achieved using an intermediate VF page in classic.
Now in lightning, I am able to create a component with a quick action to achieve the same - but the problem here is that the override has to be either a vf page or a lightning component.
Is there any way to have both on a single button - like when in classic, follow the old approach by redirecting to an intermediate vf and prepopulating the values and when in lightning, use the newly created lightning component?