+ Start a Discussion
GangaGanga 

how to write a testclass for trigger please help me ....

Hi all
I am new to salesforce i just want to write atest class on the trigger  below is the code please help me .... urgent 


Thanks in Advance




trigger Updatactcontact on Opportunity (after update) {

    Set<Id> accountidset=new Set<Id>(); 
    Map<Id,Account> mapaccount=new Map<Id,Account>();
    Map<Id,List<Contact>> mapcontacts=new Map<Id,List<Contact>>();
    List<Contact> lstcontact=new List<Contact>();

   for(Opportunity oppobj:trigger.new){
      

if(trigger.newmap.get(oppobj.id).stagename!=trigger.oldmap.get(oppobj.id).stageName)

{
           if(oppobj.accountid!=null){
               accountidset.add(oppobj.accountid);
           }             
       }  
   }
   if(accountidset!=null && accountidset.size()>0){
       for(Account actobj:[select id,name,description,(select

id,name,OpportunityUpdate__c from Contacts) from Account where id in:accountidset]){
           mapaccount.put(actobj.id,actobj);
           mapcontacts.put(actobj.id,actobj.contacts);     
       }  
   }
   for(Opportunity oppobj:trigger.new){
      

if(trigger.newmap.get(oppobj.id).stagename!=trigger.oldmap.get(oppobj.id).stageName)
{
              if(oppobj.AccountiD!=null)
              {
mapaccount.get(oppobj.AccountId).Description='Opportunity :'+oppobj.Name+'stagename changed from'+trigger.oldmap.get(oppobj.id).stageName+'to '+ trigger.newmap.get(oppobj.id).stageName;
                   for(Contact cont:mapcontacts.get(oppobj.AccountId)){
                      Contact contobj=new

Contact(id=Cont.id,OpportunityUpdate__c='Opportunity :'+oppobj.Name+' stagename changed from '+trigger.oldmap.get(oppobj.id).stageName+' to '+

trigger.newmap.get(oppobj.id).stageName);
                      lstcontact.add(contobj);
                  }           
              }
       }  
   }
  
   if(lstcontact!=null && lstcontact.size()>0)

           update lstcontact;
          
   if(mapaccount!=null && mapaccount.size()>0)
           update mapaccount.values();
}

Best Answer chosen by Ganga
CheyneCheyne
If I'm not mistaken, your trigger is attempting to write "Opportunity stagename changed from <old stagename>  to <new stagename>" to  the description on the account and all contacts related to the account whenever an opportunity stagename changes. The basic idea of a unit test is to set up some test data, perform operations on it so that your trigger runs, and then check that you got the desired results, using system.assert methods. Try something like this (keep in mind that if you have custom validation rules or required fields set up in your org, you may need to tweak the data a bit to respect those rules): 

@isTest
private class TestUpdateContact {
  static testMethod void testUpdateContact() {
    Account acct = new Account(Name='Test');
    insert acct;
    
    Contact c = new Contact(LastName='Test');
    insert c;
    
    Opportunity opp = new Opportunity(
             Name='Test Opp',
             AccountId=acct.Id,
             StageName='Prospecting',
             CloseDate=Date.Today()
    );
    insert opp;

    //Now, we can update the opp with a new stage name
    opp.StageName = 'Closed Won';
    update opp;
   
    //Here, we'll test the account and contact description values were updated correctly
    //Note that we have to query for the account and contact to get the updated values
    String checkString = 'Opportunity stagename changed from Prospecting to Closed Won';
    system.assertEquals(checkString, [SELECT Description FROM Account WHERE Id = :acct.Id].Description);
    system.assertEquals(checkString, [SELECT Description FROM Contact WHERE Id = :c.Id].Description);
  }
}

All Answers

CheyneCheyne
If I'm not mistaken, your trigger is attempting to write "Opportunity stagename changed from <old stagename>  to <new stagename>" to  the description on the account and all contacts related to the account whenever an opportunity stagename changes. The basic idea of a unit test is to set up some test data, perform operations on it so that your trigger runs, and then check that you got the desired results, using system.assert methods. Try something like this (keep in mind that if you have custom validation rules or required fields set up in your org, you may need to tweak the data a bit to respect those rules): 

@isTest
private class TestUpdateContact {
  static testMethod void testUpdateContact() {
    Account acct = new Account(Name='Test');
    insert acct;
    
    Contact c = new Contact(LastName='Test');
    insert c;
    
    Opportunity opp = new Opportunity(
             Name='Test Opp',
             AccountId=acct.Id,
             StageName='Prospecting',
             CloseDate=Date.Today()
    );
    insert opp;

    //Now, we can update the opp with a new stage name
    opp.StageName = 'Closed Won';
    update opp;
   
    //Here, we'll test the account and contact description values were updated correctly
    //Note that we have to query for the account and contact to get the updated values
    String checkString = 'Opportunity stagename changed from Prospecting to Closed Won';
    system.assertEquals(checkString, [SELECT Description FROM Account WHERE Id = :acct.Id].Description);
    system.assertEquals(checkString, [SELECT Description FROM Contact WHERE Id = :c.Id].Description);
  }
}
This was selected as the best answer
GangaGanga
Thank You So much  chyne .... given complete code helped me alot .......