Wednesday 15 June 2016

System.FinalException: Sobject rows does not allow errors:

Sometimes we need to show error message from trigger using add error method. This post is all about what are the challenge i faced to display error message.
My requirement is showing error message if an opportunity which is not linked to contact are updated.As we know opportunitycontactrole is junction object which will relate both opportunity and contact. 
So It is so simple, I went for Opportunity beofore update.
trigger OpportunityBeforeUpdate on Opportunity (before update) {       
       OpportynitySequesncecontroller.beforeUpdate();
}
public class OpportynitySequesncecontroller {
      public void static beofeUpdate(){     
               // others logic are there cos many operation might be there on before update
              OpportynityManagement .validateOppty(trigger.newmap)
      }
}
public class OpportynityManagement {
      public void static validateOppty(Map<Id,Opportunity> newOpptyMap){     
               // logic to show error 
      }
}
OpportynityManagement .validateOppty(trigger.newmap) wiil throw error like below 
Error Error: Compile Error: Method does not exist or incorrect signature: OpportunityManagement.validateOppty(Map<Id,SObject>) 



To resolve this error we need to change way of invocation 
public class OpportynitySequesncecontroller {
      public void static beofeUpdate(){     
               // others logic are there cos many operation might be there on before update
              map<Id,Opportunity> newOpptyMap = (map<Id,Opportunity>)trigger.newMap;
              OpportynityManagement .validateOppty(newOpptyMap )
      }
}
Now this the method where adderror method is used.
public static void validateOppty(map<Id,Opportunity> newOpptyMap){
Id SalesOpsRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Test Sales Opportunity').getRecordTypeId();
list<Opportunity> listOfOpportunity;
if(!newOpptyMap.keyset().isEmpty()){
listOfOpportunity = [SELECT id,(SELECT id FROM opportunitycontactroles) FROM opportunity WHERE recordtypeid =:SalesOpsRecordTypeId AND Id IN :newOpptyMap.keyset()];
for(Opportunity opp:listOfOpportunity){
if(opp.opportunitycontactroles.size() == 0 ){
opp.get(opp.id).addError('Oppotunity can not be modified since it is not having any contacts');
}
}
}
 
}

This code will be complied successfully, but when we update an oppty we can get this exception 
To resolve the above issue method needs to be modified.
public static void validateOppty(map<Id,Opportunity> newOpptyMap){
Id SalesOpsRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Test Sales Opportunity').getRecordTypeId();
list<Opportunity> listOfOpportunity;
if(!newOpptyMap.keyset().isEmpty()){
listOfOpportunity = [SELECT id,(SELECT id FROM opportunitycontactroles) FROM opportunity WHERE recordtypeid =:SalesOpsRecordTypeId AND Id IN :newOpptyMap.keyset()];
for(Opportunity opp:listOfOpportunity){
if(opp.opportunitycontactroles.size() == 0 && newOpptyMap.containskey(opp.id)){
newOpptyMap.get(opp.id).addError('Oppotunity can not be modified since it is not having any contacts');
}
}
}
 
}