• Joshua Graham 13
  • NEWBIE
  • 10 Points
  • Member since 2014
  • SalesForce Adminstrator
  • Optimal Blue, LLC

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
What I am trying to do is look at the asset related list under the account object.  I want to sum up the Price field (MRR) for each asset that shows a status of "Active / Live".  If it sums up to <$1000 I want it to set a field called "Customer Type" to Bronze <1000, and if the sum of the assets with that Active / Live status is greater than $1000 but less than $2000, I want the Customer Type field to set to Silver....and so on.  I am really new to coding and am not sure how to proceed.  I was able to get the following code to count the number of assets and populate a custom field I made with that number.  I figured I could some how make this sum instead of count and then use if statements to update the Customer Type field based on the sum of that field.  Please advise what the best way of accomplishing this? 

trigger trgr_Customer_Tier_AutoUpdate on Account (before update) {

    Map <Id,integer> assetCount = new Map<Id,integer>();
   
    for(Account a:[SELECT Id,(select Id from Assets) from Account where Id IN:Trigger.New]){
   
        integer count = 0;
        for(Asset asset:a.Assets){
            count++;
        }
        assetCount.put(a.Id, count);
    }
   
    for(Account account:Trigger.New){
       
        account.Asset_Count__c=assetCount.get(account.Id);
        }
    }
“I am having problems with trigger test code coverage.  The following code is the original trigger:

trigger CreateImplementationCaseOnWin on Opportunity (before update) {

List<Case> CaseList = new List<Case> ();

for(Opportunity o: Trigger.new) {

String opptyId = o.AccountId;

Opportunity oldopp = trigger.OldMap.get (o.id);
       if (o.isWon != oldopp.isWon)

if(o.isWon == true && o.Type == 'New Business' && o.HasOpportunityLineItem == true && o.StageName == '990 - Closed/Won' ){


  Case c = new Case();
  c.AccountId = o.AccountId;
  c.OwnerId = o.OwnerId;
  c.Subject = o.Name;
  c.RecordTypeId = o.Case_Creation_RT__c;
  c.OB_Signed_Contract_Closed_Won__c = o.CloseDate;
  CaseList.add(c);


}

if(CaseList.size() > 0)
insert(CaseList);

} }

What I need to do is add another trigger with some tweaks to the if statement including removing o.iswon == true, changing type from 'New Business' to 'Existing Business' and changing the Stage from '90 - Closed/Won' to '08 - Contract Executed'.  So...

if(o.Type == 'Existing Business' && o.HasOpportunityLineItem == true && o.StageName == '08 - Contract Executed' )

However, when I do this and use the same test code from the original trigger above....the test code coverage drops dramatically. I try to make tweaks to that test code but it drops the coverage even further.  I am not sure what I am doing wrong.  Below is the original test code that covers 100% of the above code.  However, when I make the change to the if statement to the one in bold above, the coverage drops from 100% to 46%.  I tried adding the following code after insert oll in the original test code but it didn't seem to work.  I did have a question about if a custom field is set to required on the page layout when in StageName == '08 - Contract Executed, do i need to make sure those 'required' fields are in my test code?  I am very new to coding as you can probably tell!

oo.StageName= '08 - Contract Executed';
        oo.Type = 'Existing Business';

        update oo;
      
        Case c = new Case();
        c.AccountId = aa.Id;
        c.OwnerId = oo.OwnerId;
        c.Subject = oo.Name;
        c.RecordTypeId = oo.Case_Creation_RT__c;
        c.OB_Signed_Contract_Closed_Won__c = oo.CloseDate;
        insert c;


//original test code that covers 100% of the trigger at the top of this post

@isTest (SeeAllData=true)
private class Test_Class_CreateImplementationCaseOnWin {

    static testMethod void CreateImplementationCaseOnWin() {
 
        Account aa = new Account();
        aa.Name = 'Test Account';
        aa.Type = 'Customer';
        aa.BDM__c = 'Test User';
        aa.Time_Zone__c = 'Pacific';
        insert aa;
      
        Opportunity oo = new Opportunity();  
        oo.AccountId = aa.Id;
        oo.Name = 'test';
        oo.Amount = 1000;
        oo.StageName = 'Prospecting';
        oo.CloseDate = date.today();
        insert oo;
      
        //For production test class use 01u50000001F4l9
      
        OpportunityLineItem oll = new OpportunityLineItem();
        oll.OpportunityId = oo.Id;
        oll.Quantity = 1;
        oll.UnitPrice = 2.00;
        oll.PricebookEntryId = [SELECT Id FROM PricebookEntry WHERE isActive = true LIMIT 1].id;
      
        insert oll;
      
        oo.StageName= '90 - Closed/Won';
        update oo;
      
    //Verify that a case record was created
    for(Case cr : [SELECT Subject, Id FROM Case WHERE AccountId = :aa.Id LIMIT 1]){
    //System.assertEquals(cr.Subject = o.Name);
      
        }
      
        delete oll;
        delete oo;
      
      
      
    }
  
  
}”
Hi All - I am new to this forum and new to coding so I hope someone can help because I am racking my brains to figure out what I need to do to the test code to make it cover 75% or more. I modified a trigger that someone had previously written to add an if else statement and also code to run the case assignment rules which normally are not triggered with a trigger.  When testing the trigger it works well except for if the case isn't assigned to a person based on the assignment rules it is assigned to a queue.  I would rather it be assigned to the opportunity owner but I don't see that as an option in the assignment rules.  Is there a way to assign the cases that are created to the owner if it doesn't meet the assignment rules to assign to a different user?

