You need to sign in to do that
Don't have an account?
Mayank Upadhyaya 5
How to create opportunity from account by using aura component.
Here is my code, I was not able to find the error
-----------------Component ---------------------------------------------------
<aura:component controller="QuickOpportunityController"
implements="force:lightningQuickActionWithoutHeader,force:hasRecordId">
<aura:attribute name="account" type="Account" />
<aura:attribute name="newOpportunity" type="Opportunity"
default="{ 'sobjectType': 'Opportunity' }" /> <!-- default to empty record -->
<aura:attribute name="options"
type="list"
default="[
{'label': 'Discovery', 'value': 'Discovery'},
{'label': 'Decision Maker Meeting', 'value': 'Decision Maker Meeting'},
{'label': 'Custom Proposal', 'value': 'Custom Proposal'},
{'label': 'Verbal Agreement', 'value': 'Verbal Agreement'},
{'label': 'Signed Contract', 'value': 'Signed Contract'},
{'label': 'Closed-Installed', 'value': 'Closed-Installed'},
{'label': 'Closed-Lost', 'value': 'Closed-Lost'},
{'label': 'Closed-No Decision', 'value': 'Closed-No Decision'},
{'label': 'Closed – Duplicate', 'value': 'Closed – Duplicate'},
{'label': 'Closed - Not Awarded', 'value': 'Closed - Not Awarded'}
]"
description="Below attribute will define picklist values if you want dynamic values then you can query that from the database and set those values"/>
<aura:attribute name="options2"
type="list"
default="[
{'label': 'Coin and Card', 'value': 'Coin and Card'},
{'label': 'Coin Only', 'value': 'Coin Only'},
{'label': 'Card Only', 'value': 'Card Only'},
{'label': 'Change Point Only', 'value': 'Change Point Only'},
{'label': 'No Change or N/A', 'value': 'No Change or N/A'},
{'label': 'Non-Vend', 'value': 'Non-Vend'},
{'label': 'In-Unit BOLB', 'value': 'In-Unit BOLB'},
{'label': 'In-Unit Comm Direct', 'value': 'In-Unit Comm Direct'}
]"
/>
<aura:attribute name="options3"
type="list"
default="[
{'label': 'Straight Percent', 'value': 'Straight Percent'},
{'label': 'Sliding Scale', 'value': 'Sliding Scale'},
{'label': 'Flat Amount', 'value': 'Flat Amount'}
]"
/>
<aura:attribute name="options4"
type="list"
default="[
{'label': 'None', 'value': 'None'},
{'label': '50%', 'value': '50%'},
{'label': '100%', 'value': '100%'}
]"
/>
<aura:attribute name="options1"
type="list"
default="[
{'label': 'New', 'value': 'New'},
{'label': 'Renewal', 'value': 'Renewal'}
]"
/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<!-- Display a header with details about the account -->
<div class="slds-page-header" role="banner">
<p class="slds-text-heading_label">{!v.account.Name}</p>
<h1 class="slds-page-header__title slds-m-right_small
slds-truncate slds-align-left">Create New Opportunity</h1>
</div>
<!-- Display the new opportunity form -->
<lightning:input aura:id="opportunityField" name="Name" label="Name"
value="{!v.newOpportunity.Name}" required="true"/>
<lightning:input aura:id="opportunityField" name="RecordType" label="Record Type"
value="Air IRR" required="true"/>
<lightning:input aura:id="opportunityField" name="Lease_Term_Months__c" label="Proposed Lease Term (mos)"
value="{!v.newOpportunity.Lease_Term_Months__c}" required="true"/>
<lightning:input aura:id="opportunityField" name="CSC_Pay_Value_Recovery_Method__c" label="CSC Pay Value Recovery Method"
value="None" required="true"/>
<lightning:input aura:id="opportunityField" name="Capital_Date__c" label="Capital Date" type="date"
value="{!v.newOpportunity.Capital_Date__c}" />
<lightning:input aura:id="opportunityField" name="CloseDate" label="Close Date" type="date"
value="{!v.newOpportunity.CloseDate}" />
<lightning:combobox aura:id="opportunityField" name="Admin_Fee__c" label="Admin Fee" value="{!v.newOpportunity.Admin_Fee__c}" placeholder="Select" options= "{!v.options4}" />
<lightning:combobox aura:id="opportunityField" name="Commission_Equation_Type__c" label="Commission Equation Type" value="{!v.newOpportunity.Commission_Equation_Type__c}" placeholder="Select" options= "{!v.options3}" />
<lightning:combobox aura:id="opportunityField" name="StageName" label="Stage Name" value="{!v.newOpportunity.StageName}" placeholder="Select Stage" options= "{!v.options}" />
<lightning:combobox aura:id="opportunityField" name="Type" label="Type"
value="{!v.newOpportunity.Type}" placeholder="Select type" required="true" options= "{!v.options1}" />
<lightning:combobox aura:id="opportunityField" name="Sub_Type__c" label="Sub-Type" value="!v.newOpportunity.Sub_Type__c" placeholder="Select Sub-type" options= "{!v.options2}" />
<lightning:button label="Cancel" onclick="{!c.handleCancel}" class="slds-m-top_medium" />
<lightning:button label="Save Opportunity" onclick="{!c.handleSaveOpportunity}"
variant="brand" class="slds-m-top_medium"/>
</aura:component>
-------------Controller -------------------------------------------------------------
public with sharing class QuickOpportunityController {
@AuraEnabled
public static Account getAccount(Id accountId) {
// Perform isAccessible() checks here
return [SELECT Name, BillingCity, BillingState FROM Account WHERE Id = :accountId];
}
@AuraEnabled
public static Opportunity saveOpportunityWithAccount(Opportunity opportunity, Id accountId) {
// Perform isAccessible() and isUpdateable() checks here
opportunity.AccountId = accountId;
insert opportunity;
return opportunity;
}
}
--------------------------------Controller JS file ----------------------------------
({
doInit : function(component, event, helper) {
// Prepare the action to load account record
var action = component.get("c.getAccount");
action.setParams({"accountId": component.get("v.recordId")});
// Configure response handler
action.setCallback(this, function(response) {
var state = response.getState();
if(state === "SUCCESS") {
component.set("v.account", response.getReturnValue());
} else {
console.log('Problem getting account, response state: ' + state);
}
});
$A.enqueueAction(action);
},
handleSaveOpportunity: function(component, event, helper) {
if(helper.validateOpportunityForm(component)) {
// Prepare the action to create the new opportunity
var saveOpportunityAction = component.get("c.saveOpportunityWithAccount");
saveOpportunityAction.setParams({
"opportunity": component.get("v.newOpportunity"),
"accountId": component.get("v.recordId")
});
// Configure the response handler for the action
saveOpportunityAction.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": "Opportunity Saved",
"message": "The new opportunity was created."
});
// Update the UI: close panel, show toast, refresh account page
$A.get("e.force:closeQuickAction").fire();
resultsToast.fire();
$A.get("e.force:refreshView").fire();
}
else if (state === "ERROR") {
system.debug('Problem saving opportunity, response state: ' + state);
}
else {
console.log('Unknown problem, response state: ' + state);
}
});
// Send the request to create the new opportunity
$A.enqueueAction(saveOpportunityAction);
}
},
handleCancel: function(component, event, helper) {
$A.get("e.force:closeQuickAction").fire();
}
})
---------------------------------Helper Js -------------------------------------------
({
validateOpportunityForm: function(component) {
var validOpportunity = true;
// Show error messages if required fields are blank
var allValid = component.find('opportunityField').reduce(function (validFields, inputCmp) {
inputCmp.showHelpMessageIfInvalid();
return validFields && inputCmp.get('v.validity').valid;
}, true);
if (allValid) {
// Verify we have an account to attach it to
var account = component.get("v.account");
if($A.util.isEmpty(account)) {
validOpportunity = false;
console.log("Quick action context doesn't have a valid account.");
}
}
return(validOpportunity);
}
})
-----------------Component ---------------------------------------------------
<aura:component controller="QuickOpportunityController"
implements="force:lightningQuickActionWithoutHeader,force:hasRecordId">
<aura:attribute name="account" type="Account" />
<aura:attribute name="newOpportunity" type="Opportunity"
default="{ 'sobjectType': 'Opportunity' }" /> <!-- default to empty record -->
<aura:attribute name="options"
type="list"
default="[
{'label': 'Discovery', 'value': 'Discovery'},
{'label': 'Decision Maker Meeting', 'value': 'Decision Maker Meeting'},
{'label': 'Custom Proposal', 'value': 'Custom Proposal'},
{'label': 'Verbal Agreement', 'value': 'Verbal Agreement'},
{'label': 'Signed Contract', 'value': 'Signed Contract'},
{'label': 'Closed-Installed', 'value': 'Closed-Installed'},
{'label': 'Closed-Lost', 'value': 'Closed-Lost'},
{'label': 'Closed-No Decision', 'value': 'Closed-No Decision'},
{'label': 'Closed – Duplicate', 'value': 'Closed – Duplicate'},
{'label': 'Closed - Not Awarded', 'value': 'Closed - Not Awarded'}
]"
description="Below attribute will define picklist values if you want dynamic values then you can query that from the database and set those values"/>
<aura:attribute name="options2"
type="list"
default="[
{'label': 'Coin and Card', 'value': 'Coin and Card'},
{'label': 'Coin Only', 'value': 'Coin Only'},
{'label': 'Card Only', 'value': 'Card Only'},
{'label': 'Change Point Only', 'value': 'Change Point Only'},
{'label': 'No Change or N/A', 'value': 'No Change or N/A'},
{'label': 'Non-Vend', 'value': 'Non-Vend'},
{'label': 'In-Unit BOLB', 'value': 'In-Unit BOLB'},
{'label': 'In-Unit Comm Direct', 'value': 'In-Unit Comm Direct'}
]"
/>
<aura:attribute name="options3"
type="list"
default="[
{'label': 'Straight Percent', 'value': 'Straight Percent'},
{'label': 'Sliding Scale', 'value': 'Sliding Scale'},
{'label': 'Flat Amount', 'value': 'Flat Amount'}
]"
/>
<aura:attribute name="options4"
type="list"
default="[
{'label': 'None', 'value': 'None'},
{'label': '50%', 'value': '50%'},
{'label': '100%', 'value': '100%'}
]"
/>
<aura:attribute name="options1"
type="list"
default="[
{'label': 'New', 'value': 'New'},
{'label': 'Renewal', 'value': 'Renewal'}
]"
/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<!-- Display a header with details about the account -->
<div class="slds-page-header" role="banner">
<p class="slds-text-heading_label">{!v.account.Name}</p>
<h1 class="slds-page-header__title slds-m-right_small
slds-truncate slds-align-left">Create New Opportunity</h1>
</div>
<!-- Display the new opportunity form -->
<lightning:input aura:id="opportunityField" name="Name" label="Name"
value="{!v.newOpportunity.Name}" required="true"/>
<lightning:input aura:id="opportunityField" name="RecordType" label="Record Type"
value="Air IRR" required="true"/>
<lightning:input aura:id="opportunityField" name="Lease_Term_Months__c" label="Proposed Lease Term (mos)"
value="{!v.newOpportunity.Lease_Term_Months__c}" required="true"/>
<lightning:input aura:id="opportunityField" name="CSC_Pay_Value_Recovery_Method__c" label="CSC Pay Value Recovery Method"
value="None" required="true"/>
<lightning:input aura:id="opportunityField" name="Capital_Date__c" label="Capital Date" type="date"
value="{!v.newOpportunity.Capital_Date__c}" />
<lightning:input aura:id="opportunityField" name="CloseDate" label="Close Date" type="date"
value="{!v.newOpportunity.CloseDate}" />
<lightning:combobox aura:id="opportunityField" name="Admin_Fee__c" label="Admin Fee" value="{!v.newOpportunity.Admin_Fee__c}" placeholder="Select" options= "{!v.options4}" />
<lightning:combobox aura:id="opportunityField" name="Commission_Equation_Type__c" label="Commission Equation Type" value="{!v.newOpportunity.Commission_Equation_Type__c}" placeholder="Select" options= "{!v.options3}" />
<lightning:combobox aura:id="opportunityField" name="StageName" label="Stage Name" value="{!v.newOpportunity.StageName}" placeholder="Select Stage" options= "{!v.options}" />
<lightning:combobox aura:id="opportunityField" name="Type" label="Type"
value="{!v.newOpportunity.Type}" placeholder="Select type" required="true" options= "{!v.options1}" />
<lightning:combobox aura:id="opportunityField" name="Sub_Type__c" label="Sub-Type" value="!v.newOpportunity.Sub_Type__c" placeholder="Select Sub-type" options= "{!v.options2}" />
<lightning:button label="Cancel" onclick="{!c.handleCancel}" class="slds-m-top_medium" />
<lightning:button label="Save Opportunity" onclick="{!c.handleSaveOpportunity}"
variant="brand" class="slds-m-top_medium"/>
</aura:component>
-------------Controller -------------------------------------------------------------
public with sharing class QuickOpportunityController {
@AuraEnabled
public static Account getAccount(Id accountId) {
// Perform isAccessible() checks here
return [SELECT Name, BillingCity, BillingState FROM Account WHERE Id = :accountId];
}
@AuraEnabled
public static Opportunity saveOpportunityWithAccount(Opportunity opportunity, Id accountId) {
// Perform isAccessible() and isUpdateable() checks here
opportunity.AccountId = accountId;
insert opportunity;
return opportunity;
}
}
--------------------------------Controller JS file ----------------------------------
({
doInit : function(component, event, helper) {
// Prepare the action to load account record
var action = component.get("c.getAccount");
action.setParams({"accountId": component.get("v.recordId")});
// Configure response handler
action.setCallback(this, function(response) {
var state = response.getState();
if(state === "SUCCESS") {
component.set("v.account", response.getReturnValue());
} else {
console.log('Problem getting account, response state: ' + state);
}
});
$A.enqueueAction(action);
},
handleSaveOpportunity: function(component, event, helper) {
if(helper.validateOpportunityForm(component)) {
// Prepare the action to create the new opportunity
var saveOpportunityAction = component.get("c.saveOpportunityWithAccount");
saveOpportunityAction.setParams({
"opportunity": component.get("v.newOpportunity"),
"accountId": component.get("v.recordId")
});
// Configure the response handler for the action
saveOpportunityAction.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": "Opportunity Saved",
"message": "The new opportunity was created."
});
// Update the UI: close panel, show toast, refresh account page
$A.get("e.force:closeQuickAction").fire();
resultsToast.fire();
$A.get("e.force:refreshView").fire();
}
else if (state === "ERROR") {
system.debug('Problem saving opportunity, response state: ' + state);
}
else {
console.log('Unknown problem, response state: ' + state);
}
});
// Send the request to create the new opportunity
$A.enqueueAction(saveOpportunityAction);
}
},
handleCancel: function(component, event, helper) {
$A.get("e.force:closeQuickAction").fire();
}
})
---------------------------------Helper Js -------------------------------------------
({
validateOpportunityForm: function(component) {
var validOpportunity = true;
// Show error messages if required fields are blank
var allValid = component.find('opportunityField').reduce(function (validFields, inputCmp) {
inputCmp.showHelpMessageIfInvalid();
return validFields && inputCmp.get('v.validity').valid;
}, true);
if (allValid) {
// Verify we have an account to attach it to
var account = component.get("v.account");
if($A.util.isEmpty(account)) {
validOpportunity = false;
console.log("Quick action context doesn't have a valid account.");
}
}
return(validOpportunity);
}
})
Your Component Is Working Fine Just Add It On Your account record Page Just Add this "flexipage:availableForRecordHome" Please Mark It As Best Asnwer If It Helps
Thank You!
still getting the below error
Uncaught Error in $A.getCallback() [system is not defined]
Callback failed: apex://QuickOpportunityController/ACTION$saveOpportunityWithAccount
Thanks & Regards,
Mayank Upadhyaya
I've Tried Your Component In My Org It's Working Fine
Hello!
I can see that in your handleSaveOpportunity-function you are using
Change this one to be console.log instead and your error should be resolved.
https://www.marykayintouch.ltd/