I wrote the following test class for a PRM deployment and received this crazy error when running the test:
System.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, portal account owner must have a role: []
I searched the message boards but couldn’t find any reference to the real culprit. My original thought was that the user being created was causing an error somehow. However, the real problem was that my user was not assigned a profile. Hopefully this post saves someone some time tracking down the solution.
Account a =new Account(Name='Test Account Name');
insert a;
Contact c =new Contact(LastName ='Contact Last Name', AccountId = a.id);
insert c;
User user =new User();
user.ProfileID=[Select Id From Profile Where Name='Some Profile'].id;
user.EmailEncodingKey='ISO-8859-1';
user.LanguageLocaleKey='en_US';
user.TimeZoneSidKey='America/New_York';
user.LocaleSidKey='en_US';
user.FirstName='first';
user.LastName='last';
user.Username='test@appirio.com';
user.CommunityNickname='testUser123';
user.Alias='t1';
user.Email='no@email.com';
user.IsActive=true;
user.ContactId= c.Id;
insert user;System.RunAs(user){// do all of my tests}
You will also see this same error (for the same reason) when trying to enable a contact as a partner or customer portal user.
The Force.com Utility Belt is a Google Chrome Extension that I wrote to make my life easier. While developing on the Force.com platform I consistently need access to Salesforce documents, message boards, code snippets, etc. and opening PDFs, searching documentation and looking up Visualforce component signatures becomes tedious. The extension evolved out my necessity to make myself more productive. I hope you find it useful as well. Please send me any ideas for enhancements that you have to make the tool better. Virtually anything you can do in Javascript is possible.
Displays the latest feed content from the Force.com blog in the bottom
Quick Reference Topics
The following sections are loaded into the extension with short cuts to topics that I frequently use allowing me quick access to stuff that I typically forget. The sections include:
Apex
Visualforce
Visualforce Components
SOQL
AJAX Toolkit
Web Services API
Once you choose a section you can drill down into a specific topic, view it in the extension and even open the actual page in a new browser tab.
Search for Stuff
Since the Quick Reference Topics don’t include access to every topic in the Salesforce documentation, you need a convenient way to search for stuff. The search interface allows you to select a scope and perform a keyword search against the specific site. The results are displayed in a new browser tab. Due to popular demand, the following sites are searchable.
ID Converter
The ID Converter is from my original extension so I rolled it into this one for convenience. It allows you to quickly create an 18 digit Salesforce ID from a 15 digit ID. Based upon some feedback I made a small change so that the extension tries to find a 15 digit ID from the tab’s current URL and pre-populate the conversion form field.
Please send me your feedback and suggestions for improvements.
Quinton Wall and Josh Long (SpringSource) conducted this early-morning webinar last Thursday in preparation of VMforce. Even though the webinar was essentially a high-level overview and demo of the Spring framework, there were some interesting VMforce nuggets. In addition to the screenshots below, Quinton did release that Salesforce will be using JPA to connect to the Salesforce database.
You can watch the recording of the webinar in case you missed it. If you are not familiar with Spring, it is a really good overview of the their frameworks, APIs and tools.
There is another webinar scheduled for September 9th, Cloud Computing for Java Developers, that will cover more technical details on VMforce. Don’t miss it!
I built the Chrome Extension Salesforce.com ID Converter last month and found it quite useful. I consistently need to check out the Salesforce.com developer guides (especially Apex and Visualforce) so I started on another extension to make my development tasks easier, the Force.com Utility Belt.
The extension quickly grew in functionality so I thought I would ask other developers and admins for suggestions on how to make it more useful. It currently has the following features:
Quick reference for Apex, Visualforce and Visualforce components
Converts 15 digit Salesforce IDs to 18 digit IDs
Quick link to open the Salesforce.com developer documentation page.
I hope to make the extension public soon but want to make some improvements first. I’m thinking about adding some sort of search functionality but how would you improve this extension? What features would you like to see?
In addition to the hackathon, there will be a number of speakers including Alex Himel from the Facebook Platform team. You can find the entire agenda here.
The meeting is being hosted by Isidorey and it’s been reported that the local watering hole will stay open until the last person leaves. They are giving away drink coupons so it might be a rough Friday.
Special thanks to Quinton Wall for giving me pre-release access to the new Facebook toolkit and source code. It’s slick, easy and sexy!
With the Spring ’10 release, Salesforce.com removed the limit on the number of items a collection can hold. So now, instead of ensuring that your collections contain no more than 1000 items, you have to monitor your heap size. Here are some strategies on how to write Apex scripts that run within these limits.
First of all, what is the heap? Dynamic memory allocation (also known as heap-based memory allocation) is the allocation of memory storage for use in a computer program during the runtime of that program. In Apex the heap is the reference to the amount of memory used by your reachable objects for a given script and request. When you create objects in your Apex code, memory is allocated to store these objects.
As with many other things in Force.com, there are governors and limits that prevent you from hijacking the heap and degrading the performance of other running applications. The heap limit is calculated at runtime and differs on how your code is invoked:
Triggers – 300,000 bytes
Anonymous Blocks, Visualforce Controllers, or WSDL Methods – 3,000,000 bytes
Tests – 1,500,000 bytes
These limits also scale with trigger batch sizes:
For 1-40 records, the normal limits apply
For 41-80 records, two times the normal limits apply
For 81-120 records, three times the normal limits apply
For 121-160 records, four times the normal limits apply
For 161 or more records, five times the normal limits apply
Luckily Salesforce.com increased the heap size limits in Summer ’10 but you still may run into some issues. Here are a few things you can do to write heap-friendly code.
Watch the Heap
When your scripts run you can view the heap size in the debug logs. If you notice your heap approaching the limit, you will need to investigate why and try to refactor your code accordingly.
Use the Transient Keyword
Try using the “Transient” keyword with variables in your controllers and extensions. The transient keyword is used to declare instance variables that cannot be saved, and shouldn’t be transmitted as part of the view state for a Visualforce page.
Use Limit Methods
Use heap limits methods in your Apex code to monitor/manage the heap during execution.
Limits.getHeapSize() – Returns the approximate amount of memory (in bytes) that has been used for the heap in the current context.
Limits.getLimitHeapSize() – Returns the total amount of memory (in bytes) that can be used for the heap in the current context.
// check the heap size at runtime
if (Limits.getHeapSize > 275000) {
// implement logic to reduce
}
One strategy to reduce heap size during runtime is to remove items from the collection as you iterate over it.
Put Your Objects on a Diet
If the objects in your collection contain related objects (i.e., Account objects with a number of related Contacts for each) make sure the related objects only contain the fields that are actually needed by your script.
Use SOQL For Loops
SOQL “for” loops differ from standard SOQL statements because of the method they use to retrieve sObjects. To avoid heap size limits, developers should always use a SOQL “for” loop to process query results that return many records. SOQL “for” loops retrieve all sObjects in a query and process multiple batches of records through the use of internal calls to query and queryMore.
1
2
3
4
5
6
7
8
for(List<Contact> contacts :[select id, firstname, lastname
from contact where billingState ='NY']){// implement your code to modify records in this batch// commit this batch of 200 records
update contacts;}
Note: There used to be a strategy for dealing with heap intensive scripts by moving them asynchronous, @Future methods. However, since the heap limits were increased in Summer ’10 this is no longer a reason to simply use @Future method as the limits are the same.
Any other ideas on how to effectively manage the heap?
Jason Ouellette, Appirio Chief Architect and author of Development with the Force.com Platform: Building Business Applications in the Cloud, will be conducting a developer webcast on development for the Force.com platform. The webcast will be next Thursday (August 19, 2010) @ 10 a.m. PDT / 1 p.m. EDT. Jason architected our PS Enterprise product which is reported to have 40,000-50,000 lines of Apex code. He definitely know how to develop application for the Force.com platform.
Jason has held numerous senior engineering and architecture positions at companies such as Composite Software, Informance and webMethods. At webMethods, Jason was the third employee and invented numerous technologies that made the company the leading provider of B2B infrastructure. He helped architect the industry’s first XML-based B2B server, then invented and led the development of the first B2B application for securely managing the exchange and processing of most common business document formats, such as XML and EDI, among trading partners over the Internet.
My article How to Load Large Data Sets with the Force.com Bulk API went up on developer.force.com yesterday. It shows how you can load data into Salesforce using the Bulk API either from a spreadsheet or directly from a database. You can find the code for the article at the code share project. Here’s an abstract from the article…
“Loading very large sets of data is becoming a common task for implementations, integrations and ongoing support. The standard Force.com Web Services API requires developers and administrators to implement complex processes to upload data in bite-sized chunks, monitor results and retry failed records. This method is acceptable for small loads of data but becomes unwieldy and time-consuming with large data sets that sometimes require numerous man-hours and multiple computers to operate in tandem.
The new Force.com Bulk API was designed specifically to offload the complex and time consuming process of importing large data sets from your client application to the Force.com platform. The Bulk API and supporting web interface allows you to upload and create import jobs, monitor and manage these jobs through an easy to use web interface, and receive notifications when the jobs complete. A few client applications already support the Bulk API but we’ll develop our own Java command line application that uses the Bulk API to upload large sets of data from a CSV file and database. This application can then be used as a starter for your own custom bulk data loader.”
Let me start out right away by saying I am not a DOM or CSS master. I’ve typically had employees or co-workers to perform these functions for my so I’ve (unfortunately) let me skills wane. However, I’m not totally clueless. I was trying to build the following Visualforce page a couple of days ago and working with the DOM and Visualforce components seems to be much harder than what it should be. Perhaps I’m doing something wrong? Luckily Wes Nolte and Joel Dietzare experts and they both have a number of great blog posts on this subject.
I was trying to create the following Visualforce page that contains a simple SelectList (I’ve sanitized the code for your protection).
When the user submits the form, the value of the currently selected option is fired off via JavaScript. Shouldn’t be a big deal, right? Based upon the Visualforce docs for Using JavaScript to Reference Components, I thought this code would work using the $Component variable (line #5).
Unfortunately Firebug choked with s null pointer for the object’s reference.
I found Wes’ great post, VisualForce Component Ids & Javascript, and took a look at the element Id that Salesforce generated for the SelectList component. Based upon the other elements in the DOM hierarchy, I hard-coded my JavaScript to use the id that Salesforce was generating and the method worked correctly!
3
4
5
6
7
<script language="javascript">
function doSubmit(){
alert(document.getElementById("thePage:theForm:thePageBlock:thePageBlockSection:thePageBlockItem:theSelectList").value);}</script>
So now I was confused. I dug deeper into Wes’ post, the code and comments. Wes’ solution was to assign the element value to a JavaScript variable just after it appears on the page (or at the same level within the page tree). My final (working) code looks like the following. Notice line #16 where I set the local JavaScript variable and then line #5 where I reference it’s currently selected value.
Wes states that this solution is simple as well as flexible. However, what happens if you have 20+ form fields that you need to process in this manner? Does accessing the DOM seem too hard with Visualforce components? Let me know if I am doing something wrong?
There has been a lot of buzz lately surrounding JP Seabury’s blog post, The Most Influential Salesforce.com Tech Bloggers. While it’s cool to be on the list with some of the other great people in the Salesforce community, I have to point out that there are a number of people that are making a huge impact by not blogging as regularly as they’d probably like to.
I don’t know how these guys do it. I can’t search Google or browse the message boards without seeing these guys helping other developers all over the place. And not only one or two sentences or lines of code but complete threads with source code and thoughtful discussions. Awesome stuff!
A couple of other great blogs to watch are Abhinav Gupta’s TECH GERM blog and Aslam Bari’s Technology Share blog. I’m not just saying that because they both work for us at Appirio, but they’ve honestly both been doing some really cool stuff so definitely check them out.