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
apex code shareapex code share 

Please help me writting a test class for trigger

 

 

/*****************************************************************************
Name     : Update Owner Manager on the opportunity from the User object

Purpose  : defaults the Owner Manager field of the opportunity from the User


******************************************************************************/
trigger UpdateOwnerManager on Opportunity (before insert, before update ){
  /**
  1. For each opportunity being inserted add User Id value in in Set of User Ids.
  2. Fetch all Users whose Id is in the Set.
  3. Add these fetched Users in a Map <User Id, User object>
  4. for each opportunity being inserted get User from the map and update the field values  
  **/  
  

 //holds User Ids
  Set<Id>setUserIds=new Set<Id>();
  
  //holds a list of Users
  List<User> listUser = new List<User>();
  
  //holds key value pairs of User Id and User Object
  Map<Id, User> mapUserObj = new Map<Id, User>();

  //holds User object
  User UserObj;
   
  //For each opportunity being inserted add User Id value in in Set of User Ids.
  for(Opportunity oppObj : Trigger.new){
    if(oppObj.OwnerId != null){
      setUserIds.add(oppObj.OwnerId);
    }
  }
  
  //Fetch all Users whose Id is in the Set.
  listUser = [Select u.Manager.Email, u.ManagerId From User u];
  if(listUser.size() == 0){
    return;  
  }
  
  //Add these fetched Users in a Map <User Id, User object>
  for(User usrObj : listUser){
    mapUserObj.put(usrObj.Id, usrObj);  
  }
  
  //for each opportunity being inserted get User from the map and update the field values
  for(Opportunity oppObj : Trigger.new){
    //get User object
    if(oppObj.OwnerId != null){
      if(mapUserObj.containsKey(oppObj.OwnerId)){
        UserObj = mapUserObj.get(oppObj.OwnerId);
        //map opportunity fields to User fields
        oppObj.Manager_of_record_owner__c = UserObj.Manager.Email;
      }  
    }
  }
}
Best Answer chosen by Admin (Salesforce Developers) 
Ispita_NavatarIspita_Navatar

Q1. http://boards.developerforce.com/t5/Apex-Code-Development/Insert-User-using-Apex/m-p/219195/highlight/true#M38752
Ans:
Hi,
I am posting the exact test method code for your trigger on opportunity.


@isTest
private class myUnitTest
{
public static testmethod void mytest()
{
Profile p = [select id from profile where name='System Administrator'];
User u1 = new User(alias = 'standt2', email='standarduser@test12.com',
emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,firstname='Heather',
timezonesidkey='America/Los_Angeles', username='standarduser@test12.com');
insert u1;

Opportunity opp = New Opportunity();
opp.name='abc';
opp.stagename='closed lost';
opp.closedate=system.today();
//Fill out your required Opportunity fields here and user the user id as u1.id whenever you needed.
insert opp;
}

}


Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

All Answers

Starz26Starz26

very basic example without testing system.assert after insert.

 

@isTest

private class myUnitTest{





static testmethod mytest(){



User u = [Select ID From User Where "put your criteria here"];



Opportunity opp = New Opportunity();



//Fill out your required Opportunity fields here



test.startTest();

insert opp;

test.stopTest


}

}

 

Ispita_NavatarIspita_Navatar

Q1. http://boards.developerforce.com/t5/Apex-Code-Development/Insert-User-using-Apex/m-p/219195/highlight/true#M38752
Ans:
Hi,
I am posting the exact test method code for your trigger on opportunity.


@isTest
private class myUnitTest
{
public static testmethod void mytest()
{
Profile p = [select id from profile where name='System Administrator'];
User u1 = new User(alias = 'standt2', email='standarduser@test12.com',
emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,firstname='Heather',
timezonesidkey='America/Los_Angeles', username='standarduser@test12.com');
insert u1;

Opportunity opp = New Opportunity();
opp.name='abc';
opp.stagename='closed lost';
opp.closedate=system.today();
//Fill out your required Opportunity fields here and user the user id as u1.id whenever you needed.
insert opp;
}

}


Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

This was selected as the best answer
apex code shareapex code share

thanks for the help ...i have successfully writ


Ispita_Navatar wrote:

Q1. http://boards.developerforce.com/t5/Apex-Code-Development/Insert-User-using-Apex/m-p/219195/highlight/true#M38752
Ans:
Hi,
I am posting the exact test method code for your trigger on opportunity.


@isTest
private class myUnitTest
{
public static testmethod void mytest()
{
Profile p = [select id from profile where name='System Administrator'];
User u1 = new User(alias = 'standt2', email='standarduser@test12.com',
emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,firstname='Heather',
timezonesidkey='America/Los_Angeles', username='standarduser@test12.com');
insert u1;

Opportunity opp = New Opportunity();
opp.name='abc';
opp.stagename='closed lost';
opp.closedate=system.today();
//Fill out your required Opportunity fields here and user the user id as u1.id whenever you needed.
insert opp;
}

}


Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 



ten the test class.