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
Basil ChoudhryBasil Choudhry 

Test For a Trigger when moving into from Sandbox to Production

Hi,

I've just made our first trigger in the sandbox, it's working fine and it's quite simple. But when I try to deploy it I get various errors about test coverage. I understand that these are saying I need to test the code, but after some googling I'm not sure: do I need to add this test to the trigger itself, what does the test need to look like?

This is the trigger:

trigger ChangeOpportunityToProposalSent on Task (after insert) {

    ID OppId;
        Set<ID> setSTRIDs = new Set<ID>();
        for (Task tasks:Trigger.New){
          //if (tasks.What.Type== 'Opportunity')
        
           setSTRIDs.add(tasks.WhatId);
           Opportunity opp= [SELECT StageName FROM Opportunity WHERE ID IN:setSTRIDs];
           if(tasks.Subject=='RE: Your Tailored Proposal'){opp.StageName='Proposal Sent';}
           update opp;
       
         }

Any help would be greatly appreciated.
Best Answer chosen by Basil Choudhry
Vatsal KothariVatsal Kothari
Hi,

Your updated Trigger:

trigger ChangeOpportunityToProposalSent on Task (after insert) {

	Map<Id,Opportunity> oppList = new Map<Id,Opportunity>([SELECT Id,StageName FROM Opportunity]);

	for (Task tasks:Trigger.New){	
		if(tasks.Subject == 'RE: Your Tailored Proposal'){
			if(oppMap.containsKey(tasks.WhatId)){		
				oppMap.get(tasks.WhatId).StageName = 'Proposal Sent';
			}
		}
	}

	if(!oppMap.isEmpty()){
		update oppMap;
	}	
}
Test Class of trigger :

@isTest
public class TestOpportunityToProposalSent{

	static testMethod void testOpportunity(){
	
		Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
		User u = new User(Alias = 'standt', Email='standarduser321@testorg.com', 
                  EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                  LocaleSidKey='en_US', ProfileId = p.Id, 
                  TimeZoneSidKey='America/Los_Angeles', UserName='standarduser321@testorg.com');
      
        insert u;
	
		Opportunity opp = new Opportuniy();
		opp.Name = 'test';
		opp.Type = 'A Membership';
		opp.StageName = 'Needs Analysis';
		opp.CloseDate = date.today();
		insert opp;
		
		Task t = new Task();
		t.WhatId = opp.Id;
		t.Owner = u.Id;
		t.Subject = 'RE: Your Tailored Proposal';
		t.Status = 'Not Started';
		t.Priority = 'Normal';
		insert task;
		
	}
}

If this solves your problem, kindly mark it as the best answer.

Thnaks,
Vatsal

All Answers

Sonam_SFDCSonam_SFDC
Yes, if you are deploying this code from sandbox to production , it mandate to have more than 75% code coverage.

To write the test class for this above trigger, do the following:
1)Create an Opportunity 
2)Create a Task linked to the Opportunity created at 1 with subject ="RE: Your Tailored Proposal" (with What.Type= 'Opportunity)
3)use system.assert to check the Opportunity status converted to Proposal sent which should happen when this trigger is fired.

sample code:
http://teachmesalesforce.wordpress.com/2011/05/07/how-to-write-a-trigger-test/
https://developer.salesforce.com/blogs/engineering/2013/04/apex-test-code-segregation.html

Basil ChoudhryBasil Choudhry
Hi, Thanks for your reply. I found those two articles in my own searching, but they left me even more confused! Do I need to deploy the test class with the triggers at the same time? Thanks, Basil
Sonam_SFDCSonam_SFDC
Yes, because the test classes get executed at the time of deployment of the code you have written to check the % code coverage .
reference:http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_deploying_ant_deploy.htm

Hope this helps!

Vatsal KothariVatsal Kothari
Hi,

Your updated Trigger:

trigger ChangeOpportunityToProposalSent on Task (after insert) {

	Map<Id,Opportunity> oppList = new Map<Id,Opportunity>([SELECT Id,StageName FROM Opportunity]);

	for (Task tasks:Trigger.New){	
		if(tasks.Subject == 'RE: Your Tailored Proposal'){
			if(oppMap.containsKey(tasks.WhatId)){		
				oppMap.get(tasks.WhatId).StageName = 'Proposal Sent';
			}
		}
	}

	if(!oppMap.isEmpty()){
		update oppMap;
	}	
}
Test Class of trigger :

@isTest
public class TestOpportunityToProposalSent{

	static testMethod void testOpportunity(){
	
		Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
		User u = new User(Alias = 'standt', Email='standarduser321@testorg.com', 
                  EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                  LocaleSidKey='en_US', ProfileId = p.Id, 
                  TimeZoneSidKey='America/Los_Angeles', UserName='standarduser321@testorg.com');
      
        insert u;
	
		Opportunity opp = new Opportuniy();
		opp.Name = 'test';
		opp.Type = 'A Membership';
		opp.StageName = 'Needs Analysis';
		opp.CloseDate = date.today();
		insert opp;
		
		Task t = new Task();
		t.WhatId = opp.Id;
		t.Owner = u.Id;
		t.Subject = 'RE: Your Tailored Proposal';
		t.Status = 'Not Started';
		t.Priority = 'Normal';
		insert task;
		
	}
}

If this solves your problem, kindly mark it as the best answer.

Thnaks,
Vatsal

This was selected as the best answer