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
ForceLoverForceLover 

How can we make a trigger free from validation rule

I have a validation rule on lead object which is for our users to set them as owner for the lead.i written a trigger on task object to update the lead fields with task fields here i'm getting an error

 

activitycall2:execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 00QE000000ExKxvMAF; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION.

 

how can i make my trigger free form this validation rule through Apex.

Prafull G.Prafull G.

Unfortunately, there is no straight way to avoid/bypass validation rules using Apex. However there are few workarounds. One of them is explained in this post

Raj.ax1558Raj.ax1558

Hello,

 

Salesforce not provides any functionlity to avoid validation at the time of trigger execution except Lead conversion trigger. 

 

You have require Inactive OR deactivate the validation, and write the manul validation( Validation logic)  in your the trigger.

 

Click on KUDOS button if the post helps you!!!

If you find this as solution  please marked as solution for others users help in a same query.

 

Thank  You, 

Raj Jha

ForceLoverForceLover

Hi Raj,crmtech21

 

Actually this is my situation i have a validation rule on lead object for my users to take ownership, if user role is 'salesrep'.I'm writing a trigger on task object of lead to update lead fields i'm facing a tricky situation, in my sandbox i'm not getting any errors as i mention.I login as one of my user of salesrep role i'm able to create the task on a lead which is not owned by him(owned by queue), while updating leads i'm setting present login user as lead owner will it bypass the validation rule? But in production i'm getting the error ...!

 

Here is my trigger code...please go thriugh this and tell me the exact behaviour

 

trigger activitycall2 on task(after insert, after update) {
    Set<id> leadIds = new set<id>();    // Lead container
    task[] tasks = new task[0];         // matched tasks
    id queueid,Id1;
    UserRole[] roles;
    Lead a;
    
    // are there any leads?
    for(task record:trigger.new) {
        if( record.CallDurationInSeconds > 0 || record.whoid != null && record.whoid.getsobjecttype() == lead.sobjecttype ) {
            leadids.add(record.whoid);
            System.debug('size is'+leadIds.size());
            tasks.add(record);
        }
    }
    
    // if no leads, stop here
    if(leadids.isempty()) {
        return;
    }
    
   else {
        // Gettting user Role
        roles = [select id,name from userrole where id = :userinfo.getuserroleid()];

        
        // does the queue exist, and what is the id?
        
        for(group record:[select id from group where type = 'queue' and developername = 'Mailed_Data']) {
            queueid = record.id;
        }
        
        List<Lead> ownerids=[select id ,ownerid from lead where ownerid = :queueid and id in :leadids];
        if(ownerids.size()>0){
         
         a = ownerids.get(0);
         Id1=a.OwnerId;  
         System.debug(Id1);     
        } 
        
        
             
        // find any leads owned by the queue
        leadids.retainall((new Map<id,lead>([select id from lead where ownerid = :queueid and id in :leadids])).keyset());
                
    }
    map<id,lead> leads = new map<id,lead>();
    for(task record:tasks) {
    
        if(Id1==queueid){        
        leads.put(record.whoid,new lead(id=record.whoid,last_call_result__c=record.calldisposition, notes__c=record.Note_Summary__c, ownerid=userinfo.getuserid()));
        }
        else{
        leads.put(record.whoid,new lead(id=record.whoid,last_call_result__c=record.calldisposition, notes__c=record.Note_Summary__c));
        } 
    }
    
    update leads.values();
}

 Reply me ASAP...

ForceLoverForceLover
Hi ,
Thanks for replying.As per Bob's post he has validation and trigger on same object here my situation is some what different hope you understand i have validation on lead object trigger on task object please guide me with the changes ..i really need this very urgent...