Friday, 31 March 2017

Lightning Record Type Selector Component #Salesforce

In this blog, I am going to create a lightning component which is a replica of the record type selector in lightning, its so simple, its a continuation of my last blog about lightning model link

So lets get started,

Here is the controller, which is fetching all the record type of the Account object


public class RecordTypeSelectorController {
    
    @AuraEnabled
    public static List<RecordType> getListOfRecordType(){
        String query = 'SELECT Id,Name FROM RecordType WHERE IsActive=TRUE AND SobjectType =\''+'Account'+'\' ';
        List<RecordType> rtNames = new List<RecordType>();  
        Schema.SObjectType  objType = Account.SObjectType;        
        
        for(RecordTypeInfo rt : objType.getDescribe().getRecordTypeInfos()){
            rtNames.add(new RecordType(Id = rt.getRecordTypeId(),Name = rt.getName()));
        }    
        return rtNames;    
    }
    
    @AuraEnabled
    public static Id getAccountRecordType() {
       Id ClientRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Client').getRecordTypeId();
       return ClientRecordTypeId;
    } 
}



Now here is the lightning component code

Here is the component, if you have read the last blog about the lightning model, then it would be easy to understand this one

This is the Component code, here we have a modal and button, which will have functionality of opening the modal, and the other button will open the Account record creation modal which is standard.


<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" controller="RecordTypeSelectorController" access="global">
    
    <ltng:require styles="/resource/SLDS103/assets/styles/salesforce-lightning-design-system-ltng.css" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" access="public"/>
    <aura:attribute name="recordTypes" type="String[]" access="public"/>
    
    <div class="slds">
        
        <div >
            <!-- modal body starts here -->
            <div tabindex="-1" aria-labelledby="header43" aria-hidden="false" id="newClientSectionId" role="dialog" class="slds-modal slds-fade-in-open" style="display:none;">
                <div class="slds-backdrop slds-backdrop--open">
                    <div class="slds-modal__container">
                        <div class="slds-modal__header">
                            <button class="slds-button slds-modal__close slds-button--icon-inverse" title="Close">
                                <button class="slds-button slds-button--neutral" onclick="{!c.hideModal}">X</button>  
                                <span class="slds-assistive-text">Close</span>
                            </button>
                            <h2 id="header43" class="slds-text-heading--medium">Modal Header</h2>
                        </div>
                        <div class="slds-modal__content slds-p-around--medium">
                            
                            Select a record type : 
                            <br/>
                            <div style="margin-left:30%">
                                <aura:iteration items="{!v.recordTypes}" var="rt">
                                    <ol class="slds-list--vertical slds-list--vertical-space">
                                        <input type="radio" value="{!rt.Name}" name="recordTypeRadio" id="{!rt.Id}" style="margin-right: 5px;" />{!rt.Name}
                                    </ol>   
                                </aura:iteration> 
                            </div>
                        </div>
                        
                        <div class="slds-modal__footer">
                            <button class="slds-button slds-button--brand" onclick="{!c.hideModal}">Cancel</button>
                            <button class="slds-button slds-button--brand" onclick="{!c.createRecord}">Next</button>
                        </div>
                    </div>
                </div>
            </div>
            <button class="slds-button slds-button--neutral" onclick="{!c.showModal}">Open Modal</button>
        </div>
    </div>
    
</aura:component>


Here is the controller code, which you can add to the controller which we have written earlier in the lightning modal blog, just add these two methods in the controller and we are done with the controller, init method is to initialise method of the helper and createRecord is a standard function which you can use in the lightning component to get the standard functionality of creation of the records.


createRecord : function (component, event, helper) {
        var rtDet = document.querySelector('input[name="recordTypeRadio"]:checked');
    
        if(rtDet != null) {
            document.getElementById("newClientSectionId").style.display = "none" ;
            var createRecordEvent = $A.get("e.force:createRecord");
            createRecordEvent.setParams({
                "entityApiName": "Account",
                "recordTypeId":rtDet.id
            });
            createRecordEvent.fire();
        }    
    },
    
    doInit : function(component, event, helper) {   
        helper.RecordTypeSelectorController(component);   
    } 
    


Here is our helper, where we are calling the controllers method, which will return us a list of the all the record type of the Account object,


({
   RecordTypeSelectorController: function(component) {
    var action = component.get("c.getListOfRecordType");
    action.setCallback(this, function(actionResult) {
        var infos = actionResult.getReturnValue();
        component.set("v.recordTypes", infos);  
    });
    $A.enqueueAction(action);
  }
})


You are done, now lets see how it looks,

Here is the modal having list of record types of Account.


And here is the record detail to create a new Account record


To run the above component, create a lightning app


<aura:application >
    <c:RecordTypeSelector />
</aura:application>


Here is the github Repo for updated code and deploying it salesforce Github

Post a comment if you have any query, I would be happy to help,
thank you !!

