Nerd @ Work

When Salesforce is life!

[Salesforce/ Lightning] inputLookup: the missing component

Or quick tips on how to implement your own inputLookup Salesforce ligthning component

This is a repost of the article I published on Salesforce Developer Blog.

Salesforce Spring ’15 release brought some brand new components ready to be used in your lightning apps. One of the missing components that could be useful for your apps is the input lookup component. The idea is to use a typeahead input field. We are going to user Bootstrap and the Twitter typeahead component.

The idea behind this implementation is that the user will type a search on the input field and the typeahead shows a list of the first 20 items for the given object and search string. On load, the component should also load the initial value of the “name” field for the given object, if an ID value is selected.

This is how the component will look:

For those of you who don’t want to read more and just use the component, jump to the inputLookup github repo.

To ensure proper script loading we will use RequireJS with the trick proposed in this blog post.

Let’s first have a look at the lookupInput component’s markup (github):

<aura:component controller="InputLookupController">
    
    <ltng:require scripts="/resource/RequireJS" afterScriptsLoaded="{!c.initTypeahead}" />
    <aura:handler name="init" value="{!this}" action="{!c.setup}"/>

    <!-- ... -->

    <!-- public attributes -->
    <aura:attribute name="type" type="String" required="true"/>
    <aura:attribute name="value" type="String" />
    <aura:attribute name="className" type="String" />
    <!-- PRIVATE ATTRS -->
    <aura:attribute name="nameValue" type="String"
                    access="PRIVATE"/>
    <aura:attribute name="isLoading" type="Boolean"
                    default="true"
                    access="PRIVATE"/>

    <div class="has-feedback">
        <input id="{!globalId+'_typeahead'}" type="text" 
               value="{!v.nameValue}" class="{!v.className}" 
               onchange="{!c.checkNullValue}" readonly="{!v.isLoading}"/> 
        <span class="glyphicon glyphicon-search form-control-feedback"></span>
    </div>
</aura:component>

It uses the “InputLookupController” Apex class (with required @RemoteActions).

The RequireJS components triggers “c:requireJSLoaded” event that actually makes the component load its bootstrapped Typeahead input field: note the absence of any custom namespace, that is replaced with the “c” default namespace (this is a Spring ’15 feature).

We also have an init method that ensures that all required parameters are set.

This component simply uses a “type” parameters (with the actual API name of the needed SObject) and a “value” parameter that stores the selected ID (or the loading ID).

At first we want to load on initialization (or when the value parameter changes) the content of the input (we want to see the “Name” of the pre-selected SObject and not its ID).

The initialization loading is done using the following “inputLookupController.js” method:

initTypeahead : function(component, event, helper){
        try{
            //first load the current value of the lookup field and then
            //creates the typeahead component
            helper.loadFirstValue(component);
        }catch(ex){
            console.log(ex);
        }
    }

This is the helper part:

loadFirstValue : function(component) {
        //this is necessary to avoid multiple initializations (same event fired again and again)
        if(this.typeaheadInitStatus[component.getGlobalId()]){ 
            return;
        }
        this.typeaheadInitStatus[component.getGlobalId()] = true;
        this.loadValue(component);

    },

    loadValue : function(component, skipTypeaheadLoading){
        this.typeaheadOldValue[component.getGlobalId()] = component.get('v.value');
        var action = component.get("c.getCurrentValue");
        var self = this;
        action.setParams({
            'type' : component.get('v.type'),
            'value' : component.get('v.value'),
        });

        action.setCallback(this, function(a) {
            if(a.error && a.error.length){
                return $A.error('Unexpected error: '+a.error[0].message);
            }
            var result = a.getReturnValue();
            component.set('v.isLoading',false);
            component.set('v.nameValue',result);
            if(!skipTypeaheadLoading) self.createTypeaheadComponent(component);
        });
        $A.enqueueAction(action);
    }

This piece of code simply calls the “InputLookupController.getCurrentValue” remote action that simply preload the selected record’s Name field (if any).