I have the following trigger which will create a case and add some fields based on an if statement:

trigger CreateImplementationCaseOnWin on Opportunity (before update) {

AssignmentRule AR = new AssignmentRule();
AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;
List<Case> CaseList = new List<Case> ();

for(Opportunity o: Trigger.new) {
String opptyId = o.AccountId;
Opportunity oldopp = trigger.OldMap.get (o.id);
       if (o.isWon != oldopp.isWon)

if(o.isWon == true && o.Type == 'New Business' && o.HasOpportunityLineItem == true && o.StageName == '90 - Closed/Won' ){
  Case c = new Case();
  c.AccountId = o.AccountId;
  c.OwnerId = o.OwnerId;
  c.Subject = o.Name;
  c.RecordTypeId = o.Case_Creation_RT__c;
  c.Implementation_Type__c = o.Investor_Development_Required__c;
  c.OB_Signed_Contract_Closed_Won__c = o.CloseDate;
  c.setOptions(dmlOpts);
  CaseList.add(c);
}

else if(o.Type == 'Existing Business' && o.HasOpportunityLineItem == true && o.StageName == '08 - Contract Executed'){
  Case c = new Case();
  c.AccountId = o.AccountId;
  c.OwnerId = o.OwnerId;
  c.Subject = o.Name;
  c.RecordTypeId = o.Case_Creation_RT__c;
  c.Implementation_Type__c = o.Investor_Development_Required__c;
  c.OB_Signed_Contract_Closed_Won__c = o.CloseDate;
  c.setOptions(dmlOpts);
  CaseList.add(c);
}

if(CaseList.size() > 0)
insert(CaseList);
}
}

Before adding the else if statment and the Assignment Rule code, I had 100% coverage.  When adding the else if statment I go down to 62% and when adding the Assignment Rule code I am at 6% coverage.  I can't figure out what code I need to add to test and cover 75% or more.  Can someone please please point me in the right direction?  I have the test code below:

/**
* This class contains unit tests for validating the behavior of Apex classes
* and triggers.
*
* Unit tests are class methods that verify whether a particular piece
* of code is working properly. Unit test methods take no arguments,
* commit no data to the database, and are flagged with the testMethod
* keyword in the method definition.
*
* All test methods in an organization are executed whenever Apex code is deployed
* to a production organization to confirm correctness, ensure code
* coverage, and prevent regressions. All Apex classes are
* required to have at least 75% code coverage in order to be deployed
* to a production organization. In addition, all triggers must have some code coverage.
*
* The @isTest class annotation indicates this class only contains test
* methods. Classes defined with the @isTest annotation do not count against
* the organization size limit for all Apex scripts.
*
* See the Apex Language Reference for more information about Testing and Code Coverage.
*/
@isTest (SeeAllData=true)
private class Test_Class_CreateImplementationCaseOnWin {


    static testMethod void CreateImplementationCaseOnWin() {
  

               
        Account aa = new Account();
        aa.Name = 'Test Account';
        aa.Type = 'Customer';
        aa.BDM__c = 'Test User';
        aa.Time_Zone__c = 'Pacific';
        aa.Legacy_Solution__c = 'OB';
        insert aa;
       
       
        Opportunity oo = new Opportunity();   
        oo.AccountId = aa.Id;
        oo.Name = 'test';
        oo.Amount = 1000;
        oo.StageName = 'Prospecting';
        oo.CloseDate = date.today();
        insert oo;
       
                      
        //For production test class use 01u50000001F4l9
       
        OpportunityLineItem oll = new OpportunityLineItem();
        oll.OpportunityId = oo.Id;
        oll.Quantity = 1;
        oll.UnitPrice = 2.00;
        oll.PricebookEntryId = [SELECT Id FROM PricebookEntry WHERE isActive = true LIMIT 1].id;
       
        insert oll;
       
        oo.StageName= '90 - Closed/Won';
        update oo;
       
    //Verify that a case record was created
    for(Case cr : [SELECT Subject, Id FROM Case WHERE AccountId = :aa.Id LIMIT 1]){
    //System.assertEquals(cr.Subject = o.Name); 
       
        }
       
        delete oll;
        delete oo;
       
       
       
    }
   
   
}
What I am trying to do is look at the asset related list under the account object.  I want to sum up the Price field (MRR) for each asset that shows a status of "Active / Live".  If it sums up to <$1000 I want it to set a field called "Customer Type" to Bronze <1000, and if the sum of the assets with that Active / Live status is greater than $1000 but less than $2000, I want the Customer Type field to set to Silver....and so on.  I am really new to coding and am not sure how to proceed.  I was able to get the following code to count the number of assets and populate a custom field I made with that number.  I figured I could some how make this sum instead of count and then use if statements to update the Customer Type field based on the sum of that field.  Please advise what the best way of accomplishing this? 

trigger trgr_Customer_Tier_AutoUpdate on Account (before update) {

    Map <Id,integer> assetCount = new Map<Id,integer>();
   
    for(Account a:[SELECT Id,(select Id from Assets) from Account where Id IN:Trigger.New]){
   
        integer count = 0;
        for(Asset asset:a.Assets){
            count++;
        }
        assetCount.put(a.Id, count);
    }
   
    for(Account account:Trigger.New){
       
        account.Asset_Count__c=assetCount.get(account.Id);
        }
    }