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
Livia PimentelLivia Pimentel 

Apex: FIELD_CUSTOM_VALIDATION_EXCEPTION

Hello, I am trying to implement some triggers in my org, but I am having some issues related to the error message shown in this post's title.

The complete error message is the following:

"System.DmlException: Update failed. First exception on row 0 with id a05HZ0000001vCLYAY; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, List has no rows for assignment to SObject: []"

It happens on line 43 of the following test class:
 
@isTest
public with sharing class testclass_closedRfqOpp {
    public testclass_closedRfqOpp(){
        
        
    }
    // test method
    @isTest
    public static void method_closedRfq(){
        
       // initialization
       // 
       list<user> u =[select id from user where alias='lpime'];
        
       Test.startTest(); 
       Account acc = new Account();
       acc.name = 'Conta TesteAAAA 10/03';
       acc.OwnerId = u[0].id;
        
       insert acc;
           
       Opportunity opp = new Opportunity();
       opp.name = 'OportunidadeAAAA Teste 10/03';    
       opp.Tipo_de_Opera_o__c = 'Venda';
       opp.AccountId = acc.Id;
        
       opp.StageName = 'Qualificação';
       
       opp.CloseDate = date.today();
        
       insert opp; 
        
       RFQ__c rfq = new RFQ__c();
       rfq.Oportunidade__c = opp.Id;
       rfq.In_cio_Suprimento__c = date.today();
       rfq.Validade_Suprimento__c  = date.today();
  		
       insert rfq;
        
       rfq.Status_da_RFQ__c = 'Perdida';
           
         
       update rfq; // ERROR
     
        
       Opportunity opp_a = [SELECT Id, StageName FROM Opportunity WHERE Id=:rfq.Oportunidade__c];
       
       
       system.assertEquals('Closed Lost', opp_a.StageName);
        
        
        Test.stopTest();
                
        
    }

}

I have the following "after update" triggers on the RFQ object:

Trigger 1:
 
trigger total_rfq_proposta on RFQ__c (after update) {
  
        if (Trigger.isAfter) {
            
            
        if (Trigger.isUpdate) {
            
       
           RFQHandler.updateRFQ(Trigger.new);
           
            
        }
            
}
        
       
}

Trigger 2:
 
trigger closedRfqOpp on RFQ__c (after update) {
    
        
        for(RFQ__c rfq:trigger.new){
            
            Opportunity opp = [select id, StageName from Opportunity where id=:rfq.Oportunidade__c AND id!= null];
            
            if(opp != null){
                
                if(rfq.Status_da_RFQ__c == 'Perdida'){
              	                
                 opp.StageName = 'Closed Lost';
            	
                 update opp;
                 
            }
                
                
                
            }
            
            
        }
        
  
}

Trigger 3:
 
trigger rfqsCanceledNegociationLostOpp on RFQ__c (after update) {
    
    
     for(RFQ__c rfq:trigger.new){
            
         Opportunity opp = [select id, StageName from Opportunity where id=:rfq.Oportunidade__c AND id!= null];
         
         
         if (opp != null){
             
             if(rfq.Status_da_RFQ__c == 'Fechada'){              
                opp.StageName = 'Closed Won';
                update opp;           
       		  }
         
           if(rfq.Status_da_RFQ__c == 'Em Jogo'){              
                opp.StageName = 'Negociação';
                update opp;           
       		  }
           
            
            
            if(rfq.Status_da_RFQ__c == 'Perdida' | rfq.Status_da_RFQ__c == 'Fechada' ){
                
                 List <RFQ__c> rfqs_opp = new List<RFQ__c>();
              	
                rfqs_opp = [select id, Status_da_RFQ__c from RFQ__c where Oportunidade__c=:rfq.Oportunidade__c AND id!=null];
                 
                for(RFQ__c el_rfq:rfqs_opp){
                    
                    if(el_rfq.id != rfq.id & el_rfq.id!= null){
                        
                        el_rfq.Status_da_RFQ__c = 'Cancelada';
                        update el_rfq;
                        
                    }
                }
                 
            }
             
             
             
         }
         
         	
        }
    

}

I appreciate any help you can provide.

Thank you,

Lívia.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Livia,

I hope the issuw might be in rfqsCanceledNegociationLostOpp trigger . If the query does not return any records and You are using that variable causes the issue.
I see the issue may be in the below line.
 
rfqs_opp = [select id, Status_da_RFQ__c from RFQ__c where Oportunidade__c=:rfq.Oportunidade__c AND id!=null];

After the above line there is no null check . To confirm the same you can deactive both the other triggers and run the test class and see the result. 

Or I hope you may get the exact error in which line line also in debug logs I guess.

if this solution helps, Please mark it as best answer.

https://help.salesforce.com/s/articleView?id=000328824&type=1 (https://help.salesforce.com/s/articleView?id=000328824&type=1)

Thanks,