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
OnurKOnurK 

SObject row does not allow errors

Can you helpm for adding the error message here? I am getting SObject row does not allow errors message

 

trigger beforeCase on Case (before insert, before update) {/*

   // List <Case> cs = Trigger.new;
   // 	TaxNumberDuplicate.checkDuplicates (cs);
   	
   		
        
         Set<String> taxNumber =new Set<String>();
         for (Case c : trigger.new) { 
         		
				taxNumber.add(c.Tax_Number__c);
			}
		
			List<Case> duplicatecaseList = new List<Case>();
			
			for(Case c : [SELECT id, Tax_Number__c, permit_duplicate__c FROM Case WHERE Tax_Number__c IN: TaxNumber])	
			
			duplicatecaseList.add(c);
			
            for(Case cs : duplicatecaseList){
            
            if(trigger.isInsert){
            
            
            		if(duplicatecaseList.size()> 0 && 
               		cs.permit_duplicate__c == FALSE && 
               		cs.Tax_Number__c != Null){
            
            		cs.Tax_Number__c.addError('ERROR!!!!!');
            	}
            }
            
            else if(trigger.isUpdate){
      
               
                if(duplicatecaseList.size()> 0 && 
                   cs.permit_duplicate__c == FALSE && 
                   cs.Tax_Number__c != Null &&
                   System.trigger.oldMap.get(cs.id).Tax_Number__c != System.trigger.newMap.get(cs.id).Tax_Number__c ){
            
                	cs.Tax_Number__c.addError('ERROR!!!!!');
            
               		 }
            }
        }    */
}

 

Best Answer chosen by Admin (Salesforce Developers) 
shruthishruthi

Hi OnurK

 

You are trying to add the Error on the record in duplicate list, which will not be possible. In your code duplicateList contains the list of existing records which have the similar tax number. Try the code below. I have optimized the code by including the permit_duplicate__c check in the query. And the Tax_Number__c null check before having it included in the list "taxNumber".

 

trigger beforeCase on Case (before insert, before update) { 
         Set<String> taxNumber =new Set<String>();
         for (Case c : trigger.new) {                 
         if(c.Tax_Number__c != null)      
taxNumber.add(c.Tax_Number__c);
        }       
        List<Case> duplicatecaseList = new List<Case>();           
        for(Case c : [SELECT id, Tax_Number__c, permit_duplicate__c FROM Case WHERE Tax_Number__c IN: TaxNumber AND permit_duplicate__c = FALSE])   
            duplicatecaseList.add(c);
        for(Case cs:trigger.new) {
            if(duplicatecaseList.size()>0) {
                if(trigger.isInsert) {
                    cs.Tax_Number__c.addError('You have an Error with this field');
                }
                else {
                    if(trigger.newMap.get(cs.Id).Tax_Number__c ! = trigger.oldMap.get(cs.Id).Tax_Number__c) {
                        cs.Tax_Number__c.addError('You have an Error with this field');
                    }
                }
               
            }
        }           
}

Hope this helps! :)

All Answers

kiranmutturukiranmutturu

y cant you try like this in all the places

 

cs.Tax_Number__c.addError('ERROR!!!!!');  replace this as trigger.new.tax_number__c.adderror('error'); and try

OnurKOnurK

Hi,

 

First of all thank you for replying so quickly. After I replaced the code, I received another error

 

"Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Case>"

 

Thanks

shruthishruthi

Hi OnurK

 

You are trying to add the Error on the record in duplicate list, which will not be possible. In your code duplicateList contains the list of existing records which have the similar tax number. Try the code below. I have optimized the code by including the permit_duplicate__c check in the query. And the Tax_Number__c null check before having it included in the list "taxNumber".

 

trigger beforeCase on Case (before insert, before update) { 
         Set<String> taxNumber =new Set<String>();
         for (Case c : trigger.new) {                 
         if(c.Tax_Number__c != null)      
taxNumber.add(c.Tax_Number__c);
        }       
        List<Case> duplicatecaseList = new List<Case>();           
        for(Case c : [SELECT id, Tax_Number__c, permit_duplicate__c FROM Case WHERE Tax_Number__c IN: TaxNumber AND permit_duplicate__c = FALSE])   
            duplicatecaseList.add(c);
        for(Case cs:trigger.new) {
            if(duplicatecaseList.size()>0) {
                if(trigger.isInsert) {
                    cs.Tax_Number__c.addError('You have an Error with this field');
                }
                else {
                    if(trigger.newMap.get(cs.Id).Tax_Number__c ! = trigger.oldMap.get(cs.Id).Tax_Number__c) {
                        cs.Tax_Number__c.addError('You have an Error with this field');
                    }
                }
               
            }
        }           
}

Hope this helps! :)

This was selected as the best answer
OnurKOnurK

Thanks shruthi,

 

It was great help.