+ Start a Discussion
TheLearnerTheLearner 

code coverage for the class

Hi Could anyone improve my test coverage please.

Class:

trigger S2S_Subcontractor_Client on SCC_Client__c (after insert, after update, before update, before insert) {
//CH01.End
    string userName = UserInfo.getName();
    String orgName =  UserInfo.getOrganizationName();
    
    if((Trigger.isDelete || Trigger.isUpdate || Trigger.isInsert) && Trigger.isBefore){
        if(userName !='Connection User'){
        //CH01.Start
            for (SCC_Client__c accreditation: Trigger.new) { 
                if(accreditation.Owning_Region__c != All_ConnectionHelpler.owningRegion && !All_ConnectionHelpler.justCreatedRecord) {
                    if(Trigger.isDelete)
                        accreditation.addError(All_ConnectionHelpler.deletePermissionError(accreditation.Owning_Region__c));
                    if(Trigger.isInsert)
                        accreditation.addError(All_ConnectionHelpler.createPermissionError(accreditation.Owning_Region__c));
                    if(Trigger.isUpdate){
                        if(accreditation.SCC__c != Trigger.oldmap.get(accreditation.id).SCC__c)
                            accreditation.SCC__c.addError('Not Editable');

                        else
                            accreditation.addError(All_ConnectionHelpler.updatePermissionError(accreditation.Owning_Region__c));
                    }
                }
            }
            //CH01.End
        }
    }
      
    if((trigger.isUpdate) && Trigger.isAfter){      
        // Define connection id 
        Id networkIdOne = All_ConnectionHelpler.getConnectionId(All_ConnectionHelpler.connectionOne);
        Id networkIdTwo = All_ConnectionHelpler.getConnectionId(All_ConnectionHelpler.connectionTwo);  
        
        List<PartnerNetworkRecordConnection> Connections =  new  List<PartnerNetworkRecordConnection>();
        //CH01.Start
        for (SCC_Client__c subcontractorClient: Trigger.new) { 
            if(subcontractorClient.ConnectionReceivedId != null)
                continue;           
            PartnerNetworkRecordConnection newConnection = 
              new PartnerNetworkRecordConnection( 
                  ConnectionId = networkIdOne, 
                  LocalRecordId = subcontractorClient.Id, 
                  SendClosedTasks = false, 
                  SendOpenTasks = false, 
                  SendEmails = false);
                              
            Connections.add(newConnection); 
        } 
        //CH01.End
        //CH01.start
        for (SCC_Client__c subcontractorClient: Trigger.new) { 
            if(subcontractorClient.ConnectionReceivedId != null)
                continue;           
            PartnerNetworkRecordConnection newConnection = 
              new PartnerNetworkRecordConnection( 
                  ConnectionId = networkIdTwo, 
                  LocalRecordId = subcontractorClient.Id, 
                  SendClosedTasks = false, 
                  SendOpenTasks = false, 
                  SendEmails = false); 
                  
            Connections.add(newConnection); 
        }
        //CH01.End
        if (Connections.size() > 0 ) { 
           database.insert(Connections); 
        }
      //////////////////////////////////  
    }  
        
    //CH01.Start    
    if(Trigger.isBefore && Trigger.isUpdate){
        for (SCC_Client__c subcontractorClient: Trigger.new) {
            //system.debug(subcontractorClient.CreatedById.name);
            //String createdBy = 
            if(!subcontractorClient.CreatedBy__c.startswith('partnernetwork@'))
                subcontractorClient.name = subcontractorClient.h_contract_id__c;
            else
                subcontractorClient.name = subcontractorClient.h_org_id__c;
        }
    }
    //CH01.End
    if((trigger.isUpdate || trigger.isInsert) && Trigger.isBefore){  
        Set<Id> receivedZTESTIds = new Set<Id>(); 
        //CH01.Start
        List<SCC_Client__c> accreditations = new List<SCC_Client__c>(); 
        //CH01.End
        
        // only share records created in this org, do not add ZTEST_Childs received from another org. 
        //CH01.Start
        for (SCC_Client__c subcontractorClient: TRIGGER.new) { 
            if ((subcontractorClient.ConnectionReceivedId != null || userName=='Connection User') ) {/*&& subcontractorClient.h_source_lookup_id__c!= Trigger.oldMap.get(subcontractorClient.id).h_source_lookup_id__c*/ 
                accreditations.add(subcontractorClient);
                receivedZTESTIds.add(subcontractorClient.h_source_lookup_id__c);
            }         
        }
        //CH01.End 
        
        PartnerNetworkRecordConnection[] sCCharingRecords = [SELECT Status, LocalRecordId, ConnectionId ,PartnerRecordId
                                       FROM PartnerNetworkRecordConnection               
                                       WHERE PartnerRecordId IN:receivedZTESTIds];
        //CH01.Start                                       
        for (SCC_Client__c subcontractorClient: accreditations) {
            
            // for each partner connection record for ZTEST_Child's ZTEST, check if it is active
            if(subcontractorClient.h_source_lookup_id__c!=null && subcontractorClient.h_source_lookup_id__c.startswith(SCC__c.sObjectType.getDescribe().getKeyPrefix()))
            {
                subcontractorClient.SCC__c =  subcontractorClient.h_source_lookup_id__c;
                continue;
            } 
            for (PartnerNetworkRecordConnection sCCSharingRecord:sCCharingRecords){
                system.debug('PartnerRecordId'+sCCSharingRecord.PartnerRecordId+'   ');
                system.debug('h_source_lookup_id__c'+subcontractorClient.h_source_lookup_id__c+'   ');
                if ((sCCSharingRecord.status.equalsignorecase('Sent') || sCCSharingRecord.status.equalsignorecase('Received')) 
                    && (/*sCCSharingRecord.ConnectionId == subcontractorClient.ConnectionReceivedId ||*/ userName=='Connection User') && sCCSharingRecord.PartnerRecordId == (Id)subcontractorClient.h_source_lookup_id__c)  
                {
                    //if(Trigger.isInsert)
                    if(sCCSharingRecord.LocalRecordId!=null){
                        subcontractorClient.SCC__c =  sCCSharingRecord.LocalRecordId;
                        break;

                    }
                } 
            }
        }
        //CH01.End
    }
    if((trigger.isUpdate || trigger.isInsert) && Trigger.isBefore){  
        Set<Id> receivedZTESTIds = new Set<Id>(); 
        //CH01.Start
        List<SCC_Client__c> accreditations = new List<SCC_Client__c>(); 
        //CH01.End
        
        // only share records created in this org, do not add ZTEST_Childs received from another org. 
        //CH01.Start
        for (SCC_Client__c accreditation: TRIGGER.new) { 
            if ((accreditation.ConnectionReceivedId != null || userName=='Connection User') ) {/*&& accreditation.h_source_lookup_id__c!= Trigger.oldMap.get(accreditation.id).h_source_lookup_id__c*/ 
                accreditations.add(accreditation);
                receivedZTESTIds.add(accreditation.h_source_lookup_id1__c);
            }         
        } 
        //CH01.End
        
        PartnerNetworkRecordConnection[] sCCharingRecords = [SELECT Status, LocalRecordId, ConnectionId ,PartnerRecordId
                                       FROM PartnerNetworkRecordConnection               
                                       WHERE PartnerRecordId IN:receivedZTESTIds];
             //CH01.Start                                  
        for (SCC_Client__c accreditation: accreditations) {
            system.debug('test'+accreditation.h_source_lookup_id1__c);
            system.debug('Test'+SCC_Clients__c.sObjectType.getDescribe());
            // for each partner connection record for ZTEST_Child's ZTEST, check if it is active
            if(accreditation.h_source_lookup_id1__c!=null && accreditation.h_source_lookup_id1__c.startswith(SCC_Clients__c.sObjectType.getDescribe().getKeyPrefix()))
            {
                accreditation.Client_Names__c =  accreditation.h_source_lookup_id1__c;
                continue;

            } 
            for (PartnerNetworkRecordConnection sCCSharingRecord:sCCharingRecords){
                system.debug('PartnerRecordId'+sCCSharingRecord.PartnerRecordId+'   ');
                system.debug('h_source_lookup_id1__c'+accreditation.h_source_lookup_id1__c+'   ');
                if ((sCCSharingRecord.status.equalsignorecase('Sent') || sCCSharingRecord.status.equalsignorecase('Received')) 
                    && (/*sCCSharingRecord.ConnectionId == accreditation.ConnectionReceivedId ||*/ userName=='Connection User') && sCCSharingRecord.PartnerRecordId == (Id)accreditation.h_source_lookup_id1__c)  
                {
                    //if(Trigger.isInsert)
                    if(sCCSharingRecord.LocalRecordId!=null){
                        accreditation.Client_Names__c =  sCCSharingRecord.LocalRecordId;
                        break;

                    }
                } 
            }
            //CH01.End
        }
    }
    if(Trigger.isAfter && Trigger.isInsert){
    //CH01.Start
        SCC_Client__c[] scc = new List<SCC_Client__c>();
        for (SCC_Client__c subcontractorClient: Trigger.new) {
            scc.add(new SCC_Client__c(id = subcontractorClient.id));
        }
        update scc;
        //CH01.End
    }
}

