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
sdfasdsdfasd 

How to write the Test Class for update trigger

1. i created the Trigger. Trigger use is once insert the records in Broker object automatically convert the records in contact object. once update the Broker object then Contact also updated.once delete the Broker object Contact also deleted.Trigger working Properly. but i need the Test Class for this Trigger. Test class code coverage is 13% how can i reach 75%. please help me .................

 

Trigger:

=====

 if(Trigger.isInsert){
        List<Contact> con = new List<Contact>();
        List<Broker__c> bro = new List<Broker__c>();
        List<Broker__c> UBList = new List<Broker__c>();
        List<ID> BIDs = new List<ID> {};
        for(Broker__c br:Trigger.New){
            bro.add(br);
            BIDs.add(br.id);
        }
      
        for(Broker__c b: bro) {
            Contact c = new Contact();
            c.LastName = b.Name;
            c.Title=b.Title__c;
            c.Contact_type__c=b.Broker_Type__c;
            c.Appointed_w_SeeChange__c=b.Appointed_with_SeeChange__c;
            c.RecordTypeId='012Z00000004JZS';
            c.Broker__c = b.Id;
           
            con.add(c);
        }
        insert con;
        
       Map<ID,Broker__c> UB = new Map<ID,Broker__c> {};
         for(Broker__c bc: [select id, contact_id__c, Agency__r.OwnerId from Broker__c where Id in : BIDs]) {
                UB.put(bc.Id,bc);
        }
        
        for(Contact c: con) {
            if(UB.containsKey(c.Broker__c)) {
                Broker__c UBCon = UB.get(c.Broker__c);
                UBCon.Contact_ID__c = c.Id;
                UBCon.Owner__c = UBCon.Agency__r.OwnerId;
                UBList.add(UBCon);
            }
        }
        update UBList;

    }    
    //Update Contact        
    if(Trigger.isUpdate){
        Map<ID,Broker__c> UB1 = new Map<ID,Broker__c> {};
        List<Contact> UpConList = new List<Contact> {};
        Set<ID> UBIDs = new Set<ID> {};
        for(Broker__c b: Trigger.new) {
            UB1.put(b.id,b);            
            UBIDs.add(b.id);
        }
        
        System.debug('Broker Map:'+UB1);
        for(Contact c: [select id, broker__c from contact where broker__c in :UBIDs]){
            Broker__c b = new Broker__c();
            if(UB1.containsKey(c.broker__c)) {
                b = UB1.get(c.broker__c);
                System.debug('Map get:'+b);
                c.LastName = b.Name;
                c.Title=b.Title__c;
                c.Contact_type__c=b.Broker_Type__c;
                c.Appointed_w_SeeChange__c=b.Appointed_with_SeeChange__c;
                c.Appointed_since__c=b.Appointed_Since__c;
                c.RecordTypeId='012Z00000004JZS';
                c.Broker__c = b.Id;
                UpConList.add(c);
            }
        }
        System.debug('UpConList:'+UpConList);
        update UpConList;
     }
     
    //Delete Contact
    if(Trigger.isDelete){
        List<ID> DBList = new List<ID>();
        List<Contact> DelConList = new List<Contact>();
        for(Broker__c b:Trigger.old) {
            DBList.add(b.id);
            System.debug('b value:'+b+'b.id:'+b.id);
        }  
        
        System.debug('DBList:'+DBList);
        DelConList = [select id from contact where Broker__c in :DBList];
        if(DelConList.size() >0) {
           delete DelConList;
        }
    }
 

i write the test class for Trigger. but showing this error Exception:System.ListException: Duplicate id in list:

 

how can solve this problem........

 

Best Answer chosen by Admin (Salesforce Developers) 
sdfasdsdfasd

Thanks for your help Navatar. Still i am get 75% using this code plesase . now i got 13% code coverage.pleas help me how can i got 75% of code coverage

All Answers

Navatar_DbSupNavatar_DbSup

Hi,
Try below code as reference of your test method:
@isTest
private class testTriggerinsert_Contact_Activity1
{
public static testMethod void unitTestinsert_Contact_Activity()
{
Broker__c b=new Broker__c(name='test');
insert b;
update b;
delete b;
}
}

sdfasdsdfasd

Thanks for your help Navatar. Still i am get 75% using this code plesase . now i got 13% code coverage.pleas help me how can i got 75% of code coverage

This was selected as the best answer
sdfasdsdfasd

Thanks for your help Navatar. Still i am not get 75% using this code  . now i got 13% code coverage.pleas help me how can i got 75% of code coverage

sdfasdsdfasd

Thanks Navatar for your help . Now TestClass working fine.Thanks...........................

Navatar_DbSupNavatar_DbSup

Hi,

      Thats good.Please mark it as solved.