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

AddError in Test Class not work!

I'm not write test class is full loop. Because ploblem adderror method.


trigger RelationDuplicationA2A on Relationship__c(before insert) {
set<ID> setAccountID = new set<ID>();
set<ID> setRevAccountID = new set<ID>();
integer setRecordType;

for(Relationship__c ra:{
   if(ra.Account_ID__r.Mark_for_Delete__c != true || ra.Rev_Account_ID__r.Mark_for_Delete__c  != true && (ra.RecordTypeID == '01290000000Ym1W')){
       if ([0].Account_ID__c != NULL &&[0].Rev_Account_ID__c != NULL)  {
           setAccountID.Add(ra.Account_ID__c);  //Set Value Relation Account to Account
           setRecordType = 1;  //set value criteria
//------------------------------------------- End Add Data --------------------------------------------------------------------------------------------------

// SOQL for Duplicate Relationship without Loop
    if(setRecordType == 1){
           List<Relationship__c> rs = [ select id,Account_ID__c,Rev_Account_ID__c,Mark_for_Delete__c from Relationship__c  WHERE Account_ID__c =[0].Account_ID__c And Rev_Account_ID__c =:[0].Rev_Account_ID__c And Mark_for_Delete__c =: false];
               if (rs.size() > 0 ) {
         [0].Account_ID__c.addError('Duplicate Relationship');
         [0].Rev_Account_ID__c.addError('Duplicate Relationship');

 Ploblem Trigger Line :[0].Account_ID__c.addError('Duplicate Relationship');       [0].Rev_Account_ID__c.addError('Duplicate Relationship');


private class TestRelationDuplicationA2A  {
   static testMethod void myUnitTest() {
      Account Acc = new Account(Name = 'test',AccountNumber = '00000001');
         insert Acc;
      Contact Con = new Contact(LastName = 'test',AccountID =;
         insert con;
      Relationship__c ra = new Relationship__c(Account_ID__c =,Rev_Account_ID__c =,Mark_for_Delete__c = False);
            insert ra;
            }catch(DMLException de){

      Relationship__c Dup = new Relationship__c(Account_ID__c =,Rev_Account_ID__c =,Mark_for_Delete__c = False);
             try {
             insert Dup;
             catch (Exception e) {
                   System.debug('Duplicate Relationship');                        

 Thank you so much.

Shashikant SharmaShashikant Sharma

There seems nothing wrong in your code , have you checked that you are reachibg to the code  addError, it might be a posibility that this condition if(setRecordType == 1){ is not satisfied in your test method as I don't see any record type set in test method. SO because of this your code never reached to adderror part in test context.

Try with setting record type for the records that you are inserting in test method.