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
medemazamedemaza 

Please help My trigger code coverage below 75%

 

trigger OrgOnAccount on Organization_Structure__c (after insert, after update) {

 for(Organization_Structure__c Org:Trigger.New){
 
    Organization_Structure__c[] OSC = [Select Id,Sales_Organization_Des__c,
    Sales_Person_Name__c,AccountID__c From Organization_Structure__c Where Id=:Org.id];
    
    for(Organization_Structure__c O:OSC ){
    Account[] Acc =[Select Id,Chlor_Alkali_Domestic__c,Chlor_Alkali_Exports__c,
    Epoxy_Domestic__c,Epoxy_Exports__c,Peroxide_Domestic__c,Peroxide_Exports__c,
    Phosphates_Domestic__c,Phosphates_Exports__c,Sulphites_Domestic__c,Sulphites_Exports__c,
    Sale_Chlor_Alkali_Domestic__c,Sale_Chlor_Alkali_Exports__c,Sale_Epoxy_Domestic__c,
    Sale_Epoxy_Exports__c,Sale_Peroxide_Domestic__c,Sale_Peroxide_Exports__c,Sale_Phosphates_Domestic__c,
    Sale_Phosphates_Exports__c,Sale_Sulphites_Domestic__c,Sale_Sulphites_Exports__c 
    From Account Where Id=:O.AccountId__c];
    
        for(Account A:Acc){
        
        if(O.Sales_Organization_Des__c=='Epoxy Domestic'){
        A.Epoxy_Domestic__c = true;
        A.Sale_Epoxy_Domestic__c = O.Sales_Person_Name__c; 
        }
         if(O.Sales_Organization_Des__c=='Epoxy Exports'){
        A.Epoxy_Exports__c = true;
        A.Sale_Epoxy_Exports__c = O.Sales_Person_Name__c; 
        }        
        if(O.Sales_Organization_Des__c=='Chlor-Alkali Domestic'){
        A.Chlor_Alkali_Domestic__c = true;
        A.Sale_Chlor_Alkali_Domestic__c = O.Sales_Person_Name__c; 
        }
         if(O.Sales_Organization_Des__c=='Chlor-Alkali Exports'){
        A.Chlor_Alkali_Exports__c = true;
        A.Sale_Chlor_Alkali_Exports__c = O.Sales_Person_Name__c; 
        }
        if(O.Sales_Organization_Des__c=='Phosphates Domestic'){
        A.Phosphates_Domestic__c = true;
        A.Sale_Phosphates_Domestic__c = O.Sales_Person_Name__c; 
        }
         if(O.Sales_Organization_Des__c=='Phosphates Exports'){
        A.Phosphates_Exports__c = true;
        A.Sale_Phosphates_Exports__c = O.Sales_Person_Name__c; 
        }
        if(O.Sales_Organization_Des__c=='Peroxide Domestic'){
        A.Peroxide_Domestic__c = true;
        A.Sale_Peroxide_Domestic__c = O.Sales_Person_Name__c; 
        }
         if(O.Sales_Organization_Des__c=='Peroxide Exports'){
        A.Peroxide_Exports__c = true;
        A.Sale_Peroxide_Exports__c = O.Sales_Person_Name__c; 
        }
        if(O.Sales_Organization_Des__c=='Sulphites Domestic'){
        A.Sulphites_Domestic__c = true;
        A.Sale_Sulphites_Domestic__c = O.Sales_Person_Name__c; 
        }
         if(O.Sales_Organization_Des__c=='Sulphites Exports'){
        A.Sulphites_Exports__c = true;
        A.Sale_Sulphites_Exports__c = O.Sales_Person_Name__c; 
        }
        update Acc;
    }
    
    }
    
 }

}

 

Only 50% I want more :mansad:

 

 

 

@isTest
private class TestOrgOnAccount {

    static testMethod void myUnitTest() {
        Account obj = new Account();
        obj.name = 'test';
        obj.Country__c = 'Afghanistan ';
        obj.Zone__c = 'PZ';
        insert obj;
        
        Organization_Structure__c Org = new Organization_Structure__c();
            Org.Sales_Person_Name__c = 'test';
            Org.Sales_Organization_Des__c = 'Epoxy Domestic';
            Org.name = 'test';
         insert Org;
        
        if(Org.Sales_Organization_Des__c == 'Epoxy Domestic'){
        Account obj2 = new Account(Id=obj.id,name = 'test',Country__c = 'Afghanistan',
        Zone__c = 'PZ',Epoxy_Domestic__c = true,Sale_Epoxy_Domestic__c = Org.Sales_Person_Name__c);
        
        update obj2;
        }
    }
}

 

Thak for heolp

 

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

 

Well, look at the lines of code that *aren't* being tested by clicking on the "50%".   Once you know what code remains untested, you can devise new data that will test those areas of code.

 

For example, you're inserting an Org with the line:

            Org.Sales_Organization_Des__c = 'Epoxy Domestic';

But what about Peroxide Exports, or the other types... you aren't testing those.

 

Some additional thoughts. 

 

1. You have no comments in your code.  None.   Use comments.. If only to say at the highest level what you are trying to accomplish.  It costs you little, helps you clarify your code, and helps everyone who comes after you in understanding what you're doing.  It's basic.

 

2. This:

Organization_Structure__c[] OSC = [Select Id,Sales_Organization_Des__c,
    Sales_Person_Name__c,AccountID__c From Organization_Structure__c Where Id=:Org.id];

seems wierd... after all, you're in your loop through Organization_Structure records already... Org.id is only going to point to *one* record, and you already have that record loaded in Org.   If you do want to requery the Org's, fine, but do it all in one select statement:   .... where Id in :Trigger.new and then iterate through that instead of trigger.new.

 

3. You have a Select ... from Account statement within your For... Trigger.new loop.   This is very bad because if any significant number of Organization_Structure__c records are modified and call the trigger, Trigger.new will have many records in it and you will run into the governor limits on the number of Select statements you are using.  Same thing for the Updates.

Read about "Triggers and Bulk Requests" in the Apex documentation and rewrite your trigger.

 

4. Your test seems wierd to me.  Your trigger's goal seems to be "When an Organization Structure is inserted or changed, modify the related Account to reflect those changes."

This means that in testing, before you can insert or update your test Org, you first have to set up your Test Account(s) Then you can insert your Org.   You seem to be doing it the other way around.

 

5. Lastly, you are confusing the difference between "getting code coverage through testing", and "testing your code".  You aren't checking any of the results in your testing... you're not looking to see if the Account actually was updated.

The sequence should be:

1. create 200 Accounts with all those various fields set to null.

2. Insert the Accounts.

3. Create a List of 200 Organization objects with different values in various fields (perhaps since you have 10 distinct types, create the first 10 one at a time, and the next 190 in a loop.    and link them to the 200 accounts you've created (now that you have the Account Id's)

3. Insert the Organizations with one insert statement.   This should call your Trigger once, passing in 200 records.

4. Re-query the 200 Accounts

5. Use system.assert statements to check the values in the account and make sure they match the values in the related Org objects.

 

Unless you are checking the results, your code coverage may be 100%, but the test is meaningless.

 

 

6. Lastly, seems like you might be able to this without a Trigger and instead use Workflow rules and Field updates?

If so, that might be a better way to go.

 

This is intended to be helpful / educational, sorry if it comes off harsh.  Best, Steve.