0

I have the below trigger to update certain records when a form record changes status to submitted. It simply checks the status of the form, makes the updates on the other records and then commits the updates.

Trigger:

trigger SubmitForm on disco__form__c (after update) {

    List<Citrus_Inspections__c> citUpdate = new List<Citrus_Inspections__c>();
    List<Grape_Inspection__c> grapeUpdate = new List<Grape_Inspection__c>();

    for ( disco__form__c form : trigger.new){


        if (form.disco__status__c == 'SUBMITTED'){

            if(form.Citrus_Inspection__c != null){

                Citrus_Inspections__c cit = new Citrus_Inspections__c(Id =form.Citrus_Inspection__c, Status__c = 'Complete' );
                citUpdate.add(cit);
            }

            if(form.Grape_Inspection__c != null){

                Grape_Inspection__c grape = new Grape_Inspection__c(Id =form.Grape_Inspection__c, Status__c = 'Complete' );
                grapeUpdate.add(grape);
            }

        }
    }

    update citUpdate;
    update grapeUpdate;
    

}

I have it tested by the following test class. The class contains a test setup method to create many pieces of data to be used by all methods in the test class. The creation of the commodity records and execution of the update inspection records method will trigger a flow to create the grape and citrus inspection records. All other records needed are created in the makeData setup method. The setup method and test method are shown below:

Setup:

@TestSetup
static void makeData(){


    //Create Citrus and Grape Form Templates (need to keep up to date with the latest template version)
    List<disco__form_template__c> formTempList = new List<disco__form_template__c>();

    disco__form_template__c clemFormTemp = new disco__form_template__c(Name = 'Clementine Inspection v1', disco__Status__c='In Use');
    disco__form_template__c oranFormTemp = new disco__form_template__c(Name = 'Orange Inspection v1', disco__Status__c='In Use');
    disco__form_template__c lemFormTemp = new disco__form_template__c(Name = 'Lemon Inspection v1', disco__Status__c='In Use');
    disco__form_template__c tanFormTemp = new disco__form_template__c(Name = 'Tangelo Inspection v1', disco__Status__c='In Use');
    disco__form_template__c coloredGrapeFormTemp = new disco__form_template__c(Name = 'Grape Inspection - Colored Berries v1', disco__Status__c='In Use');
    disco__form_template__c whiteGrapeFormTemp = new disco__form_template__c(Name = 'Grape Inspection - White Berries v1', disco__Status__c='In Use');

    //Add form temps to one list and perform DML insert
    formTempList.add(clemFormTemp);
    formTempList.add(oranFormTemp);
    formTempList.add(lemFormTemp);
    formTempList.add(tanFormTemp);
    formTempList.add(coloredGrapeFormTemp);
    formTempList.add(whiteGrapeFormTemp);

    insert formTempList;

    //Create Varieties for Grapes so forms can be created and to test varieties sync

    Variety__c varWhite = (Variety__c)TestDataFactory.createSObject('Variety__c', new Map<String,Object>{
        'Name' => 'Test_Variety2',
        'Size__c' => 'Test_Size2',
        'PLU__c' => '1234567890',
        'Berry_Color_Type__c' => 'White'
    });
    Variety__c varColored = (Variety__c)TestDataFactory.createSObject('Variety__c', new Map<String,Object>{
        'Name' => 'Test_Variety3',
        'Size__c' => 'Test_Size3',
        'PLU__c' => '12345678901',
        'Berry_Color_Type__c' => 'Colored'
    });

    //Create Client and inspection request
    Account client = (Account)TestDataFactory.createSObject('Account');

    Inspection_Request__c insReq = (Inspection_Request__c)TestDataFactory.createSObject('Inspection_Request__c', new Map<String, Object>{
        'Client__c' => client.id
    });

    //Create all necessary commodities (citrus and grapes)

    List<Commodity__c> commListNewVar = TestDataFactory.createSObjectList('Commodity__c',new Map<String,Object>{
        'Imported_Variety__c' => 'Test_Variety4',
        'Size__c' => 'Test_Size4',
        'Inspection_Request__c' => insReq.Id,
        'Inspect__c' => TRUE
      },3);

    List<Commodity__c> commListWhite = TestDataFactory.createSObjectList('Commodity__c',new Map<String,Object>{
        'Imported_Variety__c' => 'Test_Variety2',
        'Size__c' => 'Test_Size2',
        'Inspection_Request__c' => insReq.Id,
        'Inspect__c' => TRUE,
        'Species__c' => 'Grape'
      },3);

    List<Commodity__c> commListColored = TestDataFactory.createSObjectList('Commodity__c',new Map<String,Object>{
        'Imported_Variety__c' => 'Test_Variety3',
        'Size__c' => 'Test_Size3',
        'Inspection_Request__c' => insReq.Id,
        'Inspect__c' => TRUE,
        'Species__c' => 'Grape'
    },3);



    List<Commodity__c> commListClem = TestDataFactory.createSObjectList('Commodity__c',new Map<String,Object>{
        'Inspection_Request__c' => insReq.Id,
        'Inspect__c' => TRUE,
        'Species__c' => 'Clementine'
      },3);

    List<Commodity__c> commListOran = TestDataFactory.createSObjectList('Commodity__c',new Map<String,Object>{
        'Inspection_Request__c' => insReq.Id,
        'Inspect__c' => TRUE,
        'Species__c' => 'Orange'
    },3);

    List<Commodity__c> commListLem = TestDataFactory.createSObjectList('Commodity__c',new Map<String,Object>{
        'Inspection_Request__c' => insReq.Id,
        'Inspect__c' => TRUE,
        'Species__c' => 'Lemon'
    },3);

    List<Commodity__c> commListTan = TestDataFactory.createSObjectList('Commodity__c',new Map<String,Object>{
        'Inspection_Request__c' => insReq.Id,
        'Inspect__c' => TRUE,
        'Species__c' => 'Tangelo'
    },3);

    List<Commodity__c> commListNoSize = TestDataFactory.createSObjectList('Commodity__c',new Map<String,Object>{
        'Imported_Variety__c' => 'Test_Variety1',
        'Size__c' => '',
        'Inspection_Request__c' => insReq.Id,
        'Inspect__c' => TRUE
    },3);
    
    
    //Run Sync Varieties and inspection update logic
    //Sync class takes a list of lists with inspection requests. setting up parameter to pass in
    List<Inspection_Request__c> insList = new List<Inspection_request__c>();
    insList.add(insReq);
    List<List<Inspection_request__c>> listList = new List<List<Inspection_request__c>>();
    listList.add(insList);

    
    

    //Sync the commodity varieties and then update the inspection records with the correct PLUs/Record Types
    SyncVarieties.AddUpdateVarieties(listList);

    Test.startTest();

    UpdateInspectionRecords.SortCommoditiesUpdateInspection (listList);

    Test.stopTest();
}

