Home > Apex, Visualforce > If Visualforce Blows, This Sucks

If Visualforce Blows, This Sucks

January 11th, 2010

I recently saw a tweet that included nothing but a link and a lot of salesforce.com related tags.

Visual Force Blows (link was shortened in original tweet so I couldn’t see title)

What could this be I pondered? A new product announcement? A glowing review of the force.com platform? Upon clicking the link I entered the “I hate Apex and Visualforce Zone” (Dun Dun Dunnnnnnnnnnn). Some dude on a blog (man, everyone has a blog now days) is trying to build a dynamic link menu with data pulled from salesforce objects and using visualforce to display. This seems pretty normal right? Let us check out some excerpts first:

On Apex and Visualforce:

Sounds great in theory, however in reality it makes you want to stab yourself. A lot.


I on the other hand would bathe in it if I could. Oh ya….Visualforce and Apex, sooo bubbly clean. Ya, that was weird. Let’s move on.

Problem is, it uses what is called MVC (Model View Controller, or some shit like that) which divides your logic code, from your design code (oh my god, they invented the idea of functions that can be called, how fucking astounding). Seems like a solid plan, cause your design team and your programming team get to work separately and not step all over each others code. Problem is, I am the design team, and the coding team. That means I get to deal with the whole project from the ground up and deal with all the over engineered bs.


Whoa whoa whoa! The harshness, it hurts, but wait a second, isn’t the MVC concept pretty common. Isn’t this what all of those crazy iPhone apps use? And about a gazillion other languages.

Separating the design team and programming team is not really the purpose of this as both must work together to build awesome apps (even if it is the same person). The real advantage is that the styling/layout and logic are separate. This makes it much easier to code, debug, troubleshoot, and rework in the future. Anyone who has used the old s-controls can atest to this fact. Let us continue.

So what is the developer trying to do. He (she?) wants to simply create a navigation menu with links that have sub links. With his (we will assume it’s a he, sorry ladies) approach is that it stores all of the link relationships in one object. At first this may seem like a more simple and streamlined approach but this will soon spin out of controll as seen by the post in question.

In cold fusion he said he could do this in 6 lines but it doesn’t look like the example includes the sub links. So how about a Visualforce solution that includes sublinks in 10 lines of Apex and 11 lines of Visualforce (including class name, Visualforce page, and componenet tags)? Oh ya! It also only took me about 30 minutes to build. Double oh ya!

The first thing I would do differently is break this up into two objects. Link and Sub Link. Why more objects? We can then use the out of the box relationships to query all sub links when we query the links. This will actually significantly reduce the amount of required code. Each object has two text fields, URL and Label. The Sub Link object also has a lookup to the Link object. This approach also makes it much easier to maintain from a admin/user perspective. You can see all sub links for a link on one page.

links

Before we jump in to the code you can check out the snazzy demo here:

http://tehnrddemos-developer-edition.na7.force.com/linkmenu

First let’s look at the controller for the custom component we will be using. What we do is query the link object and all sub links related to the “parent” link at the same time. Technically I could make this class have even less lines but this example follows best practices so I won’t.

public class linkMenu{
    List<Link__c> links;
 
    public List<Link__c> getLinks(){
        if(links == null){
            links = [select Label__c, URL__c, (select Label__c, URL__c from Sub_Links__r) from Link__c];
        }
        return links;
    }
}

Next up is the component. This loops through all of the parent links and outputs a hyperlink. For each parent it will then loop through the sub links and create a link that is indented slightly for each.

<apex:component controller="linkMenu">
    <apex:repeat value="{!links}" var="l">
        <apex:outputLink value="{!l.Url__c}">{!l.Label__c}</apex:outputLink><br/>
        <apex:repeat value="{!l.Sub_Links__r}" var="sl">                
            <apex:outputLink value="{!sl.Url__c}" style="margin-left:10px;">{!sl.Label__c}</apex:outputLink><br/>
        </apex:repeat>
    </apex:repeat>
</apex:component>

And finally the page. This is were it gets sort of complicated. Sorry about that.

<apex:page >
    <c:linkMenu/>
</apex:page>

So what have we learned today? I think if the other post is accurate in its statement that Visualforce blows than I am going to assume that this post really really sucks.

Oh, and don’t worry Apex and Visualforce creators, PMs, and team. I will protect you.

Apex and Visual Force (they are the bane of my existence on this planet and I would die a happy man if their creator(s) where beaten with reeds to within an inch of their life).

But seriously, what is up with this guy? I’ll admit some rants are funny but this is going over the top and you should try to keep it a little professional. Bridge burned. This is that last time I will provide any type of assistance to this guy.

EDIT: Oh darn! Just realized I named this post “If Visualforce Sucks, This Blows”. That was backwards. It is fixed now.

Categories: Apex, Visualforce
  1. Kenji776
    January 22nd, 2010 at 07:49 | #1

    I tried making the simple web service (even in a whole new class in case there was something goofy with that last one) and still the same error.

    Check out
    http://fpitesters.testbed.cs0.force.com/

    It calls the webservice onload (can use the button to force it as well). Same exact error. Can webservices not be called from a sandbox environment?

  2. Kenji776
    January 22nd, 2010 at 08:05 | #2

    Hey I made a discussion on the boards.

    http://community.salesforce.com/sforce/board/message?board.id=Visualforce&thread.id=20624

    We can stop spamming your blog now.
    Sorry.

  3. Richard
    January 22nd, 2010 at 12:24 | #3

    Kenji,

    Did you make this object accessible to the sites guest user? I didn’t realize you were doing this for a sites project. If you ran this locally inside your dev box you’d get it to run with your admin accounts session id, but since the sites user doesnt have access you’re running into issues there.

  4. Richard
    January 22nd, 2010 at 12:28 | #4

    Or of course I could read the thread on the developer forum and read that other people already found that this morning =x lol

    Glad you got this working finally. In case anyone else ever finds this blog with a similar issue, remember to check the security permissions for the sites user if you’re trying to use the api with sites.

  5. Kenji776
    January 22nd, 2010 at 13:00 | #5

    Yup mostly got it running now. Just trying to work some of the bugs out of the sub menu systems (its displaying the same links for all menus). For anyone who cares, this is the code I have for the component. Maybe it will help someone in the future. This is also my first time using jQuery, so it may be a little more cludgy than it has to be.

    jQuery(document).ready(function()
    {
    sforce.connection.sessionId = ‘{!$Api.Session_ID}’;
    var results = sforce.apex.execute(‘linkMenu’ , ‘getLinks’, {menuName: ‘blah’ });
    dump(results);
    html = ”;
    counter = 0;
    jQuery.each(results,function(intIndex, LinkItem)
    {
    links = ”;

    jQuery.each(results[counter].Link_Items__r.records,function(intIndex, LinkData)
    {
    links += ““+ LinkData.Label__c + ““;
    });
    links +=” “;

    html += ““+LinkItem.Label__c+”“+links+”";});
    var item = jQuery(html);
    // Insert the generated HTML – is slower to do one at a time but doing this way allows us to set Data
    document.getElementById(‘main-navigation’).innerHTML += html;
    counter++;
    }
    )

Comment pages
1 2 405