53 comments:

  1. Hi Abhi, do you have any idea based on parent record type selection child record type should get selected and the record creation page should come. This should be achieved thru quick action lightning component.. any idea on this

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Nice content.very useful thank you! for your creative ideas.
    coursquare
    education

    ReplyDelete
  5. Is there any way to prepopulate values on the record detail page? For instance: prepopulate Account Name.

    ReplyDelete
  6. Do you have sample one for these if they have descriptions below?

    ReplyDelete
  7. how to open visualforce page after record selection.I have scenario with once select the account record type then it should open my custom VF page .but it opens the std.page.I have already override New button in lighting as VF page.

    ReplyDelete
  8. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    JavaScript Training in Chennai

    JavaScript Training in Chennai


    ReplyDelete
  9. You write this post very carefully I think, which is easily understand to me. Not only this, other post is also good. As a newbie this info is really helpful for me. Thanks to you.
    tally training
    Tally Training in Chennai
    Tally ERP 9 Training
    Tally Course
    tally classes
    Tally institute in Chennai
    Tally Training institute in Chennai
    Tally course in Chennai

    ReplyDelete
  10. This content of information has
    helped me a lot. It is very well explained and easy to understand.
    SEO Training
    seo course

    ReplyDelete
  11. Excellent post. The author has shared the post in a unique way. Have been in a wait for more updates. Do share more. really like this page.
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  12. Pretty informed post! I'm seeking for some topics I need to see that our site affection and then drove it our site all report is really good. thanks
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  13. Such a priceless piece of information. It was quite interesting to read this article.I would want to thank you for your efforts in writing this amazing essay. customer relationship management module in erp

    ReplyDelete
  14. Here I saw several articles and posts published on this site, I am more interested in some of them, will provide more information on these topics in future articles.Manual Testing training onlineManual Testing Online TrainingQA training OnlineSoftware testing Online Courseselenium training online courseqa testing online coursepython training in hyderabad

    ReplyDelete
  15. 15C3A280A6SummerBFD5422F9025 November 2024 at 06:15

    02D1BAB323
    görüntülü show

    ReplyDelete

  16. Hayatımız boyunca kendimizi geliştirmek ve yeni bilgiler edinmek adına çeşitli kaynaklara başvururuz. Bu süreçte, özellikle okunması gereken kitaplar listesini gözden geçirmek oldukça faydalı olabilir; çünkü bu kitaplar, farklı bakış açıları ve derin düşünceler sunar. Kendimizi sürekli yenilemek ve daha iyi anlamak adına, bu tür kaynaklar bizim için büyük önem taşır. Herkesin kendine uygun kitaplar bulup, okuma alışkanlığı edinmesi dileğiyle.

    ReplyDelete
  17. Nice explanation of the Lightning Record Type Selector component. It’s really helpful for Salesforce developers who want to simplify the process of selecting record types while creating new records. Implementing such custom Lightning components can improve user experience and make the interface more interactive. Articles like this are very useful for developers learning Lightning and Apex. Along with technical skills, learning online promotion is also valuable—check out this digital marketing course in Chennai to understand modern marketing strategies.

    ReplyDelete
  18. Great explanation of how the Lightning Record Type Selector works in Salesforce. Customizing record types makes it easier for users to access the right forms and data quickly, improving efficiency in workflows. Learning modern tech skills alongside Salesforce can be very helpful for career growth. You can explore courses like **Java course Chennai**, **Python course**, **Data Science/Data Analyst course**, **Full Stack Development course**, and **UI/UX course** at **Login360.in** to gain practical skills across tech domains.

    ReplyDelete
  19. Very useful content, especially for beginners. I’d also recommend data science or data analytics.data science or data analytics

    ReplyDelete
  20. Very useful content. Beginners should check digital marketing training in chennai.

    ReplyDelete
  21. “Great insights on Salesforce concepts—very helpful for learners! Also, check this UI UX design course online to enhance your design skills.”

    ReplyDelete
  22. This post is very useful. I’m also learning from digital marketing training in chennai

    ReplyDelete
  23. This is a well-written post. Anyone interested should also check best digital marketing training institute.

    ReplyDelete
  24. Informative content! Also relevant to Data Science or Data Analytics.

    ReplyDelete
  25. Great explanation—your breakdown of the Lightning record type selector in Salesforce makes it much easier to understand how to customize and control user experiences. Clear examples like these are really helpful for admins and developers.

    For readers who also want to strengthen their practical skills alongside understanding such concepts, structured learning can be very helpful. For example, this Figma UI/UX Course is great for design fundamentals, and this Full Stack Developer Course in Chennai helps in building strong development skills.

    Thanks for sharing!

    ReplyDelete
  26. This is actually helpful, not just filler content.
    Learn Data Analytics Easily and Join Full Stack Program

    ReplyDelete
  27. I found this article very practical and beginner friendly. Students who want complete digital skills should look into this Python course in Kochi while also reviewing the UI/UX design course syllabus.

    ReplyDelete