Test method:

@isTest private static void testSubmitForm() {

    //Get Inspection Request Created
    List<Inspection_Request__c> insList = [Select Id from Inspection_Request__c];

    //Get the form records to change to submitted
    List<disco__form__c> forms = [select id ,disco__save_complete__c from disco__form__c where Commodity__r.Inspection_Request__c IN  :insList];

    //Check boolean field to change status to submitted
    for(disco__form__c form : forms){

        form.disco__save_complete__c = TRUE;
    }

    update forms;

    //Assert Submit Form Trigger

    //Get updated citrus and grape inspection records
    List<Citrus_Inspections__c> updatedCit = [Select Id, status__c from Citrus_Inspections__c where Commodity__r.Inspection_Request__c IN :insList];
    List<Grape_Inspection__c> updatedGrape = [Select Id, status__c from Grape_Inspection__c where Commodity__r.Inspection_Request__c IN :insList];

    //Assert the trigger changed the status from incomplete to complete
    for(Citrus_Inspections__c citUpd : updatedCit){

        System.AssertEquals(citUpd.Status__c, 'Complete');
    }
    for(Grape_Inspection__c grapeUpd : updatedGrape){

        System.AssertEquals(grapeUpd.Status__c, 'Complete');
    }
}

When I run this test in a QA Sandbox, it functions perfectly fine and I get 100% coverage on the trigger. However, when I deploy the trigger with the test class and the rest of my metadata (all objects/fields/flows referenced in the trigger/test class) I get 0% coverage:

Success in QA Sandbox with 100% Coverage: Success in QA

Failure on deployment to Prod with 0% coverage: Failure on deployment to Prod

Like I said earlier, I have all objects and fields referenced in the trigger and test class available in production (they are also a part of the same deployment). I am deploying using sfdx force:source:deploy. Is there a reason that this deployment keeps failing?

  • It is likely that either the query in line of testSubmitRecord is not returning any record. Are you including the test class while deploying? – manjit5190 Sep 14 at 20:56
  • Yeah I am including the test class while deploying but for some reason in the test execution history after the deployment to production, it doesn't show the new test class I added to cover this trigger. I am not sure if this is just because it failed or if it just did not run it at all. – Willis Zawisa Sep 14 at 21:03
1

Most possibly your Trigger "SubmitForm" is not executed. The following piece of code is likely not returning the records:

//Get the form records to change to submitted
List<disco__form__c> forms = [select id ,disco__save_complete__c from disco__form__c where Commodity__r.Inspection_Request__c IN  :insList];
| improve this answer | |
  • Is there a way to confirm this within SFDX deploy? Am I able to see the debut logs from the test run? I checked the sfdx log and all it says is deploy failed. – Willis Zawisa Sep 14 at 16:27
1

@Willis Zawisa maybe there is any validation rule thing is active in your Production, that may be the reason for your that test coverage is 0% in Production and that scenario is failing in your test class.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.