Override Standard New Button and Set Default Values based on Record Type When You Create a New Record uses of Apex class and Custom Setting in Salesforce Lightning | Initially set default selected value on account based on Record Type uses of Apex Class and Lightning Component in Salesforce

2,510 views

Hey guys, today in this post we are going to learn about How to Override Standard New Button and Set Default Values based on Record Type When You Create a New Record uses of Apex class and Custom Setting in Salesforce Lightning.

To navigate in Lightning Experience, Experience Builder sites, or the Salesforce mobile app, define a PageReference object. The pageReference type generates a unique URL format and defines attributes that apply to all pages of that type. For Experience Builder sites, depending on the page type, the pageReference property requirements can differ between LWR sites and Aura sites. To more details, Click Here.

 
 Set Default Values based on Record Type -- w3web.net
 

Final Output β†’

Challenges in your career

🎯 If You Are Facing Any Of These 6 Challenges in your career.

  • Learn Salesforce Development
  • Career Confusion
  • No Interview Call
  • Low Salary
  • No Promotion/Growth
  • No Finding New Job Opportunity
  • Why you stucking from past so many years in same company?

 

Set Default Values based on Record Type -- w3web.net

 

You can download file directly from github by Click Here.

 

Other related post that would you like to learn in Salesforce

 
Set Default Values based on Record Type -- w3web.net
 

  • Find the below steps β–Ύ

 

Create Lightning Component

Step 1:- Create Lightning Component : setDefaultAccountValueCmp.cmp

From Developer Console >> File >> New >> Lightning Component

setDefaultAccountValueCmp.cmp [Lightning Component File]


  1.    <aura:component controller="setDefaultAccountValueCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction,lightning:isUrlAddressable,lightning:actionOverride,lightning:hasPageReference" access="global" description="c:setDefaultAccountValueCmp">
  2. 	<aura:attribute name="url" type="String"/>
  3.     <aura:attribute name="selectedRecordId" type="Id" />
  4.      <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>    
  5.     <lightning:pageReferenceUtils aura:id="pageRefUtils"/>
  6. 	<lightning:navigation aura:id="navLink"/>
  7.     <aura:attribute name="pageReference" type="Object"/>   
  8.     <lightning:navigation aura:id="navService"/>
  9.     <aura:attribute name="accInfoList" type="List" /> 
  10. </aura:component>

 

Create JavaScript Controller

Step 2:- Create Lightning Component : setDefaultAccountValueCmpController.js

From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller

setDefaultAccountValueCmpController.js [JavaScript Controller]


  1.    ({
  2. 	 doInit: function(component, event, helper) {
  3.  
  4.         helper.accInfoHelper(component, event, helper);
  5.  
  6.     },
  7. })

 

Create JavaScript Helper

Step 3:- Create Lightning Component : setDefaultAccountValueCmpHelper.js

From Developer Console >> File >> New >> Lightning Component >> JavaScript Helper

setDefaultAccountValueCmpHelper.js [JavaScript Helper File]


  1.    ({
  2. 	accInfoHelper : function(component, event, helper) {
  3.         var action = component.get("c.accDefaultDetails");
  4.         action.setCallback(this, function(response) {
  5.             var state = response.getState();            
  6.             if (state == "SUCCESS") {
  7.                 var results= response.getReturnValue();
  8.                 console.log("resultDataRRR", JSON.stringify(results));
  9.                 component.set("v.accInfoList", results);
  10.  
  11.  
  12.                 ///////////////start PageReference
  13.  
  14.                 var recordTypeId = component.get("v.pageReference").state.recordTypeId;
  15.                 console.log(recordTypeId);
  16.                 var actionName = component.get("v.pageReference").attributes.actionName;
  17.                 console.log('actionName-' + actionName);
  18.                 var objectApiName = component.get("v.pageReference").attributes.objectApiName;
  19.                 console.log('objectApiName-' + objectApiName);
  20.                 var createRecordEvent = $A.get("e.force:createRecord");               
  21.                 if(recordTypeId == "0125g000000fArnAAE")
  22.                     {
  23.                      createRecordEvent.setParams({
  24.                     "entityApiName": "Account",
  25.                     "recordTypeId" : recordTypeId,
  26.                     "defaultFieldValues": {
  27.                         "Ownership":results[0].Ownership__c,                        
  28.                         "ParentId":results[0].Parent_Account_ID__c,
  29.                         "Phone":results[0].Phone__c,
  30.                         "Type":results[0].Type__c,
  31.                     }
  32.                 });
  33.                 createRecordEvent.fire();
  34.                     }
  35.                 else{
  36.                     createRecordEvent.setParams({
  37.                     "entityApiName": "Account",
  38.                     "recordTypeId" : recordTypeId,
  39.                     "defaultFieldValues": {
  40.                         "Ownership":results[0].Ownership__c,                        
  41.                         "ParentId":results[0].Parent_Account_ID__c,
  42.                         "Phone":results[0].Phone__c,
  43.                         "Type":results[0].Type__c,   
  44.  
  45.                     }
  46.                 });
  47.                 createRecordEvent.fire();
  48.                 }
  49.              ////////// End PageReference
  50.             }
  51.         });
  52.         $A.enqueueAction(action);
  53.     },
  54. })

 

Create Apex Class Controller

Step 4:- Create Apex Class : setDefaultAccountValueCtrl.apxc

From Developer Console >> File >> New >> Apex Class

setDefaultAccountValueCtrl.apxc [Apex Class Controller]


  1.     public class setDefaultAccountValueCtrl {
  2.     @AuraEnabled
  3.     public static List<setDefaultAccountValues__c>  accDefaultDetails()
  4.     {        
  5.       //Custom Setting Object
  6.        List<setDefaultAccountValues__c> accValDefaultList = setDefaultAccountValues__c.getAll().values(); 
  7.        system.debug('accValDefaultList## ' + accValDefaultList);
  8.        RETURN accValDefaultList;
  9.     }
  10. }

 
 Set Default Values based on Record Type -- w3web.net
 

Further post that would you like to learn in Salesforce

 
 

 

FAQ (Frequently Asked Questions)

What is pageReference aura component?

The pageReference type generates a unique URL format and defines attributes that apply to all pages of that type. For Experience Builder sites, depending on the page type, the pageReference property requirements can differ between LWR sites and Aura sites.

What is pageReference in Salesforce?

A PageReference is a reference to an instantiation of a page. Among other attributes, PageReferences consist of a URL and a set of query parameter names and values.

How do I pass a value from controller to VF page?

In the VF page we have used param at two places, one in the commandbutton and the other in actionfunction. The param which is in the command button has a value specified to it and also assigned to a variable in controller. This value will be passed to the variable in the controller if the button is clicked.

Related Topics | You May Also Like

 
  

Our Free Courses β†’

πŸ‘‰ Get Free Course β†’

πŸ“Œ Salesforce Administrators

πŸ“Œ Salesforce Lightning Flow Builder

πŸ“Œ Salesforce Record Trigger Flow Builder

πŸ‘‰ Get Free Course β†’

πŸ“Œ Aura Lightning Framework

πŸ“Œ Lightning Web Component (LWC)

πŸ“Œ Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment