• Swaroopa Akula 10
  • NEWBIE
  • 10 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 5
    Replies

Apex class:/*




public class OpportunityTriggerHandlerverifiedIO {
    private static Boolean isExecuting = false;

    public static void handleClosedWonOpportunities(List<Opportunity> newOpportunities, Map<Id, Opportunity> oldOpportunityMap) {
        if (isExecuting) {
            // Skip trigger logic to avoid recursion
            return;
        }

        isExecuting = true;

        Set<Id> oppIds = new Set<Id>();
        Map<Id, APXT_Redlining__Contract_Agreement__c> oppToContractAgreementMap = new Map<Id, APXT_Redlining__Contract_Agreement__c>();
        
        for (Opportunity opp : newOpportunities) {
            // Check if the Opportunity is transitioning to 'Closed Won' and not a new Opportunity
            if (opp.StageName == 'Closed Won' && oldOpportunityMap.containsKey(opp.Id)) {
                oppIds.add(opp.Id);
            }
        }

        if (!oppIds.isEmpty()) {
            List<APXT_Redlining__Contract_Agreement__c> contractAgreements = [SELECT Id, Opportunity__c, APXT_Redlining__Status__c, Legal_review_completed__c,X3rd_Party_Paper__c, Customer_Signed_Date__c
                                                                            FROM APXT_Redlining__Contract_Agreement__c 
                                                                            WHERE CreatedDate > = 2023-08-13T15:13:08.000+0000 and Opportunity__c IN :oppIds];
            
           If(!contractAgreements.isEmpty()){
            
            for (APXT_Redlining__Contract_Agreement__c c : contractAgreements) {
                // Build the map of Opportunity Ids to Contract Agreement records
                oppToContractAgreementMap.put(c.Opportunity__c, c);
            }

            
            for (Opportunity opp : newOpportunities) {

                // Check if the Opportunity is transitioning to 'Closed Won' and has an existing Contract Agreement
                if (opp.StageName == 'Closed Won' && oppToContractAgreementMap.containsKey(opp.Id)) {
                    APXT_Redlining__Contract_Agreement__c contractAgreement = oppToContractAgreementMap.get(opp.Id);

                    
                    // Check if the contract agreement is active and not verified
                    if (contractAgreement.APXT_Redlining__Status__c == 'Active' && !contractAgreement.Legal_review_completed__c && contractAgreement.X3rd_Party_Paper__c == true )
                    //if (contractAgreement.APXT_Redlining__Status__c == 'Active' && !contractAgreement.Legal_review_completed__c && contractAgreement.X3rd_Party_Paper__c == true)||(contractAgreement)
                    {
                        opp.addError('Third party paper must be approved by Legal, please submit a "Legal Case".');
                    }
                    
                    else if((contractAgreement.APXT_Redlining__Status__c == 'Active'&& contractAgreement.Customer_Signed_Date__c==null)||(!(contractAgreement.APXT_Redlining__Status__c == 'Active')))
                    {
                        opp.addError('"To ‘Closed won’ the Opportunity, the Contract must be an active Contract Agreement and the customer must be signed".');
                    }

                
                }
                else if (opp.StageName == 'Closed Won' && !oppToContractAgreementMap.containsKey(opp.Id)) {
                    opp.addError('Closed Won Opportunity must have a contract agreement.');
                }

            }
        }
        }

        isExecuting = false;
    }
}

 

Trigger:

Trigger OpportunityTriggerHandlerverifiedIO on Opportunity (before update) {
    OpportunityTriggerHandlerverifiedIO.handleClosedWonOpportunities(Trigger.new, Trigger.oldMap);
}

 

 

Test class:
@isTest
private class OpportunityTriggerHandlerverifiedIOTest {
    @isTest
    static void testOpportunityTriggerHandler() {
        // Create test data
        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity1',
            StageName = 'Sales Meeting Set',
            CloseDate = Date.today(),
            Start_Date__c=Date.today(),
            End_Date__c =Date.today(),
            Contact_Role_added__c = true,
            Billing_Term__c = 'Net 30',
            Type='New Logo',
            Billing_Account_Name__c='Test Account',
            Billing_Contact_Name__c='Test contact name',
            Billing_Email__c='test@gmail.com',
            Billing_Street__c='1234 test street',
            Billing_Postal_Code__c='75046',
            Billing_Country__c='US',
            Billing_City__c='Dallas',
            Deal_Term_In_Months__c='12',
            Is_timeframe_to_launch_within_30_days__c='Yes'
        );
        insert testOpportunity;

        APXT_Redlining__Contract_Agreement__c contract = new APXT_Redlining__Contract_Agreement__c(
            Opportunity__c = testOpportunity.Id,
            APXT_Redlining__Status__c = 'Active',
             Legal_review_completed__c = true,
            X3rd_Party_Paper__c = true,
            Customer_Signed_Date__c=Date.today()
        );
        insert contract;

     // Update the Opportunity to trigger the trigger
        testOpportunity.StageName = 'Closed Lost';
        testOpportunity.Reason_Lost__c = 'Went with Competitor – Price';
        try {
            Test.startTest();
            update testOpportunity;
            Test.stopTest();
            // If no exception is thrown, the test should fail
            //System.assert(false, 'Expected DMLException but no exception was thrown.');
        } catch (DmlException e) {
            // Assert the error message for Closed Lost Opportunity
            
            
             Boolean expectedExceptionThrown =  e.getMessage().contains('Closed Won Opportunity must have a contract agreement') ? true : false;
           System.AssertEquals(expectedExceptionThrown, true);          
            //System.assertEquals('Closed Won Opportunity must have a contract agreement.', e.getMessage());
        }
        

        // Update the Opportunity again to meet the conditions
        testOpportunity.StageName = 'Closed Won';
        try {
          //  Test.startTest();
            update testOpportunity;
           // Test.stopTest();
            // If no exception is thrown, the test should fail
            //System.assert(false, 'Expected DMLException but no exception was thrown.');
        }catch (DmlException e) {
    System.debug('Caught Exception Message: ' + e.getMessage());
    System.assertEquals('Third party paper must be approved by Legal, please submit a "Legal Case".', e.getMessage());
}

        
        
         

        // Update the Contract Agreement to meet the conditions
        contract.Verified_Signed_IO__c = true;
        update contract;

        // Update the Opportunity again to satisfy the conditions
        testOpportunity.StageName = 'Closed Won';
        try {
            //Test.startTest();
            update testOpportunity;
           // Test.stopTest();
            // If an exception is thrown, the test should fail
            System.assert(true, 'No exception should be thrown.');
        } catch (DmlException e) {
            // If an exception is thrown, the test should fail
            System.assert(false, 'No exception should be thrown.');
        }
    }
}


Above test class is covering only 53%. I have updated the lines in bold which are not covered. Can anyone help me fix this code coverage issue

Apex class:

public class OpportunityTriggerHandlerverifiedIO {
    public static void handleClosedWonOpportunities(List<Opportunity> newOpportunities) {
        Set<Id> oppIds = new Set<Id>();
        List<APXT_Redlining__Contract_Agreement__c> oppWithContractAgreement = new List<APXT_Redlining__Contract_Agreement__c>();
        List<Opportunity> opportunity = new List<Opportunity>();

        for (Opportunity opp : newOpportunities) {
            if (opp.StageName == 'Closed Won') {
                oppIds.add(opp.Id);
                opportunity.add(opp);
            }
        }

        List<APXT_Redlining__Contract_Agreement__c> con = [SELECT Id, Opportunity__c, APXT_Redlining__Status__c, Verified_Signed_IO__c FROM APXT_Redlining__Contract_Agreement__c WHERE Opportunity__c IN :oppIds];

        if (con.size() > 0) {
            System.debug('Number of Contract Agreements: ' + con.size());
            for (APXT_Redlining__Contract_Agreement__c c : con) {
                if (c.APXT_Redlining__Status__c == 'Active' && c.Verified_Signed_IO__c == false) {
                    oppWithContractAgreement.add(c);
                    System.debug('-----------' + oppWithContractAgreement.size());
                }
            }
        }

        for (Opportunity opps : opportunity) {
            if (oppWithContractAgreement != null && oppWithContractAgreement.size() > 0) {
                System.debug('Number of Opportunities with Contract Agreements: ' + oppWithContractAgreement.size());
                opps.addError('Third party paper must be approved by Legal, please submit a “Legal Case');
            }

            if (con.size() == 0) {
                opps.addError('Closed Won Opportunity must have a contract agreement.');
            }
        }
    }
}


Test class:

@isTest
private class OpportunityTriggerHandlerverifiedIOTest1 {
    @isTest
    static void testHandleClosedWonOpportunities() {
        // Create test data
        Opportunity testOpportunity1 = new Opportunity(
            Name = 'Test Opportunity1',
            StageName = 'Sales Meeting Set',
            CloseDate = Date.today(),
            Contact_Role_added__c = true,
            Billing_Term__c = 'Net 30'
        );
        insert testOpportunity1;

        // Test: No contract agreement, StageName = Closed Won
        testOpportunity1.StageName = 'Closed Won';
        Test.startTest();
        try {
            update testOpportunity1;
            System.assert(false, 'Expected DMLException but no exception was thrown.');
        } catch (DmlException e) {
            // Assert the error message for no contract agreement
            System.assertEquals('Closed Won Opportunity must have a contract agreement.', e.getMessage());
        }
        Test.stopTest();

        // Create an active contract agreement without verified signed IO
        APXT_Redlining__Contract_Agreement__c contract1 = new APXT_Redlining__Contract_Agreement__c(
            Opportunity__c = testOpportunity1.Id,
            APXT_Redlining__Status__c = 'Active',
            Verified_Signed_IO__c = false,
            X3rd_Party_Paper__c = true
        );
        insert contract1;

        // Test: Contract agreement with Active status, but Verified_Signed_IO__c = false, StageName = Closed Won
        testOpportunity1.StageName = 'Closed Won';
        Test.startTest();
        try {
            update testOpportunity1;
            System.assert(false, 'Expected DMLException but no exception was thrown.');
        } catch (DmlException e) {
            // Assert the error message for contract agreement without verified signed IO
            System.assertEquals('Third party paper must be approved by Legal, please submit a “Legal Case”.', e.getMessage());
        }
        Test.stopTest();

        // Update the Contract Agreement to meet the conditions
        contract1.Verified_Signed_IO__c = true;
        update contract1;

        // Test: Contract agreement with Active status and Verified_Signed_IO__c = true, StageName = Closed Won
        testOpportunity1.StageName = 'Closed Won';
        Test.startTest();
        update testOpportunity1;
        Test.stopTest();

        // Test: No contract agreement, StageName = Closed Lost
        testOpportunity1.StageName = 'Closed Lost';
        Test.startTest();
        try {
            update testOpportunity1;
            System.assert(false, 'Expected DMLException but no exception was thrown.');
        } catch (DmlException e) {
            // Assert the error message for no contract agreement
            System.assertEquals('Closed Won Opportunity must have a contract agreement.', e.getMessage());
        }
        Test.stopTest();

        // Create another opportunity with a different Billing Term and without contract agreement
        Opportunity testOpportunity2 = new Opportunity(
            Name = 'Test Opportunity2',
            StageName = 'Sales Meeting Set',
            CloseDate = Date.today(),
            Contact_Role_added__c = true,
            Billing_Term__c = 'Net 15'
        );
        insert testOpportunity2;

        // Test: No contract agreement, StageName = Closed Won
        testOpportunity2.StageName = 'Closed Won';
        Test.startTest();
        try {
            update testOpportunity2;
            System.assert(false, 'Expected DMLException but no exception was thrown.');
        } catch (DmlException e) {
            // Assert the error message for no contract agreement
            System.assertEquals('Closed Won Opportunity must have a contract agreement.', e.getMessage());
        }
        Test.stopTest();
    }
}


Code covering only 50%. Can someone help me how to achieve 100% code coverge
I have a trigger and i am trying to create a test class. But not sure how to write test class to fulfill the test coding coverage for test apex class


TRIGGER:



trigger opportunitytrigger on opportunity(after update){
    Set<Id> oppIds = new Set<Id>();
   // Set<Id> oppWithContractAgreement = new Set<Id>();
     List<APXT_Redlining__Contract_Agreement__c> oppWithContractAgreement = new List<APXT_Redlining__Contract_Agreement__c>();
     List<Opportunity> opportunity = new List<Opportunity>();
     
    for(opportunity opp:Trigger.new)
    {
        if(opp.Stagename=='Closed Won')
        {
            oppIds.add(opp.id);
            opportunity.add(opp);

        }

    }

List<APXT_Redlining__Contract_Agreement__c> con =[Select Id, Opportunity__c,APXT_Redlining__Status__c,Verified_Signed_IO__c from APXT_Redlining__Contract_Agreement__c  where Opportunity__c IN :oppIds];
    
    {
     if(con.size()>0)
     system.debug(+con.size());
      
      {
          for(APXT_Redlining__Contract_Agreement__c c :con)
          
          {
          if( c.APXT_Redlining__Status__c == 'Active' && c.Verified_Signed_IO__c ==FALSE){
            oppWithContractAgreement.add(c);
            system.debug('-----------'+oppWithContractAgreement.size());
            }
}
      }
    }

 For(Opportunity opps:opportunity)
 {
    if(oppWithContractAgreement.size()>0)
    {
        system.debug(oppWithContractAgreement.size());
        opps.addError('Closed Won Opportunity must need to have active contract agreement and customer signed date captured or Closed Won Opportunity must need to have active contract agreement');
    }
    
    if(con.size()==0)
    {
        opps.addError('Closed Won Opportunity must need to have contract agreement' );
    }
    
 }

}


TEST CLASS:

@isTest
private class OpportunityTriggerHandler1_Test {
    
    @isTest static void testCreateOpportunityTarget() {
        
        //create Account
        Account acc = new Account();
        acc.Name = 'Test Account';
        insert acc;
        
        
        Test.startTest();
        
        Opportunity opp = new Opportunity();
        opp.AccountId = acc.Id;
        opp.Name = 'Test Opportunity';
        opp.StageName = 'Closed Won';
        opp.CloseDate = Date.Today().addDays(20);
        insert opp;
        
        Test.stopTest();
        
        List<APXT_Redlining__Contract_Agreement__c> CA = new List<APXT_Redlining__Contract_Agreement__c>();
        CA = [Select Id, Opportunity__c,APXT_Redlining__Status__c,Verified_Signed_IO__c from APXT_Redlining__Contract_Agreement__c ];

        System.assert(CA[0].Opportunity__c == opp.Id); 
    }      
}
Can we call a flow and keep the validation rule from it when the user clicks on button?
we have a requirement that when if a Conga transaction record has 'Sent' status , it should not allow the user to create a new conga transaction record. I created a trigger and apex handler class.

trigger congatransaction on APXT_CongaSign__Transaction__c (before INSERT) {
    
   congatransactionHandler.congatransactionMethod(Trigger.NewMap,Trigger.OldMap);
   
   }


public class congatransactionHandler {
    
    public static void congatransactionMethod(Map<Id,APXT_CongaSign__Transaction__c> newconMap, Map<Id,APXT_CongaSign__Transaction__c> oldconMap){
        Set<Id> conIds = New Set<Id>();
        List<APXT_CongaSign__Transaction__c> conList = New List<APXT_CongaSign__Transaction__c>();
        
        for(APXT_CongaSign__Transaction__c con : newconMap.values()){
               APXT_CongaSign__Transaction__c oldcontranmap = oldconMap.get(con.Id);
             if(oldconMap!=null && oldconMap.size()>0)
    // Trigger.new records are conveniently the "new" versions!
{        //Boolean oldcontransactionmap = oldcontranmap.APXT_CongaSign__Status__c.equals('SENT'); 
            if((newconMap.get(con.Id).Parent_a29__c== oldcontranmap.Parent_a29__c) && oldcontranmap.APXT_CongaSign__Status__c.equals('SENT')){


          con.adderror('You have already sent the conga transaction');
            }
        }
          
    }
}
}
Apex class:

public class OpportunityTriggerHandlerverifiedIO {
    public static void handleClosedWonOpportunities(List<Opportunity> newOpportunities) {
        Set<Id> oppIds = new Set<Id>();
        List<APXT_Redlining__Contract_Agreement__c> oppWithContractAgreement = new List<APXT_Redlining__Contract_Agreement__c>();
        List<Opportunity> opportunity = new List<Opportunity>();

        for (Opportunity opp : newOpportunities) {
            if (opp.StageName == 'Closed Won') {
                oppIds.add(opp.Id);
                opportunity.add(opp);
            }
        }

        List<APXT_Redlining__Contract_Agreement__c> con = [SELECT Id, Opportunity__c, APXT_Redlining__Status__c, Verified_Signed_IO__c FROM APXT_Redlining__Contract_Agreement__c WHERE Opportunity__c IN :oppIds];

        if (con.size() > 0) {
            System.debug('Number of Contract Agreements: ' + con.size());
            for (APXT_Redlining__Contract_Agreement__c c : con) {
                if (c.APXT_Redlining__Status__c == 'Active' && c.Verified_Signed_IO__c == false) {
                    oppWithContractAgreement.add(c);
                    System.debug('-----------' + oppWithContractAgreement.size());
                }
            }
        }

        for (Opportunity opps : opportunity) {
            if (oppWithContractAgreement != null && oppWithContractAgreement.size() > 0) {
                System.debug('Number of Opportunities with Contract Agreements: ' + oppWithContractAgreement.size());
                opps.addError('Third party paper must be approved by Legal, please submit a “Legal Case');
            }

            if (con.size() == 0) {
                opps.addError('Closed Won Opportunity must have a contract agreement.');
            }
        }
    }
}


Test class:

@isTest
private class OpportunityTriggerHandlerverifiedIOTest1 {
    @isTest
    static void testHandleClosedWonOpportunities() {
        // Create test data
        Opportunity testOpportunity1 = new Opportunity(
            Name = 'Test Opportunity1',
            StageName = 'Sales Meeting Set',
            CloseDate = Date.today(),
            Contact_Role_added__c = true,
            Billing_Term__c = 'Net 30'
        );
        insert testOpportunity1;

        // Test: No contract agreement, StageName = Closed Won
        testOpportunity1.StageName = 'Closed Won';
        Test.startTest();
        try {
            update testOpportunity1;
            System.assert(false, 'Expected DMLException but no exception was thrown.');
        } catch (DmlException e) {
            // Assert the error message for no contract agreement
            System.assertEquals('Closed Won Opportunity must have a contract agreement.', e.getMessage());
        }
        Test.stopTest();

        // Create an active contract agreement without verified signed IO
        APXT_Redlining__Contract_Agreement__c contract1 = new APXT_Redlining__Contract_Agreement__c(
            Opportunity__c = testOpportunity1.Id,
            APXT_Redlining__Status__c = 'Active',
            Verified_Signed_IO__c = false,
            X3rd_Party_Paper__c = true
        );
        insert contract1;

        // Test: Contract agreement with Active status, but Verified_Signed_IO__c = false, StageName = Closed Won
        testOpportunity1.StageName = 'Closed Won';
        Test.startTest();
        try {
            update testOpportunity1;
            System.assert(false, 'Expected DMLException but no exception was thrown.');
        } catch (DmlException e) {
            // Assert the error message for contract agreement without verified signed IO
            System.assertEquals('Third party paper must be approved by Legal, please submit a “Legal Case”.', e.getMessage());
        }
        Test.stopTest();

        // Update the Contract Agreement to meet the conditions
        contract1.Verified_Signed_IO__c = true;
        update contract1;

        // Test: Contract agreement with Active status and Verified_Signed_IO__c = true, StageName = Closed Won
        testOpportunity1.StageName = 'Closed Won';
        Test.startTest();
        update testOpportunity1;
        Test.stopTest();

        // Test: No contract agreement, StageName = Closed Lost
        testOpportunity1.StageName = 'Closed Lost';
        Test.startTest();
        try {
            update testOpportunity1;
            System.assert(false, 'Expected DMLException but no exception was thrown.');
        } catch (DmlException e) {
            // Assert the error message for no contract agreement
            System.assertEquals('Closed Won Opportunity must have a contract agreement.', e.getMessage());
        }
        Test.stopTest();

        // Create another opportunity with a different Billing Term and without contract agreement
        Opportunity testOpportunity2 = new Opportunity(
            Name = 'Test Opportunity2',
            StageName = 'Sales Meeting Set',
            CloseDate = Date.today(),
            Contact_Role_added__c = true,
            Billing_Term__c = 'Net 15'
        );
        insert testOpportunity2;

        // Test: No contract agreement, StageName = Closed Won
        testOpportunity2.StageName = 'Closed Won';
        Test.startTest();
        try {
            update testOpportunity2;
            System.assert(false, 'Expected DMLException but no exception was thrown.');
        } catch (DmlException e) {
            // Assert the error message for no contract agreement
            System.assertEquals('Closed Won Opportunity must have a contract agreement.', e.getMessage());
        }
        Test.stopTest();
    }
}


Code covering only 50%. Can someone help me how to achieve 100% code coverge
we have a requirement that when if a Conga transaction record has 'Sent' status , it should not allow the user to create a new conga transaction record. I created a trigger and apex handler class.

trigger congatransaction on APXT_CongaSign__Transaction__c (before INSERT) {
    
   congatransactionHandler.congatransactionMethod(Trigger.NewMap,Trigger.OldMap);
   
   }


public class congatransactionHandler {
    
    public static void congatransactionMethod(Map<Id,APXT_CongaSign__Transaction__c> newconMap, Map<Id,APXT_CongaSign__Transaction__c> oldconMap){
        Set<Id> conIds = New Set<Id>();
        List<APXT_CongaSign__Transaction__c> conList = New List<APXT_CongaSign__Transaction__c>();
        
        for(APXT_CongaSign__Transaction__c con : newconMap.values()){
               APXT_CongaSign__Transaction__c oldcontranmap = oldconMap.get(con.Id);
             if(oldconMap!=null && oldconMap.size()>0)
    // Trigger.new records are conveniently the "new" versions!
{        //Boolean oldcontransactionmap = oldcontranmap.APXT_CongaSign__Status__c.equals('SENT'); 
            if((newconMap.get(con.Id).Parent_a29__c== oldcontranmap.Parent_a29__c) && oldcontranmap.APXT_CongaSign__Status__c.equals('SENT')){


          con.adderror('You have already sent the conga transaction');
            }
        }
          
    }
}
}
Hi there,

I am getting this kind of warning when saving Lightning Flow (Spring'19 ver).
Is there any way to clear it?

This screen includes screen components that require Lightning runtime.

User-added image

Regards,
LinThaw

Hi,

I am trying to deploy a Change Set from my Sandbox to PROD Org however when I select to "Upload" the Change Set I receive the following error:
"This organization isn't authorized to upload change sets to other organizations. For authorization, contact the deployment connections administrators on the organizations where you want to upload changes.".

My profile is set up as a System Administrator for my Org.

Is anyone able to assist?

Thanks