Hi, I'm Cathy, a Web Designer/Developer/WordPress Fanatic/Salesforce.com APEX Coder, etc. and I'm from the Philippines. Learn more about me...

Archive for the 'Salesforce.com' Category


Invoking Apex from a Custom Button using a Visualforce Page

Published by under Salesforce.com on Monday, February 22nd, 2010 at 4:42 pm

This post is about creating a custom button to be placed in a detail page using Visualforce page instead of an S-control (deprecated). I will be using Opportunity object for this example.

The Controller:

public class VFController
{
    private final ApexPages.StandardController theController;
    public List<Opportunity> listOpps = new List<Opportunity>();
 
    public VFController(ApexPages.StandardController stdController)
    {
          theController = stdController;
    }
    // Code we will invoke on page load.
    public PageReference autoRun()
    {
        for (Opportunity opp:[select id, name from Opportunity where id =:theController.getId()])
            {
	        opp.Name = opp.name + ' test';
                listOpps.add(opp);
	    }
		Database.update(listOpps);
 
	return theController.view().setRedirect(true);
    }
}

The Visualforce Page:

You don’t need to do anything special here. The most important part here is the parameter action = “{!autoRun}”.

<apex:page standardController="Opportunity"
extensions="VFController"
action="{!autoRun}"
>
<apex:sectionHeader title="Invoking Apex from a Button"/>
<apex:outputPanel>
You shoudn't see this page...
</apex:outputPanel>
</apex:page>

After that, go to Setup –> App Setup –> Customize –> Opportunities –> Buttons and Links. Add a new custom button with the following configuration.

This example simply appends “test” to the Opportunity name when you click on that custom button. This is only to show you that it works. Hope this helps. :)

One response so far

Batch Update SObject in Salesforce.Com (VB.Net)

Published by under Salesforce.com on Tuesday, May 19th, 2009 at 12:04 am

salesforce dice

I’m sharing this code because I know a lot of new salesforce developers are pulling their hair out looking for concrete samples on how to do API batch update. Yes it took me awhile to be able to solve this problem. APEX API documentation doesn’t give it all and it’s frustrating…

So much for that, here’s how I did it. My sample is updating a custom field in Opportunity Object using VB.Net.


Dim arrUpdateID As New ArrayList  'This ArrayList holds the Opportunity IDs I need to update, I didn't included the query - that will be your task
 
Dim objOppx(199) As Opportunity
 
For d = d To arrUpdateID.Count - 1
 
Dim updateOppx As New Opportunity
 
updateOppx.Id = arrUpdateID(d) ' You need an ID when you update
updateOppx.Downloaded__c = True ' This is a checkbox in SFDC where I'm setting it to true
updateOppx.Downloaded__cSpecified = True ' This is required when updating a boolean field
objOppx(d) = updateOppx ' Storing opportunity values to  objOppx which is an array of Oppty
 
Next
 
Dim sr() As SaveResult = sfdc.update(objOppx) ' This is where the updating happens, this line of code should be outside the loop
 
For j As Integer = 0 To sr.Length - 1
Dim msgerr As String
 
If Not sr(j).success Then
msgerr = "Error updating Opportunity ID: " + sr(j).id + ". The error was: " _
+ sr(j).errors(0).message
End If
 
Next

Note: You can only update 200 records in an Object at a time due to Governor limits, so you need to make some work around with that issue. I have done that through chunking up my array of IDs to update. I’ll leave that up to you! :)

I hope this post helps! There will be more to come as I learn Salesforce.com and APEX programming.

Stay tuned!

No responses yet

May the Force Be With Me

Published by under Salesforce.com on Wednesday, May 13th, 2009 at 12:20 am

salesforce-logoYes, I’m one of them now! I’m now part of the leader in Customer Relationship Management (CRM) & Cloud Computing…Salesforce.Com!

No responses yet