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
KyoKyo 

Test Class not work

it's not work 2 line 

hilight a line not work in the Trigger

 

setAccountNumber.add(myNewCon .Account_Number__c) ;

myNewCon.AccountID = Acc.id;

 

@isTest
private class TestUpdateAccountNameAndNumberinContact{
     
     static testMethod void myUnitPopContact() {
      Account Acc = new Account(Name = 'test',AccountNumber = '0000001');
         insert Acc;
      Account Acc2 = new Account(Name = 'test2',AccountNumber = '0000001');
         insert Acc2;
      
      Test.StartTest();   
      Contact Con = new Contact();
      ApexPages.currentPage().getParameters().put('id',con.id);
      Con.LastName = 'test2';
      Con.AccountID = Acc.id;
      Con.Account_Number__c = Acc.AccountNumber;
      insert con;
      try{
         update con;
         }catch(DMLException de){}
         
      Contact Dup = new Contact();
      Dup.LastName = 'test';
      Dup.AccountID = Acc2.id;
      Dup.Account_Number__c = Con.Account_Number__c ;
      insert Dup;
      try{
         update Dup;
         }catch(DMLException de){}                    
         Test.StopTest();        
     }
}

 

 

trigger UpdateAccountNameAndNumberinContact on Contact (before Update){
//List<Account> Acc = new List<Account>();
Set<String> setAccountNumber = new Set<String>();
Set<String> setAccount = new Set<String>();

    for(Contact  Con : Trigger.new){
        Map<Id,Contact> newConMap = Trigger.newMap;
        Map<Id,Contact> oldConMap = Trigger.oldMap;

        for(Id ConId:newConMap.keySet()){
        Contact myNewCon = newConMap.get(ConId);
        Contact myOldCon = oldConMap.get(ConId);
        
          if(myNewCon.AccountID == myOldCon.AccountID &&  myNewCon.Account_Number__c <> myOldCon.Account_Number__c && myNewCon.Account_Number__c != null){ 
                setAccountNumber.add(myNewCon .Account_Number__c) ;
            }
             else if(myNewCon.AccountID <> myOldCon.AccountID &&  myNewCon.Account_Number__c == myOldCon.Account_Number__c && myNewCon.AccountID != null){
                setAccount.add(myNewCon .AccountID ) ;
            }
         }
    }  
    Account Acc = new Account();
    
    List<Account> lstAcc = [Select id,AccountNumber from Account Where AccountNumber in :  setAccountNumber or id in : setAccount];
    if(lstAcc.size()>0) {
        Acc = lstAcc[0];
    }
    
    for(Contact Con : Trigger.new){
        Map<Id,Contact> newConMap = Trigger.newMap;
        Map<Id,Contact> oldConMap = Trigger.oldMap;

        for(Id ConId:newConMap.keySet()){
        Contact myNewCon = newConMap.get(ConId);
        Contact myOldCon = oldConMap.get(ConId);
        
                if(myNewCon.AccountID == myOldCon.AccountID &&  myNewCon.Account_Number__c <> myOldCon.Account_Number__c && myNewCon.Account_Number__c != null){                 
                    myNewCon.AccountID = Acc.id;
                }
                else if(myNewCon.AccountID <> myOldCon.AccountID &&  myNewCon.Account_Number__c == myOldCon.Account_Number__c && myNewCon.AccountID != null){
                    myNewCon.Account_Number__c  = Acc.AccountNumber;
                }
                /*else if(myNewCon.AccountID <> myOldCon.AccountID &&  myNewCon.Account_Number__c <> myOldCon.Account_Number__c && myNewCon.AccountID != null && myNewCon.Account_Number__c != null){
                    myNewCon.AccountID.AddError('Mismatch Account Name and Account Number');
                    myNewCon.Account_Number__c.AddError('Mismatch Account Name and Account Number');
                }*/
               
                
}
}
}

 

Thank you so much

CLKCLK

first line errror may be about, you are storing number/integer value in set of string.

Shashikant SharmaShashikant Sharma

Try this I have added a method in this, May I know why you have used try catchblick  in test method, I have not used it as you are not throwing any exception in trigger , if you are facing any issue in test method let me know.

 

@isTest
private class TestUpdateAccountNameAndNumberinContact{
     
     static testMethod void myUnitPopContact() {
      Account Acc = new Account(Name = 'test',AccountNumber = '0000001');
         insert Acc;
      Account Acc2 = new Account(Name = 'test2',AccountNumber = '0000001');
         insert Acc2;
      
      Test.StartTest();   
      Contact Con = new Contact();
      ApexPages.currentPage().getParameters().put('id',con.id);
      Con.LastName = 'test2';
      Con.AccountID = Acc.id;
      Con.Account_Number__c = Acc.AccountNumber;
      insert con;
      try{
         update con;
         }catch(DMLException de){}
         
      Contact Dup = new Contact();
      Dup.LastName = 'test';
      Dup.AccountID = Acc2.id;
      Dup.Account_Number__c = Con.Account_Number__c ;
      insert Dup;
      try{
         update Dup;
         }catch(DMLException de){}                    
         Test.StopTest();        
     }


   static testMethod void myUnitPopContact() {
      Account Acc = new Account(Name = 'test',AccountNumber = '0000001');
         insert Acc;
      
      Contact Con = new Contact();
      Con.LastName = 'test2';
      Con.AccountID = Acc.id;
      Con.Account_Number__c = Acc.AccountNumber;
      insert con;
      Test.StartTest();   
     
      
         Con.Account_Number__c  =  '0000002';
         update con;
      
         Test.StopTest();        
     }
}

 

maiyakumaiyaku

thank you Shashikant Sharma it's work !

Shashikant SharmaShashikant Sharma

Your welcome mate.

 

I would request you to mark this accepted solution so that others can also benifitted from it.