1

I have an custom object called Events__c, which returns a image banner link stored in a URL field. I'm using this data to display the banner in a Visualforce page during a registration window, based on date boundaries.

My controller has a getter method that constructs the banner in form of clickable link with an embedded image and returns to the VF page. I'm using an outputText call in the VF page with escape="false" attribute to render the banner as HTML.

Controller getter

    public String eventBanner {
    get {

        // Today
        Date today = Date.today();
        String banner = '';

        // Get upcoming events
        List<Event__c> lstEvent = [
                SELECT
                        Id,
                        Registration_Start__c,
                        Registration_End__c,
                        Registration_Banner__c
                FROM
                        Event__c
                WHERE
                ( Registration_Start__c <= :today AND Registration_End__c >= :today )
        ];

        // Results returned
        if ( lstEvent.size() > 0 ) {

            for ( Event__c mc : lstEvent ) {

                // Registration
                if ( mc.Registration_Start__c <= today &&
                        mc.Registration_End__c >= today &&
                        mc.Registration_Banner__c != null ) {

                    banner = '<a href="/Register?Id=' +
                                    mc.Id +
                                    '">' +
                                    '<img src="' +
                                    mc.Registration_Banner__c +
                                    '"/></a>';

                } // if

            } // for

        } // if

        // Return
        return banner;

    }
}

VF page

    <apex:outputText value="{!eventBanner}"
                     escape="false"/>

All of this is working fine. My issue is with the block of code I'm using to construct the banner HTML.

                banner = '<a href="/Register?Id=' +
                                mc.Id +
                                '">' +
                                '<img src="' +
                                mc.Registration_Banner__c +
                                '"/></a>';

To me this is a very crude & inefficient approach at crafting the HTML block by string concatenation. Are there any supporting Apex classes that allow you to take a more elegant approach?

Thanks.

1

Seems like need only the first record from the query result, so you can directly return from there. Also, the date conditions are not necessary as those rows will be filtered in the SOQL query itself.

Notice that I have changed the return type of eventBanner property and I am returning the Event__c record itself.

public Event__c eventBanner {
    get {

        // Today
        Date today = Date.today();
        String banner = '';

        // Get upcoming events
        List<Event__c> lstEvent = [
                SELECT
                        Id,
                        Registration_Start__c,
                        Registration_End__c,
                        Registration_Banner__c
                FROM
                        Event__c
                WHERE
                ( Registration_Start__c <= :today AND Registration_End__c >= :today )
        ];

        // Results returned
        if ( lstEvent.size() > 0 ) {

            for ( Event__c mc : lstEvent ) {

                // Registration
                if ( mc.Registration_Banner__c != null ) { // no need to check other conditions as its already done in the query itself.

                    return mc;

                } // if

            } // for

        } // if

        // Return
        return new Event__c();

    }
}

On the VF page, you can use that variable directly like this.

<apex:outputText rendered="{!NOT(ISBLANK(eventBanner.Id))}">
    <a href="{! '/Register?Id=' + eventBanner.Id }">
        <img src="{!eventBanner.Registration_Banner__c}"/>
    </a>
</apex:outputText>
1
  • 1
    Rahul - thank you very much. This is definitely a MUCH better way to go about it. As for the query results - you're right. This query is supposed to return only one event at a time. However, with a similar block of code elsewhere I was facing this exception "List has no rows for assignment to SObject" when the query result was null - so I'm using a List<> to prevent that exception from being thrown. Mar 10 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.