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
Churchill19Churchill19 

Help write a test class for trigger

Hi all,

 

need your help...

 

i have the following trigger below,

 

trigger Sales_Manager_Trigger on BGB_Contract__c (before insert, before update) {

	Map <id,BGB_Contract__c> contracts = new Map <id, BGB_Contract__c>();

    for (BGB_Contract__c bgbContract : Trigger.new)
    {
		contracts.put(bgbContract.Renewal_Negotiators__c, bgbContract);
    }		   
	//Not to fire Trigger if user is equal to 'Sean Churchill'
	if (UserInfo.getName()<> 'Sean Churchill')

	{ 
		Map <Id, String> bseNameContractIdMap = new Map<Id, String>();
	   for(BGB_Sales_Employees__c bse: [select name from BGB_Sales_Employees__c where ID IN :contracts.keySet()]){
	   	bseNameContractIdMap.put(bse.name, bse.id);
	   }
	   
	   for(User u: [select ID from User where Name IN :bseNameContractIdMap.keyset()]){
	   	BGB_Contract__c contract = contracts.get(bseNameContractIdMap.get(u.name)); // this is the record that the triggered fired for, that matches your users name
	   	contract.sales_manager__c = u.id;
	   }
			
	}
}

 

 

 and i have written test code but cannot get it over the 75% test coverage but only able to get it to 70%... can any one help?

 

@isTest
private with sharing class Sales_Manager_Email_Test {

static testMethod void MyTestSalesManEma()
{
test.starttest(); 

    BGB_Contract__c testContractEmail = new BGB_Contract__c();
    
      
   
    testContractEmail.Account_Name__c = '0012000000SSw7l';
    testContractEmail.Feedback__c = 'Testing';
    testContractEmail.Renewal_Negotiators__c = 'a0H20000001oFrpEAE';
    testContractEmail.Contract_Spend__c = 110;   
    
    insert testContractEmail;
    
 	test.stoptest();  
    

}
}

 

 

I need to write some code to cover the code below as i not cover these lines: -

 

for(User u: [select ID from User where Name IN :bseNameContractIdMap.keyset()]){
	   	BGB_Contract__c contract = contracts.get(bseNameContractIdMap.get(u.name)); // this is the record that the triggered fired for, that matches your users name
	   	contract.sales_manager__c = u.id;

 Any help would be great...

 

Sean.

bob_buzzardbob_buzzard

This section:

 

 

		Map <Id, String> bseNameContractIdMap = new Map<Id, String>();
	   for(BGB_Sales_Employees__c bse: [select name from BGB_Sales_Employees__c where ID IN :contracts.keySet()]){
	   	bseNameContractIdMap.put(bse.name, bse.id);

 

looks a bit odd - you have a map of id to string, but you populate it with strings to ids.  Also, is bse.id even present - you aren't selecting it from the BGB_Sales_Employees__c.  I'm slightly surprised that it allows you to use bse.name as the key when putting data into the map.

 

 

 

 

 

justinpauldavis10justinpauldavis10

I have a similar question that I'm hoping someone will help me with, I'm new to Apex.


I have a very simple lookup-field trigger that places a record ID in that field, on a custom "Accounts" object, not the standard object:


trigger SetAccountField on Account__c (before insert, before update) {
    for (Account__c Account__c : Trigger.new) {
        a.Scoring__c = 'a00A0000003MnLw';
    }
 }



How can I create a test class, for something like this? Can you give me an example of the best way to test something this simple?