/***************************************************************************** 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; } } } } |
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
very basic example without testing system.assert after insert.
Read this :
Testing Trigger : Structure of Test Class for Apex Trigger : http://forceschool.blogspot.com/2011/06/testing-trigger-structure.html
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.
thanks for the help ...i have successfully writ
ten the test class.