function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ColbridgeColbridge 

How to create test data to test a closed opportunity which has a validation rule that prevents update?

With the validation rule active, getting validation error saying cannot update closed opportunity, while trying to insert the opportunity record. Any pointers to solve this?
Best Answer chosen by Colbridge
AnkaiahAnkaiah (Salesforce Developers) 
Hi Colbridge,

Please modify the  validation rule as like below and try will fix the error.
AND( $Profile.Name <> 'System Administrator', ISPICKVAL(PRIORVALUE(StageName), 'Closed Won') )

Thanks!!
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Colbridge,

Just insert the opportunity test data with status as closed. 

If this helps, please mark it as best answer.

Thanks!!
 
ColbridgeColbridge
No Ankaiah. I had put stage as Closed Won itself. Doesn't seem to work. The validation rule and test method code is as below:
 
Error Condition Formula: 

AND( OR($Profile.Name <> 'System Administrator'), ISPICKVAL(PRIORVALUE(StageName), 'Closed Won') ) 

Error Message: You cannot modify a closed opportunity. 

Code snippet:               
Opportunity opp = new Opportunity();         
opp.StageName ='Closed Won';         
insert opp;                 

Test.startTest();         
Test.setMock(HttpCalloutMock.class, new callAPI_Mock()); CallAPI.callServer(opp.id); Test.stopTest();

 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Colbridge,

Run your test class in system admin user mode, you will not get any error.
 
User u =[select id, name from user where profile.Name = 'System Administrator'];

system.runas(u){

Opportunity opp = new Opportunity();         
opp.StageName ='Closed Won';         
insert opp;                 

Test.startTest();         
Test.setMock(HttpCalloutMock.class, new callAPI_Mock()); CallAPI.callServer(opp.id); Test.stopTest();

}

If this hepls, Please mark it as best answer.

Thanks!!
ColbridgeColbridge
That looked it should work. Had multiple inactive users, so updated the SOQL to:

SELECT Id, Name FROM user WHERE profile.Name = 'System Administrator' AND IsActive = true LIMIT 1

But, still it didn't work. Same validation error is being thrown...
AnkaiahAnkaiah (Salesforce Developers) 
Hi Colbridge,

Please modify the  validation rule as like below and try will fix the error.
AND( $Profile.Name <> 'System Administrator', ISPICKVAL(PRIORVALUE(StageName), 'Closed Won') )

Thanks!!
 
This was selected as the best answer
ColbridgeColbridge
That fixed the problem! But wouldn't you still need to have the OR() when you have more than one profile to exempt?:
 
AND( OR($Profile.Name <> 'System Administrator', $Profile.Name <> 'Some Other Profile'), ISPICKVAL(PRIORVALUE(StageName), 'Closed Won') )
 
ColbridgeColbridge
Changed to this:

AND( AND($Profile.Name <> 'System Administrator',
$Profile.Name <> 'Some Other Profile'),
Text(PRIORVALUE(StageName)) =  'Closed Won' )
AnkaiahAnkaiah (Salesforce Developers) 
Hi Colbridge,

Yes, above formula is good. 

Thanks,
Ankaiah