The “typeaheadInitStatus” property (see the full file code) is used to avoid multiple initialization of the component (the “createTypeaheadComponent” method is the method that is used to create the typeahead component: this must be executed only once!).

On the other side the “typeaheadOldValue” stores the current value, in order to understand if the value parameter is changed: this is extremely useful when some other part of the application changes the value parameters and so the component should refresh it’s inner value.

To achieve this simply see the “inputLookupRenderer.js” file:

rerender : function(component, helper){
        this.superRerender();
        //if value changes, triggers the loading method
        if(helper.typeaheadOldValue[component.getGlobalId()] !== component.get('v.value')){
            helper.loadValue(component,true);
        }
    }

This method is called every time the component is rerendered (e.g. when one of its parameters changes), and it constantly evaluates if the “value” parameter has changed since the last set; if this is true then we load again the component, without re-contructing it (“skipTypeaheadLoading” parameters of the loadValue function).

The typeahead is a simple porting of the Twitter Typeahead component with some changes, the most important of which is the call to the remote action that search (using SOSL) for all SObjects given the search term (“substringMatcher” function of the helper file):

// . . . 
    substringMatcher : function(component) {
        //usefull to escape chars for regexp calculation
        function escapeRegExp(str) {
          return str.replace(/[-[]/{}()*+?.^$|]/g, "$&");
        }

        return function findMatches(q, cb) {
            q = escapeRegExp(q);
            var action = component.get("c.searchSObject");
            var self = this;

            action.setParams({
                'type' : component.get('v.type'),
                'searchString' : q,
            });

            action.setCallback(this, function(a) {
                if(a.error && a.error.length){
                    return $A.error('Unexpected error: '+a.error[0].message);
                }
                var result = a.getReturnValue();

                var matches, substrRegex;

                // an array that will be populated with substring matches
                var matches = [];

                // regex used to determine if a string contains the substring `q`
                var substrRegex = new RegExp(q, 'i');
                var strs = JSON.parse(result);
                // iterate through the pool of strings and for any string that
                // contains the substring `q`, add it to the `matches` array
                $.each(strs, function(i, str) {
                    if (substrRegex.test(str.value)) {
                        // the typeahead jQuery plugin expects suggestions to a
                        // JavaScript object, refer to typeahead docs for more info
                        matches.push({ value: str.value , id: str.id});
                    }
                });
                if(!strs || !strs.length){

                    $A.run(function(){
                        component.set('v.value', null);
                    });
                }
                cb(matches);
            });
            $A.run(function(){
                $A.enqueueAction(action);
            });
        };
    }
    // . . .

This “complex” code is used to push the results from the “searchSObject” remote action to the typeahead result set picklist.

Finally you can add the component in your lightning app:

<aura:application >

    <aura:attribute name="id" type="String" default="" access="GLOBAL"/>
    <aura:attribute name="objNew" type="Contact" default="{'sobjectType':'Contact',  
                                                       'Id':null}" />

    <!-- ... -->

    <div class="well">
        <div class="panel panel-primary">
            <div class="panel-heading">Existent sobject</div>
            <div class="panel-body">
                <div class="form-horizontal" >
                    <div class="form-group">
                        <label class="col-sm-2 control-label">Contact</label>
                        <div class="col-sm-8">
                            <c:inputLookup type="Contact"  
                                         value="{!v.id}"
                                         className="form-control "/>
                        </div>
                    </div>
                    <div class="form-group has-feedback">
                        <label class="col-sm-2 control-label">Loaded contact</label>
                        <div class="col-sm-8 ">
                            <ui:inputText value="{!v.id}" 
                                          class="form-control"
                                          placeholder="Change id value"/>
                         </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="panel panel-primary">
            <div class="panel-heading">New sobject</div>
            <div class="panel-body">
                <div class="form-horizontal" >
                  <div class="form-group">
                    <label class="col-sm-2 control-label">Contact</label>
                    <div class="col-sm-8">
                      <c:inputLookup type="{!v.objNew.sobjectType}"
                                    value="{!v.objNew.Id}"  
                                    className="form-control "/>
                      </div>
                  </div>
                 <div class="form-group has-feedback">
                    <label class="col-sm-2 control-label">New contact</label>
                    <div class="col-sm-8 ">
                        <ui:inputText value="{!v.objNew.Id}" 
                                      class="form-control"
                                      placeholder="Change id value"/>
                     </div>
                  </div>
                </div>
            </div>
        </div>
    </div>

