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

Trigger fails because of validation rule - workaround?

I have a trigger that updates 1 field on the opportunity that is a summary of several fields on a custom object that has a lookup relation.  I cannot change this to a master-detail relationship, because the custom object needs exposure in the customer portal.  The problem I am running into that there are several validation rules running on the opportunity that disallow saving.  I cannot figure out how to get this field updated on the opportunity while leaving the validation rules in place.  Any ideas?  Its like I want to tell the valuation rule to ignore the rule if the update is due to the trigger.




If you are updating an existing opportunity, and only updating one field, I don't see how a validation rule would keep you from updating the opportunity. What is the validation rule doing? Can you post your trigger code?


Well, the validation rule does not allow an opportunity to save if it meets the criteria below.  I am totally at a loss as to how to work around this.  The field that I update on the opportunity via the trigger has nothing to do with these validation rules.  I have been messing around trying to see if I could write something in the validation rule that will allow everything to run and update the Cost field, even if the rule evaluates to True, but I don't think I have enough experience to figure this one out.  Whatever the solution, it needs to work both from data loader and the user interface.


I really, really appreciate your help on this!!!



Error Message:  Close Date must be in the FUTURE - Please change Close Date

OR (
ISPICKVAL(StageName ,"Job Dispatched"),
ISPICKVAL(StageName ,"Waiting for Approval"),
ISPICKVAL(StageName ,"Searching for sub"),
ISPICKVAL(StageName ,"Waiting for Approval"),
ISPICKVAL(StageName ,"Approval Received - Need Contractor"),
ISPICKVAL(StageName ,"Priority - Searching for Sub"),
ISPICKVAL(StageName ,"Proposal/Price Quote")),
CloseDate < TODAY(),
IF(OR(ISNEW(), ISNULL(Id)), if(ConnectionReceivedId =null, TRUE, FALSE),NOT(AND(ISCHANGED(ConnectionReceivedId), NOT(ConnectionReceivedId = null)))),

NOT(Contains($User.Alias, "pnet"))

Additionally, this validation rule is also causing the trigger to fail:


ISPICKVAL(PRIORVALUE(StageName),"Closed Won"),
NOT($Profile.Id = "00e30000000c8cX")

Error Message
You cannot edit this opportunity as all opportunities marked "CLOSED WON" are locked from editing. If a change must be made to this record please discuss with Operations Manager or Supervisor.





The trigger updates a Cost field on the opportunity, everytime a custom object Work Order is modified:


trigger triggerWorkorderOppUpdate on Work_Order__c (after insert, after update, after delete) {

    // Build a LIST of Opportunity ID's that will
    // need recalculating
    set <id> oppIDs = new set <id>();
    list <Opportunity> opportunity = [SELECT ID FROM Opportunity WHERE ID in:oppIDs];

    if(Trigger.isInsert || Trigger.isUpdate){
        for(Work_Order__c w :{
            if(w.Opportunity__c != null) 


      // INSERT/UPDATE Trigger

    if(Trigger.isDelete || Trigger.isUpdate){
        for(Work_Order__c w : trigger.old){
            if(w.Opportunity__c != null){
 //              if(!oppIDs.contains(w.Opportunity__c)) 
            //           w = [select ID, Opportunity__c from Work_Order__c where Opportunity__c in :oppIds];
    }  // DELETE/UPDATE Trigger

    if(oppIDs .size() > 0) {

        Map<ID, Opportunity> opps = new Map<ID, Opportunity>([Select id, Estimated_Cost__c from Opportunity Where Id in :oppIds]);  
        Opportunity d = null;

        for (AggregateResult dr : [SELECT Opportunity__c, SUM(Cost_WO__c) SubPO FROM Work_Order__c WHERE Opportunity__c in :oppIds GROUP BY Opportunity__c]) {
            String dID = (String)dr.get('Opportunity__c');
            //get record or create one
            if(opps.get(dID) == null)
            d = new Opportunity (ID=dID,
                Estimated_Cost__c = 0);
                d = opps.get(dID);
            // update the summary total fields on the opportunity
            Decimal po = (Decimal)dr.Get('SubPO');
            d.Estimated_Cost__c = po;
            opps.put(dID, d);
        //commit the changes to Salesforce
        update opps.values();





Those validation rules will fire when you do the update, and if the rules are new, or have been modified since the data was inserted, the opportunities may no longer pass the validation test. Here is a blog post that may give you some ideas on how to program around this issue. Basically you need to create a custom field to control your validation rules on the opportunity.