I recently came across an unusual eclipse IDE error: Cannot specify a gender for a gender neutral language
In the 7 years I've been doing Salesforce work, that error was a first. Fortunately, I found some folks who have also run into this -
https://developer.salesforce.com/forums/ForumsMain?id=906F00000008uyaIAA
Turns out if you are toggling between a language like Spanish/French and English while building something in Salesforce, you might need to delete the <Gender> tag in the object metadata.
Showing posts with label wtf. Show all posts
Showing posts with label wtf. Show all posts
Tuesday, April 7, 2015
Friday, March 20, 2015
Visualforce Email Templates
Hello, dear readers. I have been busy and haven't updated this blog in a while but I came across a couple unusual behaviors that are not well documented. In fact, one of the behaviors led to my support rep getting a knowledge article published. The topic for today is visualforce email templates and some things to think about if you are using them. Trust me, none of this is documented and even though Salesforce says "working as designed", I think you may want to look at your solution carefully.
Imagine Joe Q. Salesperson lands a sweet client one Friday morning, creates an opportunity, generates a quote and sends that quote to his boss for approval. Joe spends the weekend relaxing the afternoon away and the next day still hasn't gotten approval from his boss. Joe calls the boss and finds out he never got the email alert that something required his approval. Well, that's strange because Joe never saw an error and the quote was submitted for approval (he can see it in Salesforce). Furthermore, after his boss approved the email, the status in the email alert still said "Pending". What was going on? Joe calls IT and is severely irritated because he couldn't close the deal before the weekend and was seeing data in the email that didn't match what was in Salesforce.
So what is it with VF email templates?
Email Alerts with Visualforce Templates Do Not Always Send (and will not throw error)
Let that sink in for a minute - your trusty email alerts, even when the rule or approval process triggers the alert do not always send the email. And when they don't send, there isn't an error generated. Not even in the logs.
According to Salesforce, this is working as designed because Joe's administrator put a field on the VF email template that Joe did not have read-access to. Support was kind enough to generate some documentation of this behavior for us, after the fact. It should be noted that if your VF template uses a controller, your users will see an error if you don't give them access to the controller. It seems to me that the expected behavior here is that field level security should be applied. If the user has access to the field, display it, otherwise, hide it. Unfortunately that is not the case.
Email Alerts with Visualforce Templates Do Not Always Get the Latest Field Values
If you have an approval process with an action that performs a field update and sends an email alert, the email will not have the field update value if the template is a VF template. But, wait Salesforce's documentation says
It is the first bullet in the Field Update considerations. Except, it doesn't apply when it comes to VF templates. Again, support says it is working as designed. If you have a text or html template, the value from the field update is represented in the email, however, this is not the case with a VF template.
Imagine Joe Q. Salesperson lands a sweet client one Friday morning, creates an opportunity, generates a quote and sends that quote to his boss for approval. Joe spends the weekend relaxing the afternoon away and the next day still hasn't gotten approval from his boss. Joe calls the boss and finds out he never got the email alert that something required his approval. Well, that's strange because Joe never saw an error and the quote was submitted for approval (he can see it in Salesforce). Furthermore, after his boss approved the email, the status in the email alert still said "Pending". What was going on? Joe calls IT and is severely irritated because he couldn't close the deal before the weekend and was seeing data in the email that didn't match what was in Salesforce.
So what is it with VF email templates?
Email Alerts with Visualforce Templates Do Not Always Send (and will not throw error)
Let that sink in for a minute - your trusty email alerts, even when the rule or approval process triggers the alert do not always send the email. And when they don't send, there isn't an error generated. Not even in the logs.
According to Salesforce, this is working as designed because Joe's administrator put a field on the VF email template that Joe did not have read-access to. Support was kind enough to generate some documentation of this behavior for us, after the fact. It should be noted that if your VF template uses a controller, your users will see an error if you don't give them access to the controller. It seems to me that the expected behavior here is that field level security should be applied. If the user has access to the field, display it, otherwise, hide it. Unfortunately that is not the case.
Email Alerts with Visualforce Templates Do Not Always Get the Latest Field Values
If you have an approval process with an action that performs a field update and sends an email alert, the email will not have the field update value if the template is a VF template. But, wait Salesforce's documentation says
- Field updates occur before email alerts, tasks, and outbound messages
It is the first bullet in the Field Update considerations. Except, it doesn't apply when it comes to VF templates. Again, support says it is working as designed. If you have a text or html template, the value from the field update is represented in the email, however, this is not the case with a VF template.
Monday, December 16, 2013
Deep Thoughts on Apex Test Methods
You're good enough.
You're smart enough.
You can write a good apex test method.
I just completed a major rewrite of all test methods for a client and while it was painful at times, I think it puts them in a position to extract some value from what was previously just a production deployment hurdle. Trust me, I'm not yet a full test-driven-development convert but I do believe that you can help your business automate some testing, and maybe even save some cash, if you take the time to think about your testing and apply it to your test methods.
You get better at the things you do over and over and writing good test methods is certainly something you can expect to have plenty of opportunity to practice. There are some great resources out there to be sure you are repeating good habits. I'd start with Dan Appleman's Advanced Apex book as he has some great ideas for test class writing. Some other articles that I think are helpful and instructive are:
http://jessealtman.com/2013/09/proper-unit-test-structure-in-apex
http://wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests
With this recent rewrite effort, some of the good practices I've incorporated are:
This is certainly not the complete list of best practices, but it's a good start.
As with other things in Salesforce, there is some room for improvement with the execution of unit testing in the application. In particular, I'm still frustrated by what Jeff Douglas calls the "black art". Like Jeff, I've come across some peculiar behaviors that can be maddening. For example, if you are testing a trigger and need to create a user and then test the dml, there's a pretty good chance you're going to get a mixed DML exception. What is maddening, however, is that the error will not be caught in the force.com ide. Oh, and you can deploy to production with this too! The only thing keeping me sane was knowing that someone else noticed this too:
And aside from the nonsense of trying to estimate your code coverage in any of the tools out there, it would be nice if the test classes had the code coverage estimation like the functional classes for those of us separating them:
And don't get me started on the new test execution screens. Aside from queuing your tests, they provide no value!
I think there is plenty to like about putting some effort into doing proper unit testing in Salesforce. I'm sure if you build in the time to your sprints/plans, it will pay dividends in the long term. Just be sure you approach this with a good sense of humor.
You're smart enough.
You can write a good apex test method.
I just completed a major rewrite of all test methods for a client and while it was painful at times, I think it puts them in a position to extract some value from what was previously just a production deployment hurdle. Trust me, I'm not yet a full test-driven-development convert but I do believe that you can help your business automate some testing, and maybe even save some cash, if you take the time to think about your testing and apply it to your test methods.
You get better at the things you do over and over and writing good test methods is certainly something you can expect to have plenty of opportunity to practice. There are some great resources out there to be sure you are repeating good habits. I'd start with Dan Appleman's Advanced Apex book as he has some great ideas for test class writing. Some other articles that I think are helpful and instructive are:
http://jessealtman.com/2013/09/proper-unit-test-structure-in-apex
http://wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests
With this recent rewrite effort, some of the good practices I've incorporated are:
- Moving test methods from functional classes into separate Test Classes
- This allows you to decouple your tests from your functional classes, which excludes your tests' ability to reach private methods. This will give you some flexibility with refactoring your code without being tied down by your test methods.
- Asserting results
- While you may achieve the 75% goal of code coverage, your tests will have little/no value if you are not actually checking expected versus actual results. As a managed package developer, you'll also get flagged during the security review if you are not asserting your results.
- Centralizing and standardizing helper utilities
- Like other apex you write, you should try to encapsulate where you can and use helpers to minimize the effort in testing various permutations of data against your code.
- Testing negative scenarios
- This is another area where you can get some value out of unit testing. While it may take 80% of your effort to identify and code these, it's going to yield lots of value in improving your code and confidence in your code handling atypical scenarios.
- Testing as an end user
- Unfortunately as developers, we are system admins and almost everything we test works as expected because we don't have to deal with sharing or role hierarchy or object/field visibility. However, in the real world, our users are almost never system admins, so testing as a system admin makes no sense.
This is certainly not the complete list of best practices, but it's a good start.
As with other things in Salesforce, there is some room for improvement with the execution of unit testing in the application. In particular, I'm still frustrated by what Jeff Douglas calls the "black art". Like Jeff, I've come across some peculiar behaviors that can be maddening. For example, if you are testing a trigger and need to create a user and then test the dml, there's a pretty good chance you're going to get a mixed DML exception. What is maddening, however, is that the error will not be caught in the force.com ide. Oh, and you can deploy to production with this too! The only thing keeping me sane was knowing that someone else noticed this too:
And aside from the nonsense of trying to estimate your code coverage in any of the tools out there, it would be nice if the test classes had the code coverage estimation like the functional classes for those of us separating them:
And don't get me started on the new test execution screens. Aside from queuing your tests, they provide no value!
I think there is plenty to like about putting some effort into doing proper unit testing in Salesforce. I'm sure if you build in the time to your sprints/plans, it will pay dividends in the long term. Just be sure you approach this with a good sense of humor.
Wednesday, November 20, 2013
Traffic Nightmare @ Dreamforce
This is incredible..last year, the SF Giants were in the playoff hunt so downtown was already jamming. I can't even imagine trying to drive anywhere with over 140k registered attendees this year:
Hey @Benioff, can you do something about this? Maybe next year, a little more dream and a little less force. pic.twitter.com/fqqrHJB7g4
— dick costolo (@dickc) November 20, 2013
Monday, November 18, 2013
Drink the Kool Aid, It's DreamForce Season Again!
Salesforce1 appears to be the big announcement this year. I just watched this video that Salesforce published and while the music and voice over are great, I don't understand a thing about what is being announced. If you can make sense of this video, please share in the comments.
Wednesday, November 13, 2013
A Bug!
It's not often that I come across a real bug with Salesforce's apex or visualforce platform. Most often there are limitations or shortcomings that you have to workaround. Recently, I had to make an urgent change to a trigger and it's associated test class. However, when I attempted to comment out a line in my test class, I got the following error in the editor when I tried to save:
When I attempted to make the same change in the developer console, I got another error:
**Update Nov 13**
Salesforce has responded and indicated that it was a bug but has been fixed. Details here: https://success.salesforce.com/issues_view?id=a1p30000000T17j
java.lang.reflect. InvocationTargetException
When I attempted to make the same change in the developer console, I got another error:
An unexpected error has occurred. 421011484-16071 (1420197083) for deploymentId= 1drJ00000002FDxIAM If this persists, please contact customer support.
Fortunately, I was able to still deploy my code without the test class change but I opened a case anyway and after waiting a few days for a reply, was told that it was a known issue. The instructions from developer support were:
- Please clear Test results and try to save the code:
- From Setup, click Develop | Apex Test Execution |View Test History | Clear test results.
However, even before I did these actions, I tried to update the test class again and surprise! no errors. So, something fishy is going on... Support wants to close the case but I'm inquiring for additional details. Will keep you posted.
**Update Nov 13**
Salesforce has responded and indicated that it was a bug but has been fixed. Details here: https://success.salesforce.com/issues_view?id=a1p30000000T17j
Friday, August 30, 2013
A Riddle, Wrapped in a Mystery; Inside an Enigma
Do you ever see inconsistent results when running unit tests in a sandbox versus running them in production? Or, inconsistent results when running unit tests between sandboxes? Well, the black-box that is the Salesforce unit test, just became blacker and boxier to me.
I was trying to deploy a patch to production and was going through our normal build path that passes through our full sandbox. When attempting to deploy to the full sandbox, our automated build process failed on a test method that we had not changed in any way.
The error: ... System.TypeException: Invalid date/time: 05/05/2010 00:00 AM stack...
A colleague and I inspected the method and the class and determined that they were identical in our sandbox and production. When we ran the individual test in our full sandbox, it failed, and when we ran it in our production environment, it passed.
What the?!
The error pointed to this fragment of code in the test: datetime.parse('05/05/2010' + ' 00:00 AM');
Not sure why it was 00:00 AM so I updated it to 12:00 AM and was able to proceed with the deployment.
I was unsettled by experience because we had been able to deploy to this sandbox two weeks earlier and had not had this test method fail. So, we opened a case with Salesforce and asked their "premier" support to explain it. Our first support rep reproduced the results but gave us a canned response that it wasn't a good practice to use 00:00 AM. Fine, we said, but tell us why it passes production tests but not tests in our full sandbox and what exactly changed in our sandbox? We went on and on like this for a couple days, escalated the case, and are now doing the same dance with another "premier" support rep.
For those of you who read this, please try this at home and let me know if you can replicate this in your sandboxes:
Create a test object called TestDate__c. Add one custom field called SomeDate (type = date/time).
Create an apex class (api version 27, for consistency sake) with the following code:
global with sharing class TestingDate
{
public static testMethod void testMyObj()
{
TESTDate__c myobj = new TESTDate__c();
myObj.SomeDate__c = datetime.parse('05/05/2010' + ' 00:00 AM');
insert myobj;
}
}
Run your test. The results we got were as follows:
NA14 (developer org): Pass
CS15: Fail
CS12: Pass
If nothing changed on CS15, how can this be? Still waiting for an answer...
I was trying to deploy a patch to production and was going through our normal build path that passes through our full sandbox. When attempting to deploy to the full sandbox, our automated build process failed on a test method that we had not changed in any way.
The error: ... System.TypeException: Invalid date/time: 05/05/2010 00:00 AM stack...
A colleague and I inspected the method and the class and determined that they were identical in our sandbox and production. When we ran the individual test in our full sandbox, it failed, and when we ran it in our production environment, it passed.
What the?!
The error pointed to this fragment of code in the test: datetime.parse('05/05/2010' + ' 00:00 AM');
Not sure why it was 00:00 AM so I updated it to 12:00 AM and was able to proceed with the deployment.
I was unsettled by experience because we had been able to deploy to this sandbox two weeks earlier and had not had this test method fail. So, we opened a case with Salesforce and asked their "premier" support to explain it. Our first support rep reproduced the results but gave us a canned response that it wasn't a good practice to use 00:00 AM. Fine, we said, but tell us why it passes production tests but not tests in our full sandbox and what exactly changed in our sandbox? We went on and on like this for a couple days, escalated the case, and are now doing the same dance with another "premier" support rep.
For those of you who read this, please try this at home and let me know if you can replicate this in your sandboxes:
Create a test object called TestDate__c. Add one custom field called SomeDate (type = date/time).
Create an apex class (api version 27, for consistency sake) with the following code:
global with sharing class TestingDate
{
public static testMethod void testMyObj()
{
TESTDate__c myobj = new TESTDate__c();
myObj.SomeDate__c = datetime.parse('05/05/2010' + ' 00:00 AM');
insert myobj;
}
}
Run your test. The results we got were as follows:
NA14 (developer org): Pass
CS15: Fail
CS12: Pass
If nothing changed on CS15, how can this be? Still waiting for an answer...
Friday, August 23, 2013
Salesforce Sandbox Instances
A while back, I was on a client site that was affected by a sandbox outage that lasted over 3 days. Unfortunately, both of our full copy sandboxes were on this instance and a good number of our developer/configuration sandboxes were as well. Needless to say, we were greatly affected by the outage and when we asked about migrating some of our sandboxes to another instance to reduce the risk for our team, we were told that it was not possible. Today, again, there is another major outage, this time on CS15, and again, most of our development, testing, and full copy sandboxes are affected. It seems crazy to me that customers who seek the cloud for protection against this type of outage do not have the ability to specify that they want their sandboxes distributed across multiple instances. If we have a break/fix situation, we are in a difficult situation given that our development path is dependent on CS15 being operational. Customers should be allowed to spread their developer sandboxes across instances to reduce the impact of another outage.
Vote for the idea here.
Tuesday, August 13, 2013
Required InputTextArea
I was asked to make an inputtextarea required on a visualforce page and thought, "ok, no problem, 2 minutes!". In reality, this was kind of a nightmare to implement. My vf page looked something like this:
<apex:pageblock id="pageBlock">
<apex: pagblocksection id="pageBlockSec">
<apex:pageblocksectionitem>
<apex:outputlabel value="Big Field">
<apex:inputtextarea value="{!myObject__c.Big_Field__c}" required = true id="bigfield"}"
...
So the first issue is that the iconic redline next to the required field does not display for text areas. To fix this, you have to wrap the tag with an outputpanel like this:
<apex:pageblock id="pageBlock">
<apex: pagblocksection id="pageBlockSec">
<apex:pageblocksectionitem>
<apex:outputlabel value="Big Field">
<apex:outputPanel styleClass="requiredInput" layout="block">
<apex:outputPanel styleClass="requiredBlock" layout="block"/>
<apex:inputtextarea value="{!myObject__c.Big_Field__c}" required = true id="bigfield"}"
</apex:outputPanel>
....
When unit testing, the error that is displayed is something like:
This is not a message a user would understand, even with the text area marked with a red line. Some of the initial searches turned up crazy solutions like using jquery to clean up the message, or rewriting the validation to occur within the controller. So, it took a while but the solution was buried in this thread.
To remove the garbage text in the error, you have to provide the textarea a label attribute. Your final markup will look something like this:
<apex:pageblock id="pageBlock">
<apex: pagblocksection id="pageBlockSec">
<apex:pageblocksectionitem>
<apex:outputlabel value="Big Field">
<apex:outputPanel styleClass="requiredInput" layout="block">
<apex:outputPanel styleClass="requiredBlock" layout="block"/>
<apex:inputtextarea value="{!myObject__c.Big_Field__c}" required = true id="bigfield" label = "Big Field"}"
</apex:outputPanel>
....
<apex:pageblock id="pageBlock">
<apex: pagblocksection id="pageBlockSec">
<apex:pageblocksectionitem>
<apex:outputlabel value="Big Field">
<apex:inputtextarea value="{!myObject__c.Big_Field__c}" required = true id="bigfield"}"
...
So the first issue is that the iconic redline next to the required field does not display for text areas. To fix this, you have to wrap the tag with an outputpanel like this:
<apex:pageblock id="pageBlock">
<apex: pagblocksection id="pageBlockSec">
<apex:pageblocksectionitem>
<apex:outputlabel value="Big Field">
<apex:outputPanel styleClass="requiredInput" layout="block">
<apex:outputPanel styleClass="requiredBlock" layout="block"/>
<apex:inputtextarea value="{!myObject__c.Big_Field__c}" required = true id="bigfield"}"
</apex:outputPanel>
....
When unit testing, the error that is displayed is something like:
pageBlock:pageBlockSec:j_id38:bigfield: Validation Error: Value is required.
This is not a message a user would understand, even with the text area marked with a red line. Some of the initial searches turned up crazy solutions like using jquery to clean up the message, or rewriting the validation to occur within the controller. So, it took a while but the solution was buried in this thread.
To remove the garbage text in the error, you have to provide the textarea a label attribute. Your final markup will look something like this:
<apex:pageblock id="pageBlock">
<apex: pagblocksection id="pageBlockSec">
<apex:pageblocksectionitem>
<apex:outputlabel value="Big Field">
<apex:outputPanel styleClass="requiredInput" layout="block">
<apex:outputPanel styleClass="requiredBlock" layout="block"/>
<apex:inputtextarea value="{!myObject__c.Big_Field__c}" required = true id="bigfield" label = "Big Field"}"
</apex:outputPanel>
....
Friday, June 7, 2013
Deployment Failure
I recently assisted an organization with developing a couple of triggers to help them roll up some child data onto the parent records. On the evening we were set to deploy, we ran into a couple issues, both of which could be filed under "WTF":
1. Change Sets were disabled for the organization
2. Deploying via Eclipse generated over 150 errors in the managed packages that were installed in their org
The client's system admin cases opened for both issues - the first with Salesforce, the second with the managed package vendor.
The response from the managed package vendor was illuminating so I wanted to share. They forwarded us this community thread in which the question of managed package errors was settled:
http://boards.developerforce.com/t5/Apex-Code-Development/Unit-Test-Code-Coverage-and-Managed-Packages/m-p/471121#M86324
In summary: if you deploy with change sets, managed package code is ignored. If you deploy with Eclipse, you're out of luck if there are test class failures in the managed package.
Will update the blog with Salesforce's explanation of issue #1.
1. Change Sets were disabled for the organization
2. Deploying via Eclipse generated over 150 errors in the managed packages that were installed in their org
The client's system admin cases opened for both issues - the first with Salesforce, the second with the managed package vendor.
The response from the managed package vendor was illuminating so I wanted to share. They forwarded us this community thread in which the question of managed package errors was settled:
http://boards.developerforce.com/t5/Apex-Code-Development/Unit-Test-Code-Coverage-and-Managed-Packages/m-p/471121#M86324
In summary: if you deploy with change sets, managed package code is ignored. If you deploy with Eclipse, you're out of luck if there are test class failures in the managed package.
Will update the blog with Salesforce's explanation of issue #1.
Friday, May 4, 2012
In the voice of Dr. Evil, "1 Billion records per hour!"
On a Friday night, we did a deployment that involved a data update into Salesforce. Whilst using the Apex Data Loader utility, I achieved a unprecedented 1 billion records per hour rate from Salesforce! This is over 300,000 records per second!
Thursday, May 3, 2012
Indexing Delay?
A business user recently showed me that if she created a contact in Salesforce and then searched for that contact, that the contact would not be found. It was so strange to see this that I opened a case with Salesforce support. Support recently replied and said that this was expected behavior and that newly created records may not be searchable for up to 30 minutes while indexing is processing! 30 minutes!!
Subscribe to:
Posts (Atom)