</aura:application>

We have two components, the first one ‘s value is grabbed directly from the “?id=XXXX” query string parameters, while the second one has no value on load:

Finally you can easily test that everytime you change the “value” input text, the inputLookup component loads it’s new “name” field, while if you search for a Contact on the component and select a record, the value input is set with the new value.

You brand new custom input lookup component is served!

[Salesforce / Apex] Integer vs Double math expressions

This is a quick post about some findings about how math expressions are handled by Apex engine, arisen from the developments of a personal package to reproduce Salesforce Formulas in Apex (and even more).

For those out there that are super expert on this field of programming, this post may seem simplicistic and “obvious”, but I think this could help a lot of guys out there that had the same problem!

Take this expression:

Double aValue = 1 / 5 * 5;

Since elementary school I learned that I could play with fractions, and in this particular example you simply reduce the 5 in the second member of the “/” operator with the third member after the “*” operator.

What’s the result?

It’s absolutely 1!

Wrong!

At first I thought I was the first to find an incredible bug, but after a while, thinking about how Java works, I understood the reason.

Try to execute this other statemanet:

Double aValue = 5 * 1 / 5;

What’s the result?

It is correctly 1.

The reason is how the “/” operator calculates:

  • If at least one operand is double/decimal, the operation is calculated in decimal style; that’s why 1.0/10 = 0.1 and 1.0/10*10 = 1 as expected
  • If both operands are integer numbers, the operation is done in the interger set of values, so 1 / 10 = 0 and the order of execution matters; that’s wky 1 / 10 * 10 = 0 and 10 * 1 / 10 = 1

In both cases the type of the result variable (Double aValue) does not affect the result, so aValue evaluates to “0.0” or “1.0”, that is a simple conversion from the integer result to a its double value.

[Salesforce / Apex / VisualForce] Getting rid of the “Save & New” button

In some circumstances the “Save & New” button on the standard layout is not desired.

This is an idea (there are others of this kind) to make this optional on layouts, but unfortunately it’s been freezed for years.

The simpler solution was to execute some javascript on a hone page component in order to hide every button that has the “save_new” name:
Remove Save & New button of the standard edit page.

With Summer ’15 you cannot use anymore custom home page components that contains script tags, so the hack is not valid anymore: if you try to create/edit an HTML Area Custom Component you get this info message:

This component contains code that is no longer supported. For security reasons, in Summer ’15 we will start removing non-supported code from HTML Area home page components. As a result, this component may stop working properly.

If you want to use JavaScript or other advanced HTML elements in your home page component, we recommend that you create a Visualforce Area component instead.

I had to find a solution for a client, and the “New” button override was the way.

The button I’m talking about is this one (on the Editing standard layout):

You simply have to override the “New” button on your selected objects, creating for each object a Visual Force page with a standard Controller and the following extension:

<apex:page standardController="Account" extensions="SObjectNewActionOverrideController" action="{!onLoadPage}">
</apex:page>

