You are here:
Create Lightning Component for Create Contract Action Button
To configure a create contract action button, you must create a lightning component that will call the connected API called Create Contract.
- Create an Apex class.
- Click File | New | Apex Class.
- Enter a name for the apex class. For example, CreateContract.
- Click OK.
- Paste the code.
public class CreateContractController { @AuraEnabled public static List<String> getCreatedRecord(String sourceRecordId, Boolean autoDocgen) { ConnectApi.ContractInputRepresentation objInput = new ConnectApi.ContractInputRepresentation(); objInput.sourceObjectId = sourceRecordId; objInput.isAutoDocgenRequired = autoDocgen; ConnectApi.ContractOutputRepresentation objOutput = ConnectApi.Clm.createContract(objInput); List<String> contractData = new List<String>(); contractData.add(objOutput.data[0]); List<Contract> contractName = [SELECT Name FROM Contract WHERE Id =:objOutput.data[0]]; contractData.add(contractName[0].Name); return contractData; } } - Save your changes.
- Open the developer console.
- Create a lightning component:
- Click File | New | Lightning Component.
- Enter a name for the lightning component.For example, <CreateContractFromCustomObject>.
- Select Lightning Quick Action.
- Click Submit.The lightning component class page opens.
- Paste the code.
<aura:component controller="CreateContractController" implements="force:lightningQuickAction,force:hasRecordId"> <aura:html tag="style"> .modal-body.spinnerHeight{ height: 25% } </aura:html> <aura:handler name="init" value="{!this}" action="{!c.onInit}"/> <aura:attribute name="recordId" type="String" description="stores the record id"/> <lightning:navigation aura:id="navLink"/> <aura:attribute name="Spinner" type="boolean" default="true"/> <aura:if isTrue="{!v.Spinner}"> <div style="height:6rem;position:relative"> <lightning:spinner variant="brand" alternativeText="Loading..." size="large"/> </div> </aura:if> </aura:component> - Save your changes.
- Create a controller.
- Click CONTROLLER.
- Paste the code.
({ onInit: function (cmp, event, helper) { var createContractAction = cmp.get("c.getCreatedRecord"); cmp.set("v.Spinner", true); createContractAction.setParams({ sourceRecordId : cmp.get("v.recordId"), autoDocgen : "true" }); createContractAction.setCallback(this, function(result) { var state = result.getState(); var response, navLink, pageRef; if (state === 'SUCCESS') { cmp.set("v.Spinner", false); response = result.getReturnValue(); var contractId = response[0]; var successMessage = response[1] + " created successfully"; $A.get("e.force:showToast").setParams({ mode: 'dismissible', type: "success", message: successMessage, duration: 10000 }).fire(); navLink = cmp.find("navLink"); pageRef = { type: 'standard__recordPage', attributes: { actionName: 'view', objectApiName: 'Contract', recordId : response[0] }, }; navLink.navigate(pageRef, true); }else{ cmp.set("v.Spinner", false); $A.get("e.force:showToast").setParams({ mode: 'dismissible', type: "error", message:result.getError() && result.getError()[0] && result.getError()[0].message ? result.getError()[0].message :"Contract creation failed", }).fire(); } }); $A.enqueueAction(createContractAction); } }) - Save your changes.

