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
sam_Adminsam_Admin 

Help with test class for S2S trigger

Can anyone help me with test class for below trigger?

Trigger AutoforwardOpp on Opportunity(after insert, after update)
{
    List <PartnerNetworkRecordConnection> prncList;
    List <PartnerNetworkConnection> connectionList;
    Map <ID, PartnerNetworkConnection> connMap;
    Map <ID, ID> oppIdvsAccountIdMap;
    Map<ID, Account> accountWithContactMap;
    
    ID cid;
    String status;
    String connName;

    if(Trigger.isafter  && (Trigger.isInsert || Trigger.isUpdate)){
            
        connectionList = new List<PartnerNetworkConnection>();
        prncList = new List<PartnerNetworkRecordConnection>();
        
        //This would ideally return multiple active Connections if they exist hence its best you use a
        //criteria to ensure only the appropriate connection record is returned.
        connMap = new Map<ID, PartnerNetworkConnection>(
            [Select ID, ConnectionStatus, ConnectionName
             From PartnerNetworkConnection
             Where ConnectionStatus = 'Accepted']);
        
        //get connection details        
        for(ID connId :connMap.keySet()){
            cid = connMap.get(connId).ID;
            status = connMap.get(connId).ConnectionStatus;
            connName = connMap.get(connId).ConnectionName;
        }
        
        //Populate a map of Opp Ids and associated Account Ids
        oppIdvsAccountIdMap = new Map<ID, ID>();
        for(Opportunity oppRecord :Trigger.new){
        
            if(oppRecord.Name.contains('US')|| oppRecord.Name.contains('VSI')){
                    //System.debug('Opp Id: ' + oppRecord.ID + '-> Account Id: ' + oppRecord.AccountId);
                    oppIdvsAccountIdMap.put(oppRecord.ID, oppRecord.AccountId);
            }
        }
        
        //Get associated Accounts and Contacts for every US opportunity if they exist
        if(oppIdvsAccountIdMap.keySet().size() > 0){
        
            accountWithContactMap = new Map<ID, Account>(
                [Select ID, Name,
                    (Select ID, Name, Account.Id From Account.Contacts)
                 From Account
                 Where ID IN :oppIdvsAccountIdMap.values()]);

            //Create PartnerNetworkRecordConnections for sharing the records
            for(ID oppId : oppIdvsAccountIdMap.keySet()){
            
                ID accountId = oppIdvsAccountIdMap.get(oppId);
                
                //Share Opportunity
                prncList.add(new PartnerNetworkRecordConnection(
                    ConnectionId = cid,
                     LocalRecordId = oppId,
                    SendClosedTasks = true,
                    SendOpenTasks = true,
                    SendEmails = true));
                
                //Share Account
                if(oppIdvsAccountIdMap.get(oppId) != null){
                
                    prncList.add(new PartnerNetworkRecordConnection(
                        ConnectionId = cid,
                        LocalRecordId = accountId,
                        SendClosedTasks = true,
                        SendOpenTasks = true,
                        SendEmails = true));
                }
                
                //Share associated Contacts
                if(accountWithContactMap.get(accountId).Contacts != null){
                                   
                    for(Contact contact :accountWithContactMap.get(accountId).Contacts){
                    
                        prncList.add(new PartnerNetworkRecordConnection(
                            ConnectionId = cid,
                            LocalRecordId = contact.ID,
                         // ParentRecordId = Contact.AccountId,
                            SendClosedTasks = true,
                            SendOpenTasks = true,
                            SendEmails = true));
                    }
                }
            }//for
            
            //Insert record connections
            if(!prncList.isEmpty()){
            
                try{
                    insert prncList;
                }
                catch(System.Dmlexception dmlExceptionInstance){
                    System.debug('Record Share Error:' + dmlExceptionInstance.getMessage());
                }
            }
        }//if
    }
}
Best Answer chosen by sam_Admin
jigarshahjigarshah
Sam,

You may want to check if the Test Data that you created within your test class complies with any custom Validation Rule or Required field settings which may cause your test class to fail. Please verify and create the test data accordingly to add the necessary fields or data and that should fix the issue for you.

Please do not forget to mark this thread as SOLVED and asnwer as the BEST ANSWER if it helps address your issue. 

All Answers

DeveloperDeveloper
Hi Sam,

May I request you to please check for Test Class Generator App from APP Exchange.Please refer the below link.
https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EFozgUAD


I hope it will be helpful.

Please mark it as best answer if the information is informative.

Thanks & Regards 
Gopal M.
 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi,
Do You got a chance to check the Test generator app from App Exchange please check the below link Hope it will be helpful.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar
 
sam_Adminsam_Admin
Here is my test class and i got the below error, how to fix it?

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, &#24517;&#39035;&#36755;&#20837;&#19968;&#20010;&#20540; &#37319;&#36141;&#35774;&#22791;/&#31995;&#32479;: [FFCNSales_PurchaseEquipment__c]


@istest

public class AutoforwardOpp
{
  static testmethod void myTest()
  {     
        Account acc = new Account(Name = 'test', Province__c = 'China', HospitalCatalog__c = 'Test', HospitalLevel__c = 'High', HospitalGrade__c = 'First', HospitalOwner__c = 'Test', Phone = '1234567890');
        insert acc;

        Contact con = new Contact(LastName = 'Test2', Email = 'test.user@gmail.com', Department = 'IT', Title = 'Mr', AccountId = acc.Id, FFSS_Account_Name__c = 'test');
        insert con;
        
        Product2 prd = new Product2(Name = 'Test');
        insert prd;

        Opportunity opp = new Opportunity(Name = 'Test', AccountId = acc.Id, StageName = '101', FFCNSales_PurchaseEquipment__c = prd.Id, Amount = 50, CloseDate = Date.newInstance(2009,02,01), FFSS_Account_Name__c = 'SonoSite');
        insert opp;     
  }
}
jigarshahjigarshah
Sam,

You may want to check if the Test Data that you created within your test class complies with any custom Validation Rule or Required field settings which may cause your test class to fail. Please verify and create the test data accordingly to add the necessary fields or data and that should fix the issue for you.

Please do not forget to mark this thread as SOLVED and asnwer as the BEST ANSWER if it helps address your issue. 
This was selected as the best answer
jigarshahjigarshah
Sam,

I just found this Blog Post (https://automationchampion.com/2015/03/13/getting-started-with-process-builder-part-10-auto-forward-records-to-a-connection/) which uses an out of box Process Builder approach to automatically share records to a Salesforce to Salesforce connection without having the need to write code. You might want to give it a try since that would help you save all the effort and pain of writing test code for the earlier written Apex Trigger during your production deployment.