public class SObjectNewActionOverrideController {
    public String debug{get;Set;}
    private String SObjectPrefix = null;
    public SObjectNewActionOverrideController(ApexPages.StandardController controller){
        this.SObjectPrefix = controller.getRecord().getSObjectType().getDescribe().getKeyPrefix();
        
    }
    public PageReference onLoadPage(){
        this.debug = JSON.serializePretty(ApexPages.currentPage().getParameters());
        String retURL = ApexPages.currentPage().getParameters().get('retURL');
        //these are the conditions to understand that this is actually a "new" page
        //that comes from a previously "save" page in which the user clicked on "Save & New"
        if(ApexPages.currentPage().getParameters().get('save_new')=='1' 
           && retURL != null
           && retURL.startsWith('/'+SObjectPrefix)
           && retURL.indexOf('/', 4) < 0
           && !retURL.contains('?')
           && retURL.length() >= 15){
               PageReference pg = new PAgeReference(retURL);
               pg.setRedirect(true);
               return pg;
           }else{
               PageReference pg = new PAgeReference('/'+this.SObjectPrefix+'/e');
               for(String key : ApexPages.currentPage().getParameters().keyset()){
                   if(key == 'save_new' || key == 'sfdc.override') continue;
                   pg.getParameters().put(key, ApexPages.currentPage().getParameters().get(key));
               }
               //https://mindfiresfdcprofessionals.wordpress.com/2013/07/10/override-standard-button-in-specific-condition/
               pg.getParameters().put('nooverride','1');
               pg.setRedirect(true);
               return pg;
           }
    }
}

The code simply does a redirect to the “previously” edited record if we are coming from a save operation (and user clicks on “Save & New”), getting rid to the “New” action of the “Save & New”, or simply goes to the standard edit page (the retURL is evaluated: if it is not an object ID then goes to the New action).

As stated in the code, there are other places in which the user clicks on a “New” button but the “save_new” parameter is still passed by Salesforce (I don’t understand the reason though), but this time the user should see the “New” page.

These buttons are the “New” button on the Sobject main page (e.g. /001/o page):

And the related lists:

Comments and alternative solutions are really appreciated!

[Salesforce / Trailhead] The beginning of your Force.com journey

a{color:red !important;}

Few days ago the Salesforce team released 4 new Trailhead modules, and I decided finally to have a look at it…with my style.

What is my style?

I may say “all in” or “all or nothing” style.

I decided to do all the modules and see which topics were covered (and how deeply).

With year of Force.com development it’s been quite easy for me to complete in few hours all the modules, but I haven’t earned all the points available (I did some mistakes when answering to questions) and I must admint I also learned something (expecially in modules I don’t play a lot in my job).

After 16 modules I have to say this is a really good starting point to start learning the basics of the Force.com platform, and sometimes the documentation goes deeper than you could expect, covering important aspects that sometimes you find out only after months of “real world” practice.

The Trailhead modules page lists all the modules in alphabetical order, but you can follow the 3 steps on the Trailhead main page to have a better study order.

Recently some cool modules have been added:

  • Process Automation: you learn the basics of process automation and the new Process Builder and Visual Workflows tools, cool ways to get your complex processes up and running in few clicks!
  • Chatter Basics: this useful module teaches the basics of setting up Chatter on your org to improve social collaboration
  • Lightning Components: this is my favorite modulo above all, I love Lightning Components and can’t wait to see them out of the Beta stage. Learn base and advanced aspects of this new powerful framework
  • Visualforce Mobile: learn how to create slick and smooth visualforce pages on the Salesforce1 mobile platform

This is the complete list of available modules:

And (awesomely) these are “can’t wait” upcoming modules:

  • Reports & Dashboards: Build real-time reports and charts to visualize key business metrics
  • Apex Integration Services: Integrate with external apps using Apex REST and SOAP services
  • Asynchronous Apex: Write more efficient Apex code with asynchronous processing
  • Visualforce & JavaScript: Use JavaScript to enhance and customize your Visualforce pages
  • App Deployment: Learn best practices for team app development and lifecycle management

Stay tuned for more modules!

I’m gonna definitely suggest all my Company’s interns to start learning Force.com using Trailhead, a funny and awesome way to learn the Force!

And once for all:

[Salesforce / SSO] Implementing Delegated SSO

a{color:red !important;}

Playing with code is cool, but playing with useless stuff is even better 🙂

Ok, I’m kidding, I just want to say that sometimes you have to get your hands dirty to understand what lies underneath things and try to build useless stuff to see simple “Hello world!” appear!

This is the case of Delegated SSO.

Few weeks ago with Paolo (a colleage of mine), I was checking deeper on Salesforce SSO, trying to figure out from the docs how to implement it.

