You need to sign in to do that
Don't have an account?
Help on Trigger Unit Test
Hi,
Please help to create a unit test for my trigger.
I having a trouble deploying my trigger in productio since sfdc is requiring a unit test. please help me. sandbox do not require a unit test. please please, im clueless onhow to start the unite tes.
here's the code:
=====
trigger UpdateQuoteManagers on Quote (before insert, before update) {
Set<ID> idOwners = new Set<ID>();
for(Quote quotes:trigger.new)
{
idOwners.add(quotes.Account_Manager__c);
}
if (idOwners.size() > 0)
{
Map<Id,User> users= new Map<Id,User>([Select Territory_Head__c, District_Head__c from User where Id in : idOwners]);
for(Quote qu:trigger.new)
{
if (users.get(qu.Account_Manager__c).Id!= null)
{
qu.District_Head2__c = users.get(qu.Account_Manager__c).District_Head__c;
qu.Territory_Head2__c = users.get(qu.Account_Manager__c).Territory_Head__c;
}
else
{
qu.District_Head2__c = null;
qu.Territory_Head2__c = null;
}
}
}
}
Hi,
It is covered 81% about your trigger.
@isTest(SeeAllData = true)
public class TestUpdateQuoteManagersTRIGGER{
public static testMethod void test1(){
Opportunity opp = new Opportunity(Name='Test',CloseDate=Date.Today(),StageName='Closed Won');
insert opp;
user u = [select id,name,Territory_Head__c, District_Head__c from user where id =: userInfo.getUserID() limit 1];
quote q = new quote(Name='Test Quote',OpportunityID=opp.Id,AccountManager__c = u.id,Territory_Head2__c = 'Test Head', District_Head2__c = 'Test head2' );
insert q;
q.Territory_Head2__c = u.Territory_Head__c;
q.District_Head2__c = u.District_Head__c;
update q;
}
}
All Answers
Please Use folloing Code ....
@isTest
private class HelloWorldTestClass {
static testMethod void validateHelloWorld() {
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
Account acc=new Account();
acc.Name='Test Account';
insert acc;
Opportunity Opp=new Opportunity();
opp.Name='Test Opp';
opp.AccountId=acc.id;
opp.Stage='P';
opp.CloseDate=system.today();
insert opp;
//then after create Quote record===
quote qt=new quote()
qt.OpportunityId=opp.Id;
qt.Account_Manager__c=u.Id;
insert qt;
}
Should i create this as a class? thanks.
Hi,
It is covered 81% about your trigger.
@isTest(SeeAllData = true)
public class TestUpdateQuoteManagersTRIGGER{
public static testMethod void test1(){
Opportunity opp = new Opportunity(Name='Test',CloseDate=Date.Today(),StageName='Closed Won');
insert opp;
user u = [select id,name,Territory_Head__c, District_Head__c from user where id =: userInfo.getUserID() limit 1];
quote q = new quote(Name='Test Quote',OpportunityID=opp.Id,AccountManager__c = u.id,Territory_Head2__c = 'Test Head', District_Head2__c = 'Test head2' );
insert q;
q.Territory_Head2__c = u.Territory_Head__c;
q.District_Head2__c = u.District_Head__c;
update q;
}
}
thank you very much! this is perfect