• dhooft
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies

Hi Guys,

 

I wrote an 'after insert, after update' trigger on Opportunity, which updates a field(say, A) in Opportunity depending on the value of another field(say, B)  on Opportunity. Now when I convert a lead, the opportunity gets created but the value of A doesn't update, though the value of B is flowing from Lead itself ie B is populated but the trigger doesn't fire resulting in no population of A.

 

Could anyone of you help me overcome this?

 

Thanks 

  • July 09, 2009
  • Like
  • 0

I've bolded the lines Im trying to cover and my test conditions that are an attempt to cover those lines.

 

These are the lines coming up as not covered, I understand I need write test for these conditions, but cant figure out how to test these conditions:

 

Platform__c==null

pName.contains('All Verticals')==true

Log__c==null

 

 

Test Method:

 

@isTest private class alertControllerTestClass { static testMethod void myUnitTest() { // find test profile Profile careersPro = [select Id,Name from Profile where UserLicense.Name='Customer Portal Manager Standard' and Name like '%Careers' and (Name <> '%Real Estate' or Name <> '%Motors') limit 1]; system.debug('My profile name is... '+careersPro.Name); system.assertEquals('Customer Portal User - Careers', careersPro.Name); Profile allVertPro = [select Id,Name from Profile where UserLicense.Name='Customer Portal Manager Standard' and Name like '%All Verticals' limit 1]; system.debug('My profile name is... '+allVertPro.Name); system.assertEquals('Customer Portal User - All Verticals', allVertPro.Name); // setup set for user id of test user set<Id> testUserSet = new set<Id>(); // find test user User u01 = [select Id,Name,ProfileId,Profile.Name,FirstName, LastName from User where Profile.UserType='PowerCustomerSuccess' and IsActive=true limit 1]; system.debug('user name is... '+u01.Name); system.debug('user profile is... '+u01.Profile.Name); // add test user to set testUserSet.add(u01.Id); // change the test user for(User u02 : [select Id,Name,ProfileId,Profile.Name,FirstName, LastName from User where Id In :testUserSet]) { u02.FirstName='just updated'; u02.LastName='this user'; u02.ProfileId=careersPro.Id; update u02; testUserSet.add(u02.Id); } // find the test user again after changes User u03 = [select Id,Name,ProfileId,Profile.Name,FirstName, LastName from User where Id In :testUserSet]; system.debug('user name now is... '+u03.Name); system.debug('user profile now is... '+u03.Profile.Name); // initiate controller // start delete solutions List<Solution> deleteSolutions = new List<Solution>(); for(Solution soL03 :[select Id from Solution where IsDeleted=false]) { deleteSolutions.add(soL03); } delete deleteSolutions; // end delete solutions // check existing solutions again system.debug('now how many solutions are there: ' +[select count() from Solution]); system.assertEquals(0,[select count() from Solution]); // run class // when this is called once it pulls data from the Org alertController ac = new alertController(); // check if clean up test environment worked system.debug('profileAppropriateAlerts size: ' +ac.profileAppropriateAlerts.size()); system.assertEquals(0,ac.profileAppropriateAlerts.size()); // run as test user System.runAs(u03) { // run method to populate pName ac.getCurrentUserInfo(); // check current user system.debug('make sure test user is the current user: ' +u03.Name); system.assertEquals( UserInfo.getUserId(), u03.Id ); // test pName system.debug('test if pName condition is working'); system.assertEquals( [select Id,Profile.Name from User where Id = :UserInfo.getUserId() limit 1].Profile.Name , ac.pName ); } // end run as System.runAs(u03) { // run method to give solutions to users based on vertical ac.assignUserToVerticals(); system.assertEquals( [select Id from Solution where RecordType.DeveloperName='Messages_and_Alerts' and IsPublished=true and IsDeleted=false].size() , ac.profileAppropriateAlerts.size() ); } RecordType rt = [select Id from RecordType where DeveloperName='Messages_and_Alerts' and SobjectType='Solution']; // publish three solutions Solution alert01 = new Solution( SolutionName='test01', Approval_Status__c='Approved', RecordTypeId=rt.Id, IsPublished=true, Platform__c=null, Takedown__c=datetime.newInstance(2010, 1, 1), Posted__c=datetime.newInstance(2008, 1, 1), Log__c=null ); insert alert01; Solution alert02 = new Solution( SolutionName='test02', Approval_Status__c='Approved', RecordTypeId=rt.Id, IsPublished=true, Platform__c=null, Takedown__c=datetime.newInstance(2010, 1, 1), Posted__c=datetime.newInstance(2008, 1, 1), Log__c=null ); insert alert02; Solution alert03 = new Solution( SolutionName='test03', Approval_Status__c='Approved', RecordTypeId=rt.Id, IsPublished=true, Platform__c=null, Takedown__c=datetime.newInstance(2010, 1, 1), Posted__c=datetime.newInstance(2008, 1, 1), Log__c=null ); insert alert03; System.runAs(u03) { // run method to give solutions to users based on vertical ac.assignUserToVerticals(); system.assertEquals( [select Platform__c,Importance__c,Posted__c,Takedown__c, SolutionName,Portal_Version__c,Log__c,Lightbox_Popup__c,SolutionNote from Solution where RecordType.DeveloperName='Messages_and_Alerts' and IsPublished=true and IsDeleted=false and Platform__c=null and Posted__c<:ac.myDateTime and Takedown__c>:ac.myDateTime].size() , ac.profileAppropriateAlerts.size() ); system.assertEquals(3,ac.profileAppropriateAlerts.size()); for(Solution ss :ac.profileAppropriateAlerts){ system.assertEquals(null,ss.Platform__c); } } // start setup nonLoggedSolutions list List<Solution> nonLoggedSolutions = new List<Solution>(); for(Solution soL03 :[select Id,Log__c from Solution where RecordType.DeveloperName='Messages_and_Alerts' and IsPublished=true and IsDeleted=false and Platform__c=null and Posted__c<:ac.myDateTime and Takedown__c>:ac.myDateTime] ) { if(soL03.Log__c==null) { nonLoggedSolutions.add(soL03); } } // end setup nonLoggedSolutions list System.runAs(u03) { ac.putAlertsIntoLighBox(); system.debug('test showTheseInLightBoxList list'); system.assertEquals( nonLoggedSolutions.size() , ac.showTheseInLightBoxList.size() ); system.assertEquals(3,ac.showTheseInLightBoxList.size()); } // start change user profile User u04 = [select Id,ProfileId,Profile.Name from User where Id In :testUserSet]; u04.ProfileId=allVertPro.Id; update u04; User u05 = [select Id,ProfileId,Profile.Name from User where Id In :testUserSet]; system.assertEquals('Customer Portal User - All Verticals',u05.Profile.Name); // end change user profile // clear list ac.profileAppropriateAlerts.clear(); System.runAs(u05) { // change pName ac.getCurrentUserInfo(); // make sure pName is correct system.assertEquals('Customer Portal User - All Verticals',u05.Profile.Name); } // start setup a list for testing List<Solution> allVerticalSolutionsList = new List<Solution>(); for(Solution soL04 :[select Id,Posted__c,Takedown__c from Solution where RecordType.DeveloperName='Messages_and_Alerts' and IsPublished=true and IsDeleted=false] ) { if(ac.pName.contains('All Verticals')==true && soL04.Posted__c<ac.myDateTime && soL04.Takedown__c>ac.myDateTime) { allVerticalSolutionsList.add(soL04); } } // end setup a list for testing system.assertEquals( 3 , allVerticalSolutionsList.size() ); system.runAs(u05){ // reassign solutions to user ac.assignUserToVerticals(); system.assertEquals( allVerticalSolutionsList.size() , ac.profileAppropriateAlerts.size() ); } } // end my unit tests }

 

 

 

Class:

 

 

 

public class alertController { // start variables public string log { get; set;} {log='';} public string userId { get; set;} {userId='';} public string pName =''; public datetime myDateTime = datetime.now(); // map profiles to verticals public Map<String,String> userProfileToSolutionPlatfromMap = new Map<String,String>(); // alerts for profile mapping public List<Solution> profileAppropriateAlerts = new List<Solution>(); // alerts for time to live public List<Solution> timeAppropriateAlerts = new List<Solution>(); // send alerts to the right server public List<Solution> alwaysShowTheseInAlertsPageList = new List<Solution>(); // list of light box alerts public List<Solution> showTheseInLightBoxList = new List<Solution>(); // make the light box not visible by default public boolean lightBoxPopUp { get; set;} {lightBoxPopUp=false;} // hide the alerts on the dashboard by default public boolean alertRenderController { get; set;} {alertRenderController=false;} // end variables /** The next section is the constructor for the page... its all the scripts we will run **/ public alertController() { userId+=UserInfo.getUserId(); // get info about current user getCurrentUserInfo(); // start assign solutions to users assignUserToVerticals(); // lightbox alerts putAlertsIntoLighBox(); } /************* end constructor */ public void getCurrentUserInfo() { for(User u2 :[select Id,Profile.Name from User where Id = :UserInfo.getUserId() limit 1]) { pName = u2.Profile.Name; } } public void assignUserToVerticals() { for(Solution s : [select Platform__c,Importance__c,Posted__c,Takedown__c,SolutionName,Portal_Version__c,Log__c,Lightbox_Popup__c,SolutionNote from Solution where RecordType.DeveloperName='Messages_and_Alerts' and IsPublished=true and IsDeleted=false order by Ranking__C Desc nulls last,Importance__c Desc nulls last,Posted__c nulls last]) { // if there is no platform selected, give it to everyone if(s.Platform__c==null && s.Posted__c<myDateTime && s.Takedown__c>myDateTime) { profileAppropriateAlerts.add(s); } // if its the 'all vertical' profile... // give them everything else if(pName.contains('All Verticals')==true && s.Posted__c<myDateTime && s.Takedown__c>myDateTime) { profileAppropriateAlerts.add(s); } // no matter what give everything to the system admin else if(s.Platform__c!=null && pName.contains('System Administrator')==true && s.Posted__c<myDateTime && s.Takedown__c>myDateTime) { profileAppropriateAlerts.add(s); } // now match up the solution platform with the user profile else if(s.Platform__c!=null && pName.contains('Careers')==true && s.Platform__c.contains('Careers')==true && s.Posted__c<myDateTime && s.Takedown__c>myDateTime) { profileAppropriateAlerts.add(s); } else if(s.Platform__c!=null && pName.contains('Motors')==true && s.Platform__c.contains('Motors')==true && s.Posted__c<myDateTime && s.Takedown__c>myDateTime) { profileAppropriateAlerts.add(s); } else if(s.Platform__c!=null && pName.contains('Real Estate')==true && s.Platform__c.contains('Real Estate')==true && s.Posted__c<myDateTime && s.Takedown__c>myDateTime) { profileAppropriateAlerts.add(s); } } // end assign solutions to users } public void putAlertsIntoLighBox() { // start list for lightbox for(Solution s9 : profileAppropriateAlerts) { // if i dont account for a blank value here i get the // attempt to dereference a null value error if(s9.Log__c==null || (s9.Log__c!=null && s9.Log__c.contains(userId)==false)) { showTheseInLightBoxList.add(s9); } } // end list for lightbox } /** this next section has all of the list returns - start **/ // return always show alerts public List<Solution> getalwaysShowTheseInAlertsPageList() { return alwaysShowTheseInAlertsPageList; } // return light box alerts public List<Solution> getShowTheseInLightBoxList() { return showTheseInLightBoxList; } // return profile appropriate alerts public List<Solution> getProfileAppropriateAlerts() { return ProfileAppropriateAlerts; } // return time appropriate alerts public List<Solution> getTimeAppropriateAlerts() { return timeAppropriateAlerts; } /************* end */ /** this next method controls the visibility of the lightbox popup & page redirects - start **/ // start method for page run time actions public PageReference controlLightBoxVisibility() { if(profileAppropriateAlerts.size()==0) { PageReference homePage = new PageReference('/home/home.jsp'); homePage.setRedirect(true); return homePage; } if( // check if there are light box alerts for live showTheseInLightBoxList.size()<>0) // if list is not empty... { // render the HTML for them in the page lightBoxPopUp=true; } return null; } // end method for page run time actions /************* end */ /** this next method turns off the light box - start **/ // start method to disable thickbox messages on live public PageReference doNotShowAlertAgain() { for(Solution s5 :showTheseInLightBoxList) { if(s5.Log__c==null) { // add the Id for the current user to the log s5.Log__c=+UserInfo.getUserId(); } else { // if already have value put space in front s5.Log__c +=' '+UserInfo.getUserId(); } // update the list, future change to only update one alert... // not whole list update showTheseInLightBoxList; } return null; } // end method to disable thickbox messages on live /************* end */ }