The first thing that came across my eyes was the difference between Delegated and Federated SSO.
It wans’t that clear at that time, that’s why Paolo played with the code for some time and did a cool thing that I reproduced in the following Github repo.

Federated SSO is done using well known protocols such as SAML, granting a secure identity provisioning: with Microsoft ADFS you can use your own company domain to log on your Salesforce CRMs as well.

Which is the problem with this kind of SSO?

To implement its bases you don’t need a single line of code.

Too bad we are dirty men that like to play with mud!

That’s why this post!

With Delegated SSO you need 2 actors:

  • An Identity provider (e.g. your Domain server)
  • The Salesforce ORG in which you want to be logged in without remembering username/password

The first wall you splash into is the fact the you have your ORG to be enabled to Delegated SSO: this should be enabled by Salesforce support, so you need a way to contact support (if you’re not using an ORG with built in support).

After your ORG is enabled to Delegated SSO, this is where the config has to be enabled in your “delegated” ORG:

The Delegated Gateway URL contains the URL of the webservice of the “delegating” server.

Then you have to enable the Is Single Sign-On Enabled flag in the Administrative Permissions section of your users’ profile.

This is where we got our hands dirty while making out research: why not using a Salesforce ORG as the identity provider?

Challenge accepted!

What happens with delegated SSO? When you try to log in into your delegated ORG, Salesforce at first try to access the “Delegated SSO” webservice: if this accept the request, than you are automatically logged in; if the server gives a KO, than the ORG checks for username/password to be correct.

This is the message that the delegated ORG sends to the identity provider:

<?xml version="1.0" encoding="UTF-8" ?>
<soapenv:Envelope
   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <Authenticate xmlns="urn:authentication.soap.sforce.com">
         <username>[email protected]</username>
         <password>myPassword99</password>
         <sourceIp>1.2.3.4</sourceIp>
      </Authenticate>
   </soapenv:Body>
</soapenv:Envelope>

It basically asks for a user/password couple with a source IP, and receives this response:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope 
   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <AuthenticateResult xmlns="urn:authentication.soap.sforce.com">
         <Authenticated>false</Authenticated>
      </AuthenticateResult>
   </soapenv:Body>
</soapenv:Envelope>

The Authenticated field conveys the OK/KO result.

You can use the password field to host a unique and temporary token to make the connection more secure.

To log-in from your identity provider page, use this example page (see the /apex/DelegateLogin page):

Each of this users is a delegated user on another ORG, that is stored in the DelegatedUser__c SObject:

It stores Username and Remote ORG ID, because it is used to create the login URL to make the authentication go smootly for the user:

public PageReference delegateAuthentication(){
 String password = generateGUID();
 insert new DelegatedToken__c(Token__c = password, 
         Username__c = this.usr.DelegatedUsername__c,
         RequestIP__c = getCurrentIP());
 String url = 'https://login.salesforce.com/login.jsp?'
    +'un='+EncodingUtil.urlEncode(this.usr.DelegatedUsername__c, 'utf8')
    +'&orgId='+this.usr.Delegated_ORG_ID__c 
    +'&pw='+EncodingUtil.urlEncode(password, 'utf8')
    +'&rememberUn=0&jse=0';
 //you can also setup a startURL, logoutURL and ssoStartPage parameters to enhance usre experience
 PageReference page = new PageReference(url);
 page.setRedirect(false);
 return page;
}

This way you can request a login action for every user you have stored in your objects (this case has the same ORG but you can have wathever ORG you want, no limits); the token is stored in a DelegatedToken__c SObject that is used to handle temporary tokens, usernames and IPs: this way, when the delegated ORG asks our ORG with this infos, our webservice can succesfully authenticate the requesting user.