Test class: 

@isTest
public with sharing class Testclassforsubcontractor { 

    public static testMethod void test1(){
     
      Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        
        
        //User ux = [select id from User where  name = 'Connection User']; 
        User u = new User(Alias = 'pnet', Email='scctest@testorg.com',  LanguageLocaleKey='en_US', ProfileId = p.Id, firstName='Connection',LastName='User', username = 'connuser@test.com',EmailEncodingKey='UTF-8',TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US',CommunityNickname='ddff');
        
        System.runAs(u) {
            system.debug('****************************'+UserInfo.getName());
            //System.assertEquals(u.username, null);
            
            string owningRegion = 'South';
        
            SCC_Clients__c sccc = new SCC_Clients__c(owning_region__c = owningRegion);
            insert sccc;
        
        SCC_Accreditations__c sccAcc = new SCC_Accreditations__c(Name = 'ISO 14001',Owning_Region__c = owningRegion);
        insert sccAcc;
        
        
        SCC__c scc = new SCC__c(Subcontractor_Name__c = 'abcd', Location__c = 'lll', Street__c = 'street', Town__c='ttt', Contact_Name__c = 'name', Number_of_Employees__c = 'Less than 5 employees', owning_region__c = owningRegion);
        insert scc;
        
        Accreditation__c acc = new Accreditation__c(UKAS_Certified__c='Yes',SCC__c = scc.id, SCC_Accreditations__c = sccAcc.id, Accreditation_Date__c= System.toDay(), Accreditation_Expiry_Date__c = System.toDay());
        insert acc; 
        //CH02.Start
        SCC_Client__c sc = new SCC_Client__c(scc__c = scc.id, Client_Names__c = sccc.id);
        insert sc;
        //CH02.End
        
        //CH02.Start
        SCC_Audits__c sa = new SCC_Audits__c(scc__c = scc.id);
        insert sa;
        //CH02.End

        //CH02.Start
        HS_Evaluation__c sccC34 = new HS_Evaluation__c(  SCC__c = scc.id, Review_Requested_Date_del__c = System.toDay(), HS_Evaluation_Year__c ='Year 1', Expiry_Date__c = System.toDay());
        insert sccC34;
        //CH02.End
        
        SCC_Performance_Review__c pr = new SCC_Performance_Review__c(SCC_ID__c = scc.id, Review_Date__c = System.today());
        insert pr;
        
        SCC_Sent_Email_History__c seh = new SCC_Sent_Email_History__c(SCC__c = scc.id);
        insert seh;
            
            Blob picture = Blob.valueOf('');
            
            Attachment a = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            
            Note aa = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
            Note aa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
            Note aa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
            Note aa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
            Note aa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
            Note aa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
            Note aa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
            Note aa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
            
            Attachment[] lstAt = new List<Attachment>{a,a1,a2,a3,a4,a5,a6,a7};
            Note[] lstNote = new List<Note>{aa,aa1,aa2,aa3,aa4,aa5,aa6,aa7};
            
            insert lstAt;
            insert lstNote;
            test.stoptest();    
            
            Attachment aaa0 = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            
            Note aaaa0 = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
            Note aaaa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
            Note aaaa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
            
            Attachment[] lstAt1 = new List<Attachment>{aaa0,aaa1,aaa2,aaa3,aaa4,aaa5,aaa6,aaa7};
            Note[] lstNote1 = new List<Note>{aaaa0,aaaa1,aaaa2,aaaa3,aaaa4,aaaa5,aaaa6,aaaa7};
            
            
           try{
                insert lstAt1;
                insert lstNote1;
            }catch(Exception e){}
            
            
            
               
            All_ConnectionHelpler.owningRegion = 'North';
            All_ConnectionHelpler.getConnectionOwnerId(All_ConnectionHelpler.connectionOne);
            All_ConnectionHelpler.createPermissionError('North');
            All_ConnectionHelpler.updatePermissionError('North');
            All_ConnectionHelpler.deletePermissionError('North');
            
            
            
            All_ConnectionHelpler.createPersonel('a#@#a');
            All_ConnectionHelpler.createPersonel('a#@#a');
            
            try{
                update sccc;
            }catch(Exception e){}
            try{
                update sccAcc;
            }catch(Exception e){}
            try{
                update scc;
            }catch(Exception e){}
            try{
                update acc;
            }catch(Exception e){}
            try{
                update sc;
            }catch(Exception e){}
             try{
                 update sccC34;
            }catch(Exception e){}
            try{
                update pr;
            }catch(Exception e){}
            try{
                update seh;
            }catch(Exception e){}
           try{
                 update lstAt;
            }catch(Exception e){}
        }
    }// CH-01 START
    
   }
Best Answer chosen by TheLearner
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
 
@isTest
public class Testclassforsubcontractor 
{ 

    static testMethod void test1()
	{
     
      Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        
        
        //User ux = [select id from User where  name = 'Connection User']; 
        User u = new User(Alias = 'pnet', Email='scctest@testorg.com',  LanguageLocaleKey='en_US', ProfileId = p.Id, firstName='Connection',LastName='User', username = 'connuser@test.com',EmailEncodingKey='UTF-8',TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US',CommunityNickname='ddff');
        
        System.runAs(u) {
            system.debug('****************************'+UserInfo.getName());
            //System.assertEquals(u.username, null);
            
            string owningRegion = 'South';
        
            SCC_Clients__c sccc = new SCC_Clients__c(owning_region__c = owningRegion);
            insert sccc;
        
        SCC_Accreditations__c sccAcc = new SCC_Accreditations__c(Name = 'ISO 14001',Owning_Region__c = owningRegion);
        insert sccAcc;
        
        
        SCC__c scc = new SCC__c(Subcontractor_Name__c = 'abcd', Location__c = 'lll', Street__c = 'street', Town__c='ttt', Contact_Name__c = 'name', Number_of_Employees__c = 'Less than 5 employees', owning_region__c = owningRegion);
        insert scc;
        
        Accreditation__c acc = new Accreditation__c(UKAS_Certified__c='Yes',SCC__c = scc.id, SCC_Accreditations__c = sccAcc.id, Accreditation_Date__c= System.toDay(), Accreditation_Expiry_Date__c = System.toDay());
        insert acc; 
        //CH02.Start
        SCC_Client__c sc = new SCC_Client__c(scc__c = scc.id, Client_Names__c = sccc.id);
        insert sc;
        //CH02.End
        
        //CH02.Start
        SCC_Audits__c sa = new SCC_Audits__c(scc__c = scc.id);
        insert sa;
        //CH02.End

        //CH02.Start
        HS_Evaluation__c sccC34 = new HS_Evaluation__c(  SCC__c = scc.id, Review_Requested_Date_del__c = System.toDay(), HS_Evaluation_Year__c ='Year 1', Expiry_Date__c = System.toDay());
        insert sccC34;
        //CH02.End
        
        SCC_Performance_Review__c pr = new SCC_Performance_Review__c(SCC_ID__c = scc.id, Review_Date__c = System.today());
        insert pr;
        
        SCC_Sent_Email_History__c seh = new SCC_Sent_Email_History__c(SCC__c = scc.id);
        insert seh;
            
            Blob picture = Blob.valueOf('');
            
            Attachment a = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            
            Note aa = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
            Note aa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
            Note aa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
            Note aa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
            Note aa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
            Note aa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
            Note aa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
            Note aa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
            
            Attachment[] lstAt = new List<Attachment>{a,a1,a2,a3,a4,a5,a6,a7};
            Note[] lstNote = new List<Note>{aa,aa1,aa2,aa3,aa4,aa5,aa6,aa7};
            
            insert lstAt;
            insert lstNote;
            test.stoptest();    
            
            Attachment aaa0 = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            
            Note aaaa0 = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
            Note aaaa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
            Note aaaa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
            
            Attachment[] lstAt1 = new List<Attachment>{aaa0,aaa1,aaa2,aaa3,aaa4,aaa5,aaa6,aaa7};
            Note[] lstNote1 = new List<Note>{aaaa0,aaaa1,aaaa2,aaaa3,aaaa4,aaaa5,aaaa6,aaaa7};
            
            
           try{
                insert lstAt1;
                insert lstNote1;
            }catch(Exception e){}
            
            
            
               
            All_ConnectionHelpler.owningRegion = 'North';
            All_ConnectionHelpler.getConnectionOwnerId(All_ConnectionHelpler.connectionOne);
            All_ConnectionHelpler.createPermissionError('North');
            All_ConnectionHelpler.updatePermissionError('North');
            All_ConnectionHelpler.deletePermissionError('North');
            
            
            
            All_ConnectionHelpler.createPersonel('a#@#a');
            All_ConnectionHelpler.createPersonel('a#@#a');
            
            try{
                update sccc;
            }catch(Exception e){}
            try{
                update sccAcc;
            }catch(Exception e){}
            try{
                update scc;
            }catch(Exception e){}
            try{
                update acc;
            }catch(Exception e){}
            try{
                update sc;
            }catch(Exception e){}
             try{
                 update sccC34;
            }catch(Exception e){}
            try{
                update pr;
            }catch(Exception e){}
            try{
                update seh;
            }catch(Exception e){}
           try{
                 update lstAt;
            }catch(Exception e){}
        }
    }// CH-01 START

    static testMethod void testNonConnectionUser()
	{
     
      Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        
        User u = new User(Alias = 'pnet', Email='scctest@testorg.com',  LanguageLocaleKey='en_US', ProfileId = p.Id, firstName='Connection1',LastName='User1', username = 'connuser@test.com',EmailEncodingKey='UTF-8',TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US',CommunityNickname='ddff');
        
        System.runAs(u) 
		{
			try
			{	
					system.debug('****************************'+UserInfo.getName());
					//System.assertEquals(u.username, null);
					
					string owningRegion = 'South';
				
					SCC_Clients__c sccc = new SCC_Clients__c(owning_region__c = owningRegion);
					insert sccc;
				
				SCC_Accreditations__c sccAcc = new SCC_Accreditations__c(Name = 'ISO 14001',Owning_Region__c = owningRegion);
				insert sccAcc;
				
				
				SCC__c scc = new SCC__c(Subcontractor_Name__c = 'abcd', Location__c = 'lll', Street__c = 'street', Town__c='ttt', Contact_Name__c = 'name', Number_of_Employees__c = 'Less than 5 employees', owning_region__c = owningRegion);
				insert scc;
				
				Accreditation__c acc = new Accreditation__c(UKAS_Certified__c='Yes',SCC__c = scc.id, SCC_Accreditations__c = sccAcc.id, Accreditation_Date__c= System.toDay(), Accreditation_Expiry_Date__c = System.toDay());
				insert acc; 
				//CH02.Start
				SCC_Client__c sc = new SCC_Client__c(scc__c = scc.id, Client_Names__c = sccc.id);
				insert sc;
				//CH02.End
				
				//CH02.Start
				SCC_Audits__c sa = new SCC_Audits__c(scc__c = scc.id);
				insert sa;
				//CH02.End

				//CH02.Start
				HS_Evaluation__c sccC34 = new HS_Evaluation__c(  SCC__c = scc.id, Review_Requested_Date_del__c = System.toDay(), HS_Evaluation_Year__c ='Year 1', Expiry_Date__c = System.toDay());
				insert sccC34;
				//CH02.End
				
				SCC_Performance_Review__c pr = new SCC_Performance_Review__c(SCC_ID__c = scc.id, Review_Date__c = System.today());
				insert pr;
				
				SCC_Sent_Email_History__c seh = new SCC_Sent_Email_History__c(SCC__c = scc.id);
				insert seh;
					
					Blob picture = Blob.valueOf('');
					
					Attachment a = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					
					Note aa = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
					Note aa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
					Note aa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
					Note aa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
					Note aa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
					Note aa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
					Note aa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
					Note aa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
					
					Attachment[] lstAt = new List<Attachment>{a,a1,a2,a3,a4,a5,a6,a7};
					Note[] lstNote = new List<Note>{aa,aa1,aa2,aa3,aa4,aa5,aa6,aa7};
					
					insert lstAt;
					insert lstNote;
					test.stoptest();    
					
					Attachment aaa0 = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					
					Note aaaa0 = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
					Note aaaa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
					Note aaaa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
					Note aaaa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
					Note aaaa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
					Note aaaa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
					Note aaaa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
					Note aaaa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
					
					Attachment[] lstAt1 = new List<Attachment>{aaa0,aaa1,aaa2,aaa3,aaa4,aaa5,aaa6,aaa7};
					Note[] lstNote1 = new List<Note>{aaaa0,aaaa1,aaaa2,aaaa3,aaaa4,aaaa5,aaaa6,aaaa7};
					
					
				   try{
						insert lstAt1;
						insert lstNote1;
					}catch(Exception e){}
					
					
					
					   
					All_ConnectionHelpler.owningRegion = 'North';
					All_ConnectionHelpler.getConnectionOwnerId(All_ConnectionHelpler.connectionOne);
					All_ConnectionHelpler.createPermissionError('North');
					All_ConnectionHelpler.updatePermissionError('North');
					All_ConnectionHelpler.deletePermissionError('North');
					
					
					
					All_ConnectionHelpler.createPersonel('a#@#a');
					All_ConnectionHelpler.createPersonel('a#@#a');
					
					try{
						update sccc;
					}catch(Exception e){}
					try{
						update sccAcc;
					}catch(Exception e){}
					try{
						update scc;
					}catch(Exception e){}
					try{
						update acc;
					}catch(Exception e){}
					try{
						update sc;
					}catch(Exception e){}
					 try{
						 update sccC34;
					}catch(Exception e){}
					try{
						update pr;
					}catch(Exception e){}
					try{
						update seh;
					}catch(Exception e){}
				   try{
						 update lstAt;
					}catch(Exception e){}
			}
			 catch(Exception ee)
			 {
			 }
		 
		}
    }// CH-01 START
   
}


Please let us know above solution helps you. If that will help you then please select the above solution as best solution so that if some one have same problem that post can help other.

Thanks
Amit Chaudhary

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
 
@isTest
public with sharing class Testclassforsubcontractor 
{ 

    public static testMethod void test1(){
     
      Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        
        
        //User ux = [select id from User where  name = 'Connection User']; 
        User u = new User(Alias = 'pnet', Email='scctest@testorg.com',  LanguageLocaleKey='en_US', ProfileId = p.Id, firstName='Connection',LastName='User', username = 'connuser@test.com',EmailEncodingKey='UTF-8',TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US',CommunityNickname='ddff');
        
        System.runAs(u) {
            system.debug('****************************'+UserInfo.getName());
            //System.assertEquals(u.username, null);
            
            string owningRegion = 'South';
        
            SCC_Clients__c sccc = new SCC_Clients__c(owning_region__c = owningRegion);
            insert sccc;
        
        SCC_Accreditations__c sccAcc = new SCC_Accreditations__c(Name = 'ISO 14001',Owning_Region__c = owningRegion);
        insert sccAcc;
        
        
        SCC__c scc = new SCC__c(Subcontractor_Name__c = 'abcd', Location__c = 'lll', Street__c = 'street', Town__c='ttt', Contact_Name__c = 'name', Number_of_Employees__c = 'Less than 5 employees', owning_region__c = owningRegion);
        insert scc;
        
        Accreditation__c acc = new Accreditation__c(UKAS_Certified__c='Yes',SCC__c = scc.id, SCC_Accreditations__c = sccAcc.id, Accreditation_Date__c= System.toDay(), Accreditation_Expiry_Date__c = System.toDay());
        insert acc; 
        //CH02.Start
        SCC_Client__c sc = new SCC_Client__c(scc__c = scc.id, Client_Names__c = sccc.id);
        insert sc;
        //CH02.End
        
        //CH02.Start
        SCC_Audits__c sa = new SCC_Audits__c(scc__c = scc.id);
        insert sa;
        //CH02.End

        //CH02.Start
        HS_Evaluation__c sccC34 = new HS_Evaluation__c(  SCC__c = scc.id, Review_Requested_Date_del__c = System.toDay(), HS_Evaluation_Year__c ='Year 1', Expiry_Date__c = System.toDay());
        insert sccC34;
        //CH02.End
        
        SCC_Performance_Review__c pr = new SCC_Performance_Review__c(SCC_ID__c = scc.id, Review_Date__c = System.today());
        insert pr;
        
        SCC_Sent_Email_History__c seh = new SCC_Sent_Email_History__c(SCC__c = scc.id);
        insert seh;
            
            Blob picture = Blob.valueOf('');
            
            Attachment a = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            
            Note aa = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
            Note aa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
            Note aa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
            Note aa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
            Note aa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
            Note aa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
            Note aa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
            Note aa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
            
            Attachment[] lstAt = new List<Attachment>{a,a1,a2,a3,a4,a5,a6,a7};
            Note[] lstNote = new List<Note>{aa,aa1,aa2,aa3,aa4,aa5,aa6,aa7};
            
            insert lstAt;
            insert lstNote;
            test.stoptest();    
            
            Attachment aaa0 = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            
            Note aaaa0 = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
            Note aaaa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
            Note aaaa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
            
            Attachment[] lstAt1 = new List<Attachment>{aaa0,aaa1,aaa2,aaa3,aaa4,aaa5,aaa6,aaa7};
            Note[] lstNote1 = new List<Note>{aaaa0,aaaa1,aaaa2,aaaa3,aaaa4,aaaa5,aaaa6,aaaa7};
            
            
           try{
                insert lstAt1;
                insert lstNote1;
            }catch(Exception e){}
            
            
            
               
            All_ConnectionHelpler.owningRegion = 'North';
            All_ConnectionHelpler.getConnectionOwnerId(All_ConnectionHelpler.connectionOne);
            All_ConnectionHelpler.createPermissionError('North');
            All_ConnectionHelpler.updatePermissionError('North');
            All_ConnectionHelpler.deletePermissionError('North');
            
            
            
            All_ConnectionHelpler.createPersonel('a#@#a');
            All_ConnectionHelpler.createPersonel('a#@#a');
            
            try{
                update sccc;
            }catch(Exception e){}
            try{
                update sccAcc;
            }catch(Exception e){}
            try{
                update scc;
            }catch(Exception e){}
            try{
                update acc;
            }catch(Exception e){}
            try{
                update sc;
            }catch(Exception e){}
             try{
                 update sccC34;
            }catch(Exception e){}
            try{
                update pr;
            }catch(Exception e){}
            try{
                update seh;
            }catch(Exception e){}
           try{
                 update lstAt;
            }catch(Exception e){}
        }
    }// CH-01 START

    public static testMethod void testNonConnectionUser()
	{
     
      Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        
        User u = new User(Alias = 'pnet', Email='scctest@testorg.com',  LanguageLocaleKey='en_US', ProfileId = p.Id, firstName='Connection1',LastName='User1', username = 'connuser@test.com',EmailEncodingKey='UTF-8',TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US',CommunityNickname='ddff');
        
        System.runAs(u) 
		{
				system.debug('****************************'+UserInfo.getName());
				//System.assertEquals(u.username, null);
				
				string owningRegion = 'South';
			
				SCC_Clients__c sccc = new SCC_Clients__c(owning_region__c = owningRegion);
				insert sccc;
			
			SCC_Accreditations__c sccAcc = new SCC_Accreditations__c(Name = 'ISO 14001',Owning_Region__c = owningRegion);
			insert sccAcc;
			
			
			SCC__c scc = new SCC__c(Subcontractor_Name__c = 'abcd', Location__c = 'lll', Street__c = 'street', Town__c='ttt', Contact_Name__c = 'name', Number_of_Employees__c = 'Less than 5 employees', owning_region__c = owningRegion);
			insert scc;
			
			Accreditation__c acc = new Accreditation__c(UKAS_Certified__c='Yes',SCC__c = scc.id, SCC_Accreditations__c = sccAcc.id, Accreditation_Date__c= System.toDay(), Accreditation_Expiry_Date__c = System.toDay());
			insert acc; 
			//CH02.Start
			SCC_Client__c sc = new SCC_Client__c(scc__c = scc.id, Client_Names__c = sccc.id);
			insert sc;
			//CH02.End
			
			//CH02.Start
			SCC_Audits__c sa = new SCC_Audits__c(scc__c = scc.id);
			insert sa;
			//CH02.End

			//CH02.Start
			HS_Evaluation__c sccC34 = new HS_Evaluation__c(  SCC__c = scc.id, Review_Requested_Date_del__c = System.toDay(), HS_Evaluation_Year__c ='Year 1', Expiry_Date__c = System.toDay());
			insert sccC34;
			//CH02.End
			
			SCC_Performance_Review__c pr = new SCC_Performance_Review__c(SCC_ID__c = scc.id, Review_Date__c = System.today());
			insert pr;
			
			SCC_Sent_Email_History__c seh = new SCC_Sent_Email_History__c(SCC__c = scc.id);
			insert seh;
				
				Blob picture = Blob.valueOf('');
				
				Attachment a = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				
				Note aa = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
				Note aa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
				Note aa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
				Note aa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
				Note aa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
				Note aa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
				Note aa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
				Note aa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
				
				Attachment[] lstAt = new List<Attachment>{a,a1,a2,a3,a4,a5,a6,a7};
				Note[] lstNote = new List<Note>{aa,aa1,aa2,aa3,aa4,aa5,aa6,aa7};
				
				insert lstAt;
				insert lstNote;
				test.stoptest();    
				
				Attachment aaa0 = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				
				Note aaaa0 = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
				Note aaaa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
				Note aaaa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
				Note aaaa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
				Note aaaa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
				Note aaaa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
				Note aaaa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
				Note aaaa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
				
				Attachment[] lstAt1 = new List<Attachment>{aaa0,aaa1,aaa2,aaa3,aaa4,aaa5,aaa6,aaa7};
				Note[] lstNote1 = new List<Note>{aaaa0,aaaa1,aaaa2,aaaa3,aaaa4,aaaa5,aaaa6,aaaa7};
				
				
			   try{
					insert lstAt1;
					insert lstNote1;
				}catch(Exception e){}
				
				
				
				   
				All_ConnectionHelpler.owningRegion = 'North';
				All_ConnectionHelpler.getConnectionOwnerId(All_ConnectionHelpler.connectionOne);
				All_ConnectionHelpler.createPermissionError('North');
				All_ConnectionHelpler.updatePermissionError('North');
				All_ConnectionHelpler.deletePermissionError('North');
				
				
				
				All_ConnectionHelpler.createPersonel('a#@#a');
				All_ConnectionHelpler.createPersonel('a#@#a');
				
				try{
					update sccc;
				}catch(Exception e){}
				try{
					update sccAcc;
				}catch(Exception e){}
				try{
					update scc;
				}catch(Exception e){}
				try{
					update acc;
				}catch(Exception e){}
				try{
					update sc;
				}catch(Exception e){}
				 try{
					 update sccC34;
				}catch(Exception e){}
				try{
					update pr;
				}catch(Exception e){}
				try{
					update seh;
				}catch(Exception e){}
			   try{
					 update lstAt;
				}catch(Exception e){}
		}
    }// CH-01 START

    
}

Please mark this as solution if this will help you
 
TheLearnerTheLearner
Hi Amit,

its showing this error" System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Only South organisation can add a new Record.: []",,' method testNonConnectionUser() at line  "SCC_Audits__c sa = new SCC_Audits__c(scc__c = scc.id);

           insert sa;"

for this there is helper class which owing region set to CENTRAL, if we use owning region central its excuting if you use other than south its throwing error
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
 
@isTest
public with sharing class Testclassforsubcontractor 
{ 

    public static testMethod void test1(){
     
      Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        
        
        //User ux = [select id from User where  name = 'Connection User']; 
        User u = new User(Alias = 'pnet', Email='scctest@testorg.com',  LanguageLocaleKey='en_US', ProfileId = p.Id, firstName='Connection',LastName='User', username = 'connuser@test.com',EmailEncodingKey='UTF-8',TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US',CommunityNickname='ddff');
        
        System.runAs(u) {
            system.debug('****************************'+UserInfo.getName());
            //System.assertEquals(u.username, null);
            
            string owningRegion = 'South';
        
            SCC_Clients__c sccc = new SCC_Clients__c(owning_region__c = owningRegion);
            insert sccc;
        
        SCC_Accreditations__c sccAcc = new SCC_Accreditations__c(Name = 'ISO 14001',Owning_Region__c = owningRegion);
        insert sccAcc;
        
        
        SCC__c scc = new SCC__c(Subcontractor_Name__c = 'abcd', Location__c = 'lll', Street__c = 'street', Town__c='ttt', Contact_Name__c = 'name', Number_of_Employees__c = 'Less than 5 employees', owning_region__c = owningRegion);
        insert scc;
        
        Accreditation__c acc = new Accreditation__c(UKAS_Certified__c='Yes',SCC__c = scc.id, SCC_Accreditations__c = sccAcc.id, Accreditation_Date__c= System.toDay(), Accreditation_Expiry_Date__c = System.toDay());
        insert acc; 
        //CH02.Start
        SCC_Client__c sc = new SCC_Client__c(scc__c = scc.id, Client_Names__c = sccc.id);
        insert sc;
        //CH02.End
        
        //CH02.Start
        SCC_Audits__c sa = new SCC_Audits__c(scc__c = scc.id);
        insert sa;
        //CH02.End

        //CH02.Start
        HS_Evaluation__c sccC34 = new HS_Evaluation__c(  SCC__c = scc.id, Review_Requested_Date_del__c = System.toDay(), HS_Evaluation_Year__c ='Year 1', Expiry_Date__c = System.toDay());
        insert sccC34;
        //CH02.End
        
        SCC_Performance_Review__c pr = new SCC_Performance_Review__c(SCC_ID__c = scc.id, Review_Date__c = System.today());
        insert pr;
        
        SCC_Sent_Email_History__c seh = new SCC_Sent_Email_History__c(SCC__c = scc.id);
        insert seh;
            
            Blob picture = Blob.valueOf('');
            
            Attachment a = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            
            Note aa = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
            Note aa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
            Note aa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
            Note aa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
            Note aa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
            Note aa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
            Note aa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
            Note aa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
            
            Attachment[] lstAt = new List<Attachment>{a,a1,a2,a3,a4,a5,a6,a7};
            Note[] lstNote = new List<Note>{aa,aa1,aa2,aa3,aa4,aa5,aa6,aa7};
            
            insert lstAt;
            insert lstNote;
            test.stoptest();    
            
            Attachment aaa0 = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            
            Note aaaa0 = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
            Note aaaa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
            Note aaaa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
            
            Attachment[] lstAt1 = new List<Attachment>{aaa0,aaa1,aaa2,aaa3,aaa4,aaa5,aaa6,aaa7};
            Note[] lstNote1 = new List<Note>{aaaa0,aaaa1,aaaa2,aaaa3,aaaa4,aaaa5,aaaa6,aaaa7};
            
            
           try{
                insert lstAt1;
                insert lstNote1;
            }catch(Exception e){}
            
            
            
               
            All_ConnectionHelpler.owningRegion = 'North';
            All_ConnectionHelpler.getConnectionOwnerId(All_ConnectionHelpler.connectionOne);
            All_ConnectionHelpler.createPermissionError('North');
            All_ConnectionHelpler.updatePermissionError('North');
            All_ConnectionHelpler.deletePermissionError('North');
            
            
            
            All_ConnectionHelpler.createPersonel('a#@#a');
            All_ConnectionHelpler.createPersonel('a#@#a');
            
            try{
                update sccc;
            }catch(Exception e){}
            try{
                update sccAcc;
            }catch(Exception e){}
            try{
                update scc;
            }catch(Exception e){}
            try{
                update acc;
            }catch(Exception e){}
            try{
                update sc;
            }catch(Exception e){}
             try{
                 update sccC34;
            }catch(Exception e){}
            try{
                update pr;
            }catch(Exception e){}
            try{
                update seh;
            }catch(Exception e){}
           try{
                 update lstAt;
            }catch(Exception e){}
        }
    }// CH-01 START

    public static testMethod void testNonConnectionUser()
	{
     
      Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        
        User u = new User(Alias = 'pnet', Email='scctest@testorg.com',  LanguageLocaleKey='en_US', ProfileId = p.Id, firstName='Connection1',LastName='User1', username = 'connuser@test.com',EmailEncodingKey='UTF-8',TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US',CommunityNickname='ddff');
        
        System.runAs(u) 
		{
		 try{	
				system.debug('****************************'+UserInfo.getName());
				//System.assertEquals(u.username, null);
				
				string owningRegion = 'South';
			
				SCC_Clients__c sccc = new SCC_Clients__c(owning_region__c = owningRegion);
				insert sccc;
			
			SCC_Accreditations__c sccAcc = new SCC_Accreditations__c(Name = 'ISO 14001',Owning_Region__c = owningRegion);
			insert sccAcc;
			
			
			SCC__c scc = new SCC__c(Subcontractor_Name__c = 'abcd', Location__c = 'lll', Street__c = 'street', Town__c='ttt', Contact_Name__c = 'name', Number_of_Employees__c = 'Less than 5 employees', owning_region__c = owningRegion);
			insert scc;
			
			Accreditation__c acc = new Accreditation__c(UKAS_Certified__c='Yes',SCC__c = scc.id, SCC_Accreditations__c = sccAcc.id, Accreditation_Date__c= System.toDay(), Accreditation_Expiry_Date__c = System.toDay());
			insert acc; 
			//CH02.Start
			SCC_Client__c sc = new SCC_Client__c(scc__c = scc.id, Client_Names__c = sccc.id);
			insert sc;
			//CH02.End
			
			//CH02.Start
			SCC_Audits__c sa = new SCC_Audits__c(scc__c = scc.id);
			insert sa;
			//CH02.End

			//CH02.Start
			HS_Evaluation__c sccC34 = new HS_Evaluation__c(  SCC__c = scc.id, Review_Requested_Date_del__c = System.toDay(), HS_Evaluation_Year__c ='Year 1', Expiry_Date__c = System.toDay());
			insert sccC34;
			//CH02.End
			
			SCC_Performance_Review__c pr = new SCC_Performance_Review__c(SCC_ID__c = scc.id, Review_Date__c = System.today());
			insert pr;
			
			SCC_Sent_Email_History__c seh = new SCC_Sent_Email_History__c(SCC__c = scc.id);
			insert seh;
				
				Blob picture = Blob.valueOf('');
				
				Attachment a = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment a7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				
				Note aa = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
				Note aa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
				Note aa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
				Note aa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
				Note aa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
				Note aa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
				Note aa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
				Note aa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
				
				Attachment[] lstAt = new List<Attachment>{a,a1,a2,a3,a4,a5,a6,a7};
				Note[] lstNote = new List<Note>{aa,aa1,aa2,aa3,aa4,aa5,aa6,aa7};
				
				insert lstAt;
				insert lstNote;
				test.stoptest();    
				
				Attachment aaa0 = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				Attachment aaa7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
				
				Note aaaa0 = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
				Note aaaa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
				Note aaaa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
				Note aaaa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
				Note aaaa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
				Note aaaa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
				Note aaaa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
				Note aaaa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
				
				Attachment[] lstAt1 = new List<Attachment>{aaa0,aaa1,aaa2,aaa3,aaa4,aaa5,aaa6,aaa7};
				Note[] lstNote1 = new List<Note>{aaaa0,aaaa1,aaaa2,aaaa3,aaaa4,aaaa5,aaaa6,aaaa7};
				
				
			   try{
					insert lstAt1;
					insert lstNote1;
				}catch(Exception e){}
				
				
				
				   
				All_ConnectionHelpler.owningRegion = 'North';
				All_ConnectionHelpler.getConnectionOwnerId(All_ConnectionHelpler.connectionOne);
				All_ConnectionHelpler.createPermissionError('North');
				All_ConnectionHelpler.updatePermissionError('North');
				All_ConnectionHelpler.deletePermissionError('North');
				
				
				
				All_ConnectionHelpler.createPersonel('a#@#a');
				All_ConnectionHelpler.createPersonel('a#@#a');
				
				try{
					update sccc;
				}catch(Exception e){}
				try{
					update sccAcc;
				}catch(Exception e){}
				try{
					update scc;
				}catch(Exception e){}
				try{
					update acc;
				}catch(Exception e){}
				try{
					update sc;
				}catch(Exception e){}
				 try{
					 update sccC34;
				}catch(Exception e){}
				try{
					update pr;
				}catch(Exception e){}
				try{
					update seh;
				}catch(Exception e){}
			   try{
					 update lstAt;
				}catch(Exception e){}
		 }
		 catch(Exception ee)
		 {
		 }
		 
		}
    }// CH-01 START

    
}

Please mark this as solution if this will help you
 
TheLearnerTheLearner
Hi,
why im getting this error while copy pasting this code :"Compile Error: unexpected token: 'public with' at line 2 column 0"
TheLearnerTheLearner
HI Amit,

Could you write test class for this trigger please:

trigger S2S_Subcontractor_Audits on SCC_Audits__c (after insert, after update, before update, before insert) {
//CH01.End
    string userName = UserInfo.getName();
    String orgName =  UserInfo.getOrganizationName();
   
    /*if(Trigger.isBefore && Trigger.isUpdate){
        if(userName=='Connection User'){
            for (Subcontractor_Audits__c newScc: Trigger.new) {
                if(newScc.h_Source_H_S_Advisor__c == null || newScc.h_Source_H_S_Advisor__c == '')
                    continue;
                else{
                    Id personelId = All_ConnectionHelpler.createPersonel(newScc.h_Source_H_S_Advisor__c);
                    if(personelId != null)
                        newScc.H_S_Advisor__c = personelId;
                    else
                        newScc.H_S_Advisor__c = null;
                }  
            }
        }
    }*/
   
    if((Trigger.isDelete || Trigger.isUpdate || Trigger.isInsert) && Trigger.isBefore){
        if(userName !='Connection User'){
        //CH01.Start
            for (SCC_Audits__c newScc: Trigger.new) {
                if(newScc.Owning_Region__c != All_ConnectionHelpler.owningRegion) {
                    if(Trigger.isDelete)
                        newScc.addError(All_ConnectionHelpler.deletePermissionError(newScc.Owning_Region__c));
                    if(Trigger.isInsert)
                        newScc.addError(All_ConnectionHelpler.createPermissionError(newScc.Owning_Region__c));
                    if(Trigger.isUpdate){
                        if(newScc.SCC__c != Trigger.oldmap.get(newScc.id).SCC__c)
                            newScc.SCC__c.addError('Not Editable');
                        else
                            newScc.addError(All_ConnectionHelpler.updatePermissionError(newScc.Owning_Region__c));
                    }
                }
            }
            //CH01.End
        }
    }
     
    if((trigger.isUpdate) && Trigger.isAfter){     
        // Define connection id
        Id networkIdOne = All_ConnectionHelpler.getConnectionId(All_ConnectionHelpler.connectionOne);
        Id networkIdTwo = All_ConnectionHelpler.getConnectionId(All_ConnectionHelpler.connectionTwo); 
       
        List<PartnerNetworkRecordConnection> Connections =  new  List<PartnerNetworkRecordConnection>();
        //CH01.Start
        for (SCC_Audits__c subcontractorAudits: Trigger.new) {
            if(subcontractorAudits.ConnectionReceivedId != null)
                continue;          
            PartnerNetworkRecordConnection newConnection =
              new PartnerNetworkRecordConnection(
                  ConnectionId = networkIdOne,
                  LocalRecordId = subcontractorAudits.Id,
                  SendClosedTasks = false,
                  SendOpenTasks = false,
                  SendEmails = false);
                             
            Connections.add(newConnection);
        } //CH01.End
        //CH01.Start
        for (SCC_Audits__c subcontractorAudits: Trigger.new) {
            if(subcontractorAudits.ConnectionReceivedId != null)
                continue;          
            PartnerNetworkRecordConnection newConnection =
              new PartnerNetworkRecordConnection(
                  ConnectionId = networkIdTwo,
                  LocalRecordId = subcontractorAudits.Id,
                  SendClosedTasks = false,
                  SendOpenTasks = false,
                  SendEmails = false);
                 
            //Connections.add(newConnection);
        }//CH01.End
        if (Connections.size() > 0 ) {
           database.insert(Connections);
        }
      ////////////////////////////////// 
    } 
       
       
    if(Trigger.isBefore && Trigger.isUpdate){
    //CH01.Start
        for (SCC_Audits__c subcontractorAudits: Trigger.new) {
            //system.debug(subcontractorAudits.CreatedById.name);
            //String createdBy =
            if(!subcontractorAudits.CreatedBy__c.startswith('partnernetwork@'))
                subcontractorAudits.name = subcontractorAudits.h_contract_id__c;
            else
                subcontractorAudits.name = subcontractorAudits.h_org_id__c;
        }//CH01.End
    }
    if((trigger.isUpdate || trigger.isInsert) && Trigger.isBefore){ 
        Set<Id> receivedZTESTIds = new Set<Id>();
        //CH01.Start
        List<SCC_Audits__c> accreditations = new List<SCC_Audits__c>();
        //CH01.End
       
        // only share records created in this org, do not add ZTEST_Childs received from another org.
        //CH01.Start
        for (SCC_Audits__c subcontractorAudits: TRIGGER.new) {
            if ((subcontractorAudits.ConnectionReceivedId != null || userName=='Connection User') ) {/*&& subcontractorAudits.h_source_lookup_id__c!= Trigger.oldMap.get(subcontractorAudits.id).h_source_lookup_id__c*/
                accreditations.add(subcontractorAudits);
                receivedZTESTIds.add(subcontractorAudits.h_source_lookup_id__c);
            }        
        } //CH01.End
       
        PartnerNetworkRecordConnection[] sCCharingRecords = [SELECT Status, LocalRecordId, ConnectionId ,PartnerRecordId
                                       FROM PartnerNetworkRecordConnection              
                                       WHERE PartnerRecordId IN:receivedZTESTIds];
           //CH01.Start                                   
        for (SCC_Audits__c subcontractorAudits: accreditations) {
           
            // for each partner connection record for ZTEST_Child's ZTEST, check if it is active
            if(subcontractorAudits.h_source_lookup_id__c!=null && subcontractorAudits.h_source_lookup_id__c.startswith(SCC__c.sObjectType.getDescribe().getKeyPrefix()))
            {
                subcontractorAudits.SCC__c =  subcontractorAudits.h_source_lookup_id__c;
                continue;
            }
            for (PartnerNetworkRecordConnection sCCSharingRecord:sCCharingRecords){
                system.debug('PartnerRecordId'+sCCSharingRecord.PartnerRecordId+'   ');
                system.debug('h_source_lookup_id__c'+subcontractorAudits.h_source_lookup_id__c+'   ');
                if ((sCCSharingRecord.status.equalsignorecase('Sent') || sCCSharingRecord.status.equalsignorecase('Received'))
                    && (/*sCCSharingRecord.ConnectionId == subcontractorAudits.ConnectionReceivedId ||*/ userName=='Connection User') && sCCSharingRecord.PartnerRecordId == (Id)subcontractorAudits.h_source_lookup_id__c) 
                {
                    //if(Trigger.isInsert)
                    if(sCCSharingRecord.LocalRecordId!=null){
                        subcontractorAudits.SCC__c =  sCCSharingRecord.LocalRecordId;
                        break;
                    }
                }
            }
        }
        //CH01.End
    }
    if(Trigger.isAfter && Trigger.isInsert){
    //CH01.Start
       SCC_Audits__c[] scc = new List<SCC_Audits__c>();
        for (SCC_Audits__c subcontractorAudits: Trigger.new) {
            scc.add(new SCC_Audits__c(id = subcontractorAudits.id));
        }
        update scc;
        //CH01.End
    }
}
 
 
TheLearnerTheLearner
Hi Amit,

Actually that code thrworing error unexpected token public error, that code also not allowing to save.
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
 
@isTest
public class Testclassforsubcontractor 
{ 

    static testMethod void test1()
	{
     
      Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        
        
        //User ux = [select id from User where  name = 'Connection User']; 
        User u = new User(Alias = 'pnet', Email='scctest@testorg.com',  LanguageLocaleKey='en_US', ProfileId = p.Id, firstName='Connection',LastName='User', username = 'connuser@test.com',EmailEncodingKey='UTF-8',TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US',CommunityNickname='ddff');
        
        System.runAs(u) {
            system.debug('****************************'+UserInfo.getName());
            //System.assertEquals(u.username, null);
            
            string owningRegion = 'South';
        
            SCC_Clients__c sccc = new SCC_Clients__c(owning_region__c = owningRegion);
            insert sccc;
        
        SCC_Accreditations__c sccAcc = new SCC_Accreditations__c(Name = 'ISO 14001',Owning_Region__c = owningRegion);
        insert sccAcc;
        
        
        SCC__c scc = new SCC__c(Subcontractor_Name__c = 'abcd', Location__c = 'lll', Street__c = 'street', Town__c='ttt', Contact_Name__c = 'name', Number_of_Employees__c = 'Less than 5 employees', owning_region__c = owningRegion);
        insert scc;
        
        Accreditation__c acc = new Accreditation__c(UKAS_Certified__c='Yes',SCC__c = scc.id, SCC_Accreditations__c = sccAcc.id, Accreditation_Date__c= System.toDay(), Accreditation_Expiry_Date__c = System.toDay());
        insert acc; 
        //CH02.Start
        SCC_Client__c sc = new SCC_Client__c(scc__c = scc.id, Client_Names__c = sccc.id);
        insert sc;
        //CH02.End
        
        //CH02.Start
        SCC_Audits__c sa = new SCC_Audits__c(scc__c = scc.id);
        insert sa;
        //CH02.End

        //CH02.Start
        HS_Evaluation__c sccC34 = new HS_Evaluation__c(  SCC__c = scc.id, Review_Requested_Date_del__c = System.toDay(), HS_Evaluation_Year__c ='Year 1', Expiry_Date__c = System.toDay());
        insert sccC34;
        //CH02.End
        
        SCC_Performance_Review__c pr = new SCC_Performance_Review__c(SCC_ID__c = scc.id, Review_Date__c = System.today());
        insert pr;
        
        SCC_Sent_Email_History__c seh = new SCC_Sent_Email_History__c(SCC__c = scc.id);
        insert seh;
            
            Blob picture = Blob.valueOf('');
            
            Attachment a = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment a7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            
            Note aa = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
            Note aa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
            Note aa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
            Note aa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
            Note aa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
            Note aa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
            Note aa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
            Note aa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
            
            Attachment[] lstAt = new List<Attachment>{a,a1,a2,a3,a4,a5,a6,a7};
            Note[] lstNote = new List<Note>{aa,aa1,aa2,aa3,aa4,aa5,aa6,aa7};
            
            insert lstAt;
            insert lstNote;
            test.stoptest();    
            
            Attachment aaa0 = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            Attachment aaa7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
            
            Note aaaa0 = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
            Note aaaa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
            Note aaaa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
            Note aaaa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
            
            Attachment[] lstAt1 = new List<Attachment>{aaa0,aaa1,aaa2,aaa3,aaa4,aaa5,aaa6,aaa7};
            Note[] lstNote1 = new List<Note>{aaaa0,aaaa1,aaaa2,aaaa3,aaaa4,aaaa5,aaaa6,aaaa7};
            
            
           try{
                insert lstAt1;
                insert lstNote1;
            }catch(Exception e){}
            
            
            
               
            All_ConnectionHelpler.owningRegion = 'North';
            All_ConnectionHelpler.getConnectionOwnerId(All_ConnectionHelpler.connectionOne);
            All_ConnectionHelpler.createPermissionError('North');
            All_ConnectionHelpler.updatePermissionError('North');
            All_ConnectionHelpler.deletePermissionError('North');
            
            
            
            All_ConnectionHelpler.createPersonel('a#@#a');
            All_ConnectionHelpler.createPersonel('a#@#a');
            
            try{
                update sccc;
            }catch(Exception e){}
            try{
                update sccAcc;
            }catch(Exception e){}
            try{
                update scc;
            }catch(Exception e){}
            try{
                update acc;
            }catch(Exception e){}
            try{
                update sc;
            }catch(Exception e){}
             try{
                 update sccC34;
            }catch(Exception e){}
            try{
                update pr;
            }catch(Exception e){}
            try{
                update seh;
            }catch(Exception e){}
           try{
                 update lstAt;
            }catch(Exception e){}
        }
    }// CH-01 START

    static testMethod void testNonConnectionUser()
	{
     
      Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        
        User u = new User(Alias = 'pnet', Email='scctest@testorg.com',  LanguageLocaleKey='en_US', ProfileId = p.Id, firstName='Connection1',LastName='User1', username = 'connuser@test.com',EmailEncodingKey='UTF-8',TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US',CommunityNickname='ddff');
        
        System.runAs(u) 
		{
			try
			{	
					system.debug('****************************'+UserInfo.getName());
					//System.assertEquals(u.username, null);
					
					string owningRegion = 'South';
				
					SCC_Clients__c sccc = new SCC_Clients__c(owning_region__c = owningRegion);
					insert sccc;
				
				SCC_Accreditations__c sccAcc = new SCC_Accreditations__c(Name = 'ISO 14001',Owning_Region__c = owningRegion);
				insert sccAcc;
				
				
				SCC__c scc = new SCC__c(Subcontractor_Name__c = 'abcd', Location__c = 'lll', Street__c = 'street', Town__c='ttt', Contact_Name__c = 'name', Number_of_Employees__c = 'Less than 5 employees', owning_region__c = owningRegion);
				insert scc;
				
				Accreditation__c acc = new Accreditation__c(UKAS_Certified__c='Yes',SCC__c = scc.id, SCC_Accreditations__c = sccAcc.id, Accreditation_Date__c= System.toDay(), Accreditation_Expiry_Date__c = System.toDay());
				insert acc; 
				//CH02.Start
				SCC_Client__c sc = new SCC_Client__c(scc__c = scc.id, Client_Names__c = sccc.id);
				insert sc;
				//CH02.End
				
				//CH02.Start
				SCC_Audits__c sa = new SCC_Audits__c(scc__c = scc.id);
				insert sa;
				//CH02.End

				//CH02.Start
				HS_Evaluation__c sccC34 = new HS_Evaluation__c(  SCC__c = scc.id, Review_Requested_Date_del__c = System.toDay(), HS_Evaluation_Year__c ='Year 1', Expiry_Date__c = System.toDay());
				insert sccC34;
				//CH02.End
				
				SCC_Performance_Review__c pr = new SCC_Performance_Review__c(SCC_ID__c = scc.id, Review_Date__c = System.today());
				insert pr;
				
				SCC_Sent_Email_History__c seh = new SCC_Sent_Email_History__c(SCC__c = scc.id);
				insert seh;
					
					Blob picture = Blob.valueOf('');
					
					Attachment a = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment a7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					
					Note aa = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
					Note aa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
					Note aa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
					Note aa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
					Note aa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
					Note aa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
					Note aa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
					Note aa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
					
					Attachment[] lstAt = new List<Attachment>{a,a1,a2,a3,a4,a5,a6,a7};
					Note[] lstNote = new List<Note>{aa,aa1,aa2,aa3,aa4,aa5,aa6,aa7};
					
					insert lstAt;
					insert lstNote;
					test.stoptest();    
					
					Attachment aaa0 = new Attachment (ParentId = sccc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa1 = new Attachment (ParentId = sc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa2 = new Attachment (ParentId = sccAcc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa3 = new Attachment (ParentId = scc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa4 = new Attachment (ParentId = acc.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa5 = new Attachment (ParentId = sccC34.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa6 = new Attachment (ParentId = pr.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					Attachment aaa7 = new Attachment (ParentId = seh.id,Body = picture,ContentType = 'image/jpg',Name = 'VehiclePicture');
					
					Note aaaa0 = new Note (ParentId = sccc.id,Body='***',Title = 'VehiclePicture');
					Note aaaa1 = new Note (ParentId = sc.id,Body='***',Title = 'VehiclePicture');
					Note aaaa2 = new Note (ParentId = sccAcc.id,Body='***',Title = 'VehiclePicture');
					Note aaaa3 = new Note (ParentId = scc.id,Body='***',Title = 'VehiclePicture');
					Note aaaa4 = new Note (ParentId = acc.id,Body='***',Title = 'VehiclePicture');
					Note aaaa5 = new Note (ParentId = sccC34.id,Body='***',Title = 'VehiclePicture');
					Note aaaa6 = new Note (ParentId = pr.id,Body='***',Title = 'VehiclePicture');
					Note aaaa7 = new Note (ParentId = seh.id,Body='***',Title = 'VehiclePicture');
					
					Attachment[] lstAt1 = new List<Attachment>{aaa0,aaa1,aaa2,aaa3,aaa4,aaa5,aaa6,aaa7};
					Note[] lstNote1 = new List<Note>{aaaa0,aaaa1,aaaa2,aaaa3,aaaa4,aaaa5,aaaa6,aaaa7};
					
					
				   try{
						insert lstAt1;
						insert lstNote1;
					}catch(Exception e){}
					
					
					
					   
					All_ConnectionHelpler.owningRegion = 'North';
					All_ConnectionHelpler.getConnectionOwnerId(All_ConnectionHelpler.connectionOne);
					All_ConnectionHelpler.createPermissionError('North');
					All_ConnectionHelpler.updatePermissionError('North');
					All_ConnectionHelpler.deletePermissionError('North');
					
					
					
					All_ConnectionHelpler.createPersonel('a#@#a');
					All_ConnectionHelpler.createPersonel('a#@#a');
					
					try{
						update sccc;
					}catch(Exception e){}
					try{
						update sccAcc;
					}catch(Exception e){}
					try{
						update scc;
					}catch(Exception e){}
					try{
						update acc;
					}catch(Exception e){}
					try{
						update sc;
					}catch(Exception e){}
					 try{
						 update sccC34;
					}catch(Exception e){}
					try{
						update pr;
					}catch(Exception e){}
					try{
						update seh;
					}catch(Exception e){}
				   try{
						 update lstAt;
					}catch(Exception e){}
			}
			 catch(Exception ee)
			 {
			 }
		 
		}
    }// CH-01 START
   
}


Please let us know above solution helps you. If that will help you then please select the above solution as best solution so that if some one have same problem that post can help other.

Thanks
Amit Chaudhary

 
This was selected as the best answer
TheLearnerTheLearner
Same error amit,

please could you write test class for the tirrger which i posted please
TheLearnerTheLearner
Hi Amit,

Thanks alot, its working improved from 72 to 74%. could you able to write some more please 
TheLearnerTheLearner
HI Amit,

Thanks for the help, i improved it 100%, thanks alot
rajasekhar reddy 161rajasekhar reddy 161
I have one profile with two users. I want to show that zone picklist data type . I want different picklist visible to each user like north user to north zone and south user to South zone only by default . How to achieve it without cloning that profile ?