+ Start a Discussion
Dovid BDovid B 

testing a formula

Is there any mechanism to test a formula? Kind of like the run anonymous apex functionality?

 

I created a formula to do lead assignment based on area code but it doesn't seem to be reassigning.

 

****Update

 

Actually it seems like the assignment code isn't running at all. I am using a VF on Sites with a custom controller to insert the lead. Will that bypass the assignment rules or still kick them off? If it does bypass them, how can I get them to trigger? (Preferably without a trigger)

Best Answer chosen by Admin (Salesforce Developers) 
Dovid BDovid B

I discovered my problem was do to the custom controller not firing off the assignment rules. Thanks to a response to my post on the Visualforce forum, the answer was to set the DMLOptions on the lead, which did the trick.

 

http://boards.developerforce.com/t5/Visualforce-Development/assignment-rules-not-firing-on-lead-insert-using-Sites-VF-page/m-p/296879#M37252

All Answers

Shashikant SharmaShashikant Sharma

You can test values of formula field in test methods.

 

Take an example  I ahve a formula field on Account object that gives me Today date. I want to test it then

 

My test class

 

@istest

private Class testFormula {


    private static TestMethod void  testM()
        {
            Account a = new Account(Name = 'Test');
            insert a;
            
            Account a1 = [Select SIXMonthBack__c from Account where id =: a.id];
            
            system.assertEquals(a1.SIXMonthBack__c  , Date.today());
            system.debug('******* a1'+ a1);
            
        }
}

 

Dovid BDovid B

these aren't formula fields. They are lead assignment rules, which I assume is some sort of workflow rule.

 

Would a test class kick off the assignment rules? All a test method would show me is if the lead owner had been reassigned but nothing about the evaluation of the formula.

 

Perhaps I should show the actual formula:

 

IF(CASE(VALUE(MID(Phone,2,3)), 
205,1, 
251,1, 
256,1, 
...
715,1, 
920,1, 
0) = 1, TRUE, FALSE)

 

I have a JS format function so I can assume the phone number is in (xxx) xxx-xxxx format. I then want to take the area code (using MID), convert it from text to number (using VALUE) and evaluate against a list of area codes.

 

Since CASE can't return a logical, I use 1/0 and then an IF to see if the CASE returned 1, if so the formula returns TRUE, or 0, if so the formula returns FALSE

 

But in any case, I see to have a bigger problem in that the assignment rules don't seem to fire at all using my form with custom controller in Sites. Anybody know why that would be?

Shashikant SharmaShashikant Sharma

I just tested result of workflow in test method and I got them and was able to verify my results. I hope then assignment rules also can be tested in tst class.

Dovid BDovid B

I discovered my problem was do to the custom controller not firing off the assignment rules. Thanks to a response to my post on the Visualforce forum, the answer was to set the DMLOptions on the lead, which did the trick.

 

http://boards.developerforce.com/t5/Visualforce-Development/assignment-rules-not-firing-on-lead-insert-using-Sites-VF-page/m-p/296879#M37252

This was selected as the best answer