This is done through the public webservice exposed by the RESTDelegatedAuthenticator class:

    @HttpPost
    global static void getOpenCases() {
        RestResponse response = RestContext.response;
        response.statusCode = 200;
        response.addHeader('Content-Type', 'application/xml');
        Boolean authResult = false;
        try{
            Dom.Document doc = new DOM.Document(); 
            doc.load(RestContext.request.requestBody.toString());  
            DOM.XMLNode root = doc.getRootElement();
            Map<String,String> requestValues = walkThrough(root);
            
            
            authResult = checkCredentials(requestValues.get('username'), 
                                          requestValues.get('password'),
                                          requestValues.get('sourceIp'));
        }catcH(Exception e){
            insert new Log__c(Description__c = e.getStackTraceString()+'n'+e.getMessage(), 
                       Request__c = RestContext.request.requestBody.toString());
        }finally{
            insert new Log__c(Description__c = 'Result:'+authResult, 
                       Request__c = RestContext.request.requestBody.toString());
        }
        String soapResp = '<?xml version="1.0" encoding="UTF-8"?>'
            +'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">'
            +'<soapenv:Body>'
            +'<authenticateresult xmlns="urn:authentication.soap.sforce.com">'
            +'<authenticated>'+authResult+'</Authenticated>'
            +'</AuthenticateResult>'
            +'</soapenv:Body>'
            +'</soapenv:Envelope>';
        response.responseBody = Blob.valueOf(soapResp);
    }

This webservice simply checks the incoming SOAP XML request, extracts the fields on the request and tests its values with the checkCredentials() method.

If the token is not expired you’ll be succesfully redirected to the new ORG logged as the user you wanted to.

