1

I want to change Owner of an Opportunity (an owner of an opportunity left company and all his opps need to be transferred to another user). Is there any way it can be done without validating record? What I do now is stupid

User toUser = ...;
Opportunity oppToTransfer = ... ;
oppToTransfer.OwnerId = toUser.Id;
update oppToTransfer;

And, as you understand, third line is where I'm having a problem (if opp has a problem).

I know that Opportunity has it's own rules in SFDC (like User or Leads). Maybe there is a way to do transfer without validating opportunity?

2 Answers 2

2

This is the reason we have added a profile check to all our validation rules:

$Profile.Name <> 'System Administrator' && (SomeOtherLogic)

Because generally, system administrators will only be performing special tasks like your example.

Another option is to create a 'Validation Manager' in custom settings, and add that as another / alternative layer of protection:

 $Setup.ValidationManager.Opportunity__c = true && $Profile.Name <> 'System Administrator' && (SomeOtherLogic)

Opportunity__c being a checkbox that would normally be set to true, only briefly set to false for scenarios where it is necessary to bypass validation. There would be one checkbox for each object of course.

3
  • 2
    Note that if you use the $Profile.Name <> ... approach and you have apex classes, many of those classes' testmethods need to do System.runAs(someUserOtherThanSysad) otherwise you may be inadvertently bypassing validation rules and leading to a false sense of having the tests be realistic.
    – cropredy
    Commented Aug 14, 2014 at 13:58
  • Good point, I should have said that - it's part of the reason for adding the custom setting option. Commented Aug 14, 2014 at 14:30
  • Yes, I like the custom setting option -- which needs to be a hierarchical custom setting as otherwise unavailable in VR
    – cropredy
    Commented Aug 14, 2014 at 16:12
0

You can't save a record in salesforce that isn't valid. So if it has become invalid because circumstances of other records have changed, and then you try to save it, you can't.

If the validation rules that are failing are your own rules, you can make edit them so that they only take effect in certain circumstances. For instance if you want to validate a particular field with a rule like this:

AND (StageName = 'Closed', ISBLANK(Some_Field__c))

meaning "You can't close the opportunity until you filled in Some_Field__c"

you can make the rule

AND (
OR (ISNEW(), ISCHANGED(StageName))
,  StageName = 'Closed', ISBLANK(Some_Field__c))

which means "You can't change the opportunity to closed, or create an opportunity with stage Closed unless you filled in Some_Field__c. But if somebody empties out the value of Some_Field__c after you saved and closed the opportunity, next time you come along and do something else to the opportunity, like change its Owner, you'll be fine".

No way of knowing if this strategy will work in your particular case, unfortunately.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .