Showing posts with label Advanced Apex Programming. Show all posts
Showing posts with label Advanced Apex Programming. Show all posts

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 !!

Sunday, 5 March 2017

Lightning Modal Using Salesforce Lightning Component #Salesforce

After a long time I got some time for blogging, I've been thinking for a long to post something about lightning component, its feel great when you learn something, I've been now working on lightning for more then 6 months now, learned somethings about the SLDS (Salesforce Lightning Design System) and still learning about lightning Components,

Lightning Component Framework : It is one of the most powerful app development technology we have, Lightning Components is a UI framework for developing web apps for mobile and desktop devices. It’s a modern framework for building single-page applications with dynamic, responsive user interfaces for Force.com apps. It uses JavaScript on the client side and Apex on the server side. (Source Trailhead)


(Source Trailhead)

I would highly recommend TRAILHEAD for learning Salesforce Lightning Component module

So here I am going to show you how simple is creating a modal in Lightning component, and if you ask why modal, then this is because, I get many requirement where client asks that we need this thing in a pop-up or that form should come in a modal, so it is very commonly used thing, so I thought it will be helpful to post about it.

I am not going into the basics of the lightning component framework, so here how it looks


So here is the component code for the lightning modal


<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global">
    
    <ltng:require styles="/resource/SLDS103/assets/styles/salesforce-lightning-design-system-ltng.css" />
    
    <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">
                            
                           London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.
                        </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">Continue</button>
                        </div>
                    </div>
                </div>
            </div>
            <button class="slds-button slds-button--neutral" onclick="{!c.showModal}">Open Modal</button>
        </div>
    </div>
    
</aura:component>

And here is the controller code :

({  
    showModal : function(component, event, helper) {
        document.getElementById("newClientSectionId").style.display = "block";
    },
    
    hideModal : function(component,event, helper){
        document.getElementById("newClientSectionId").style.display = "none" ;
    }
 })

To run the above component, create a lightning app

<aura:application >
    <c:JustLightningModal />
</aura:application>


Note : Use SLDS library of Salesforce used in the component code.


Hope this help will soon continue to add more functionality to the modal using lightning component.

Thanks

Thursday, 13 August 2015

Get Current Geolocation in Salesforce using Geolocation API

Recently was trying to fetch the current location of mine in salesforce, most of the time people use google api's for it, but just dig in a bit and found something way too easy and way too awesome to use, the best part is you can utilize the javascript code without referring any further libraries.

Here I got the example of using geolocation API, this document is the specification defines an API that provides scripted access to geographical location information associated with the hosting device.

here is the sample code that they shared


So to implement this thing I want to check weather it work on normally on Salesforce or not, So I created a detail button on Account to check the code. I created a geolocation type field on Account object, and created a detail button on it adn added it to the layout.

Here is the detail page button "Check-in" and "Geolocation" field, just click on check-in button you will the current geolocation, to know more about the geolocation field click here


Here is the code of the check-in detail button, please put Behavior as Execute JavaScript of the detail button.

1:  {  
2:    !requireScript("/soap/ajax/26.0/connection.js")  
3:  }  
4:  navigator.geolocation.getCurrentPosition(function(position) {  
5:    var longitude = position.coords.longitude;  
6:    var latitude = position.coords.latitude;  
7:    var account = new sforce.SObject("Account");  
8:    account.id = '{!Account.Id}';  
9:    account.Geolocation__Latitude__s = latitude;  
11:   account.Geolocation__Longitude__s = longitude;  
12:   var result = sforce.connection.update([account]);  
13:   if (result[0].getBoolean("success")) {  
14:        alert('Account updated successfully');  
15:        window.location.reload();  
16:   } else {  
17:       alert('Error : ' + result);  
18:   }  
19: });  

Just try above code, it works like charm. Happy to answer any query.
Hope this will help someone.

Happy coding!!

Saturday, 18 July 2015

Salesforce - "Summer of #Trailhead" Bangalore meetup Experience

Hi all, I am here again to share some amazing experience of Salesforce meetup "Summer of Trailhead", its all about trailhead practice and awesome experience that trailhead gives to a learner of Salesforce, its an amazing place to learn and explore new things. I have already posted my Trailhead experience on my Salesforce #trailhead : Place to Learn, Explore & Play post.

Thanks to all the Salesforce geeks to join us on the meetup, thanks to Salesforce for sending us awesome goodies, I feel proud when I got some of those, along with goodies we got amazing trailhead stickers and books and the amazing t-shirts.


There was hands on for those who are new with trailhead, people took few badges as well, it was amazing when a bunch of people are trailing and tweeting about trailhead, take a look at one of my tweets



It was an amazing experience while helping and learning with new geeks of salesforce, salesforce is now a relation between two humans. Thats what I saw in the summer of trailhead meetup.

I cannot thank enough for the amazing book that I won at trivia of the meetup "Advanced Apex Programming" written by Dan Appleman I will finish this one asap, then will definitely post some new learning from the book.


Once again after #lightning meetup felt awesome to meet salesforce plumbers and newbies.
Thanks Salesforce and everyone.

Keep trailing!!