A good practice is to use custom domains: you can thus replace “login.salesforce.com” with your “My Domain” of the corresponding ORG (you can also add a new field on the DelegatedUser__c SObject.

To enable public webservice, you simlpy need to create a new Site:

Then click on your Site, click the button “Public Access Setting” and add the RESTDelegatedAuthenticator to the Apex classes accessible by this public profile.

The comple code is right here in this Github repository.

May the Force.com be with you!

[Salesforce / RemoteAction] Using Date fields on a RemoteAction (PITA ALERT!)

I’ve just come through a bug on the Date field handling for Visual Force @RemoteAction.

We want to pass to a remote action a list of SObjects to be handled correctly.

This is the controller that hosts the @RemoteAction method:

public class MyController(){
   @RemoteAction
   public String doAction(List<contact> contacts){
      return 'All is ok!';
   }
}

This is the JS on the VF page:

function action(){
   var contact1 = {"LastName":"Smith",
                   "FirstName":"John",
                   "Phone" : "999-999-999"};
   var contact2 = {"LastName":"Brown",
                   "FirstName":"Mike",
                   "Phone" : "123-456-789"};
   var contactList= [contact1, contact2];
   Visualforce.remoting.Manager.invokeAction(
              '{!$RemoteAction.MyController.doAction}', 
              accList || [],
                function(result, event){
                    if(event.status){
                        alert(result);
                    }else{
                        //generic event error
                        alert(event.message);
                    }
                },{escape: false, timeout: 120000});
}

This runs perfectly.

But let’s add a Date field on the JS part:

function action(){
   var contact1 = {"LastName":"Smith",
                   "FirstName":"John",
                   "BirthDate":"1980-01-01",
                   "Phone" : "999-999-999"};
   var contact2 = {"LastName":"Brown",
                   "FirstName":"Mike",
                   "BirthDate":"1982-11-21",
                   "Phone" : "123-456-789"};
   var contactList= [contact1, contact2];
   Visualforce.remoting.Manager.invokeAction(
              '{!$RemoteAction.MyController.doAction}', 
              contactList || [],
                function(result, event){
                    if(event.status){
                        alert(result);
                    }else{
                        //generic event error
                        alert(event.message);
                    }
                },{escape: false, timeout: 120000});
}

And all messes up, stating that you are passing an incorrect type to MyController.doAction(List).

The problem is related to how the Date type serialization is done when invoking the Remote Action (it is apparently a bug not fixed yet).

The solution is to pass a serialized version of the JS array and deserialize it with Apex:

public class MyController(){
   @RemoteAction
   public String doAction(String serializedContacts){
      List<contact> contacts = (List<contact>)JSON.deserialize(objList,List<contact>.class);
      return 'All is ok!';
   }
}
function action(){
   var contact1 = {"LastName":"Smith",
                   "FirstName":"John",
                   "BirthDate":"1980-01-01",
                   "Phone" : "999-999-999"};
   var contact2 = {"LastName":"Brown",
                   "FirstName":"Mike",
                   "BirthDate":"1982-11-21",
                   "Phone" : "123-456-789"};
   var contactList= [contact1, contact2];
   Visualforce.remoting.Manager.invokeAction(
              '{!$RemoteAction.MyController.doAction}', 
              JSON.stringify(contactList || []),
                function(result, event){
                    if(event.status){
                        alert(result);
                    }else{
                        //generic event error
                        alert(event.message);
                    }
                },{escape: false, timeout: 120000});
}

Remember: don’t mess with JSON serialization!

[NodeJS] My Simple Static Webserver

a{color:red !important;}

When developing pure HTML5 mockups I usually don’t want to install a webserver or host my static pages on a remote webserver…
More over I sometimes like to create my own tools even if they are already on the net or they are super duper simple.

That’s why I created a simple NodeJS static webserver.

You can find that simple app in this Github repo.

To use it just type:

$ git clone https://github.com/enreeco/simple-static-web-server

$ node server

This will create a local webserver @ http://localhost:3000/index.html: all the files must be kept on the root folder.

If you want to specify another port simply type:

$ node server 1234

And the magic will be @ http://localhost:1234/index.html.

Before leaving remember my motto:

[Salesforce / Mobile SDK] Salesforce Mobile SDK 3.1 brings unified app architecture

I left mobile development in the far 2010, when Salesforce development became my actual job.
In the past 6 years I’ve gradually switched to web dev techs over native development, and this a kind of hurted my feelings, but it was a necessary path (I merged the passion for discovering new techs with learning something usefull for my “giving-money” job).

Few months ago I made up with my team a quick demo for a client using PhoneGap and the Salesforce Mobile SDK: it was really amazing, we created a fully working app (I’m not the wizard of UIs, but it was pretty amazing) in a couple of weeks.

With the new release of the Mobile SDK we have a unique set of interfaces shared between all platforms to link your app, so you don’t have to think to which feature you can use and which not, and dedicate your time to develop your awesome app in your preferred platform.

Here it is a link to the Mobile SDK Release Notes.

SmartSync for native and hybrid development is (in my opinion) the coolest utility feature in this release: this grants a common set of complete API to talk to the SmartSync layer, to ptimize data access when storing objects or doing cached queries.

This allow you to develop responsive and high qualities apps that work perfectly is an offline mode as well as online mode, creating a caching layer and a conflict resolution control.

The following is a cool mini-tutorial on how to detect conflicts when managing offline data while the details of the SmartSync usage.

For a complete example jump on this tutorial.

Other important features includes:

  • CocoaPods for iOS: iOS devs can now use CocoaPods to handle the Salesforce Mobile SDK
  • Gradle for Native Android Apps: Gradle is now supported for Android development, and will be available for Cordova Hybrid Dev in the next Cordova release
  • Certificate Based Authentication Pilot: no need for username/password request for your users to log in

This framework is getting new features release after release, and if you haven’t join our developer community and start build awesome apps!

[Salesforce] My favorite Spring ’15 features

a{color:red !important;}
h3{color:red !important;}

This is my top 10 list of the Spring ’15 Force.com platform update’s features.

Make Long-Running Callouts from a Visualforce Page

This is a really important feature. Imagine you have big VisualForce pages in which Apex methods, triggered by buttons / links / rerenders, do one or more callouts to external services.

Imagine hundreds of users use that page and the external services go down, thus causing timeouts on the callouts.

This could lead to heavy problems, because there can be no more than 10 long running Apex processes at the same time: this leads to unexpected and horrible output errors.

Using this new feature we can now “asynchronize” synchronous Apex callouts, using the new Connection Apex class, that is used like a callback when the callout has ended.
Basically you create a method split in 2 parts: the first (invoked by a button / link) has the starting callout code and the last receives the results from the callout.
Even if the callout/callouts (you can trigger up to 3 callout a time) fails / go timed out this call won’t count towards the logn running processes.

I’m gonna write down a post to show how this works using HttpRequests and also SOAP requests.

Set Up Test Data for an Entire Test Class

This is an extremely useful feature for large ORGs with hundreds of test classes.

This allow us to write “test setup” methods (new annotation @testSetup) that are called once per test class: when a test method is executed the context has all the objects created in those setup methods (you can have more than one method of this kind, but the execution order is not guaranteed). Each test method execution rollbacks its data at the end, so that the following class’s test method will see the values as if they were just been created.

Deploy Your Components in Less Time

Imagine you have the GO for a deploy in your production ORG but you have to do it at saturday midnight (and test methods requires more than an hour to run!!!!)…this is absolutely not cool!

This feature allow you to send a deploy to production before your deadline, test classes are run during this “freezed” deploy and it stays freezed for 4 days: when you are ready you simply click the “Quick Deploy” button and the components are deployed instantly! Whoooaa this is magic!

Create Named Credentials to Define Callout Endpoints and Their Authentication Settings

You can finally leave all the authentication mess to the Force.com platform and just sit on your screen and simply code you business requirement, magically storing sessions, tokens and whatever it is needed on Salesforce.

You can also use a named credential in a simplyfied way to store callout endpoints.

Business Continuity – Promote Business Continuity with Organization Sync

With Organization Sync you can setup a secondary ORG that you can use when your primary ORG needs some maintenance (e.g. a big release of your developments), giving a 24/7 service availability.

Orgs are synched automatically through a data replication mapping.

I haven’t already tested this feature, but being available on developer Orgs I’ll certainly try it soon.

Visually Automate Your Business Processes

This is the Lightning Process Builder, a cool visual tool that help you automates your business processes, from simple actions to complex combinations.

It seems really awesome but unfortunately it is not available on the pre-release program.
You better see in the next days tens of posts about this new feature!

Lightning Components (BETA)

Lightning components are still in beta and the builder in pilot.
But we have new additions:

  • New Components: brand new base components (such as select input, textarea, …)
  • Namespace Requirement Removed: finally no need to setup a namespace, easing the creation of packages and the deploy across orgs
  • Support Added for Default Namespace: follows the previous point
  • Extend Lightning Components and Apps: like classes, you can extend components and apps
  • Referential Integrity Validation Added: integrity validation has been boosted

New increased limits

There are some new increased limits:

  • Deploy and Retrieve More Components: limit increased from 5000 to 10000 components
  • Chain More Jobs with Queueable Apex: from 2 chained jobs to infinity (except for Developer and Trial orgs where the limit is 5)
  • Make Apex Callouts with More Data: size per callout (HTTP or SOAP) increased from 3 MB to 6 MB for synchronous APEX and 12 MB for asynchronous

Create or Edit Records Owned by Inactive Users

All users can now edit objects owned by inactive Users, before Spring ’15 only administrator could do it!
Believe me, this is really usefull.

Legitimize User Activity using Login Forensics (PILOT)

These are forensics metrics to identify suspicious behavior of users (such as logins from unusual IPs or excessive number of login among the average number).
This is a PILOT program, so you have to explicitly ask Salesforce to be enabled.

As usual, may the FOrce.com be with you guys!

[Salesforce / JS] Download automatically files from apex (using href link and base64)

This post is a recap of this Salesforce Developer Forum thread.

We want to trigger a download from an attachment but the running user don’t have access to the object (think of Community User for instance).

In the controller read the Attachment’s body in a String getter coded in Base64:

public String base64Value{get;set;}
public String contentType{get;set;}
public void loadAttachment(){
   Attachment att = [Select Id, Body, ContentType From Attachment limit 1];
   base64Value = EncodingUtil.base64Encode(att.Body);
   contentType = att.ContentType;
}

In the page:

<a href="data:{!contentType};content-disposition:attachment;base64,{!base64Value}">Download file</a>

Page 24 of 27

Powered by WordPress & Theme by Anders Norén