• ssr
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 18
    Replies
Hi tell me the good example how to write test classes
  • March 27, 2012
  • Like
  • 0

I have 3 survey questions that are ranked 1-5.  I'm trying to create a percentage formula field that will give me the overall percentage of the 3 questions.  

 

survey_question1__c = 4

survey_question2__c = 5

survey_question3__c = 5

 

Any help would be greatly appreciated.

Field Name: Date Sent 
API name: Date_Sent__c

Field Name: TAT
API name: TAT__c

Objective - Automatically add 15 days to "TAT" field from input made in the "Date Sent" field. 

 

"TAT" field, will be a read only field but "Date Sent"will be a "Date" Data Type.

 

Need a VR that will add 15 days from date input to be made from the "Date Sent" field and autopopulate the "TAT" field.

 

Please see screenshot for example: http://cl.ly/I9Ei

 

Thank you!

 

 

 

 

 

 

Hi There,

 

I am trying to build a validation rule which prevents sales people from closing opportunities with the close date in the future. Especially when the opportunity is closed lost, they quite often don't change the close date and just leave the expected close date, which can be months ahead.


Any suggestions? I already use the close date cannot be prior the current month, maybe this could be altered ?

 

AND(OR (ISNEW(),ISCHANGED( CloseDate )),
CloseDate < DATE( YEAR(TODAY()), MONTH(TODAY()), 1) )

 

Thanks,

Jenni

Hello,

 

I have a custom wizard controller that is working and am trying to write a test case for it. I have a button that writes three parameters to querystring in the url.

 

Controller

public class osmController {

   // These member variables maintain the state of the wizard. 
   // When users enter data into the wizard, their input is stored 
   // in these variables.  
    
   Participant_Assessment__c pAsmt;
   Participant_Interaction__c pInt;
   Participant_Answer__c pAns1;
 ...
   private final Participant__c participant;
   private final Assessment__c assessment;
   private final Family__c family;
   string possibleanswer1 = null;
 ...

   // The next methods return one of each of the member 
   // variables. If this is the first time the method is called, 
   // it creates an empty record for the variable. 
      
   public List<selectOption> getanswer1() {
        List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options       
        for (Possible_Answer__c pa1 : [SELECT Id, Answer_Display__c FROM Possible_Answer__c
         WHERE Dimension__c = 'Income']) { 
            options.add(new selectOption(pa1.Id, pa1.Answer_Display__c)); //for all records found - add them to the picklist options
        }
        return options; //return the picklist options
 ...
   public List<selectOption> getInteractiontypes() {
        List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options        
        options.add(new selectOption('', '--None--')); //add the first option of '- None -' in case the user doesn't want to select a value or in case no values are returned from query below
        for (Interaction_Type__c Interactiontypes : [SELECT Id, Description__c FROM Interaction_Type__c WHERE IsDeleted = FALSE]) { 
            options.add(new selectOption(Interactiontypes.Description__c, Interactiontypes.Description__c)); //for all records found - add them to the picklist options
        }
        return options; //return the picklist options
    }
   
   public Participant_Assessment__c getpAsmt() {
      if(pAsmt == null) pAsmt = new Participant_Assessment__c();
      return pAsmt;
   }
   
   public Participant_Interaction__c getpInt() {
      if(pInt == null) pInt = new Participant_Interaction__c();
      return pInt;
   }
   
   public Participant_Answer__c  getpAns1() {
      if(pAns1 == null) pAns1 = new Participant_Answer__c();
      return pAns1;
   }
   public Participant_Answer__c  getpAns2() {
      if(pAns2 == null) pAns2 = new Participant_Answer__c();
      return pAns2;
   }
   public Participant_Answer__c  getpAns3() {
      if(pAns3 == null) pAns3 = new Participant_Answer__c();
      return pAns3;
   }
   public Participant_Answer__c  getpAns4() {
      if(pAns4 == null) pAns4 = new Participant_Answer__c();
      return pAns4;
   }
   public Participant_Answer__c  getpAns5() {
      if(pAns5 == null) pAns5 = new Participant_Answer__c();
      return pAns5;
   }
   public Participant_Answer__c  getpAns6() {
      if(pAns6 == null) pAns6 = new Participant_Answer__c();
      return pAns6;
   }
   public Participant_Answer__c  getpAns7() {
      if(pAns7 == null) pAns7 = new Participant_Answer__c();
      return pAns7;
   }
   public Participant_Answer__c  getpAns8() {
      if(pAns8 == null) pAns8 = new Participant_Answer__c();
      return pAns8;
   }
   public Participant_Answer__c  getpAns9() {
      if(pAns9 == null) pAns9 = new Participant_Answer__c();
      return pAns9;
   }
   public Participant_Answer__c  getpAns10() {
      if(pAns10 == null) pAns10 = new Participant_Answer__c();
      return pAns10;
   }
   public Participant_Answer__c  getpAns11() {
      if(pAns11 == null) pAns11 = new Participant_Answer__c();
      return pAns11;
   }
   public Participant_Answer__c  getpAns12() {
      if(pAns12 == null) pAns12 = new Participant_Answer__c();
      return pAns12;
   }
   public Participant_Answer__c  getpAns13() {
      if(pAns13 == null) pAns13 = new Participant_Answer__c();
      return pAns13;
   }
   public Participant_Answer__c  getpAns14() {
      if(pAns14 == null) pAns14 = new Participant_Answer__c();
      return pAns14;
   }
   public Participant_Answer__c  getpAns15() {
      if(pAns15 == null) pAns15 = new Participant_Answer__c();
      return pAns15;
   }
   public Participant_Answer__c  getpAns16() {
      if(pAns16 == null) pAns16 = new Participant_Answer__c();
      return pAns16;
   }
   public Participant_Answer__c  getpAns17() {
      if(pAns17 == null) pAns17 = new Participant_Answer__c();
      return pAns17;
   }
   public Participant_Answer__c  getpAns18() {
      if(pAns18 == null) pAns18 = new Participant_Answer__c();
      return pAns18;
   }
   
   public Participant__c getParticipant() {
            return participant;
   }
   
   public Assessment__c getAssessment() {
            return assessment;
   } 
   
   public Family__c getFamily() {
            return family;
   }   
   
   public osmController() {

            if (ApexPages.currentPage().getParameters().get('family') == '') {
            	//do nothing
            } else {
            	participant = [select id, name from Participant__c where id =  <================GETTING ERROR HERE
                       :ApexPages.currentPage().getParameters().get('id')];
            }
            assessment = [select id, name from Assessment__c where Name = 
                       :ApexPages.currentPage().getParameters().get('asmt') AND
                       Version__c = :ApexPages.currentPage().getParameters().get('version')];
                       
            if (ApexPages.currentPage().getParameters().get('family') == '') {
            	//do nothing
            } else {
            	family = [select id, name, Poverty_Level_2012__c from Family__c where name = 
                                         :ApexPages.currentPage().getParameters().get('family')];
            }              
   }
    
   public String getpossibleanswer1() {
            return possibleanswer1;
   }   
   public String getpossibleanswer2() {
            return possibleanswer2;
   }
   public String getpossibleanswer3() {
            return possibleanswer3;
   }   
   public String getpossibleanswer4() {
            return possibleanswer4;
   }   
   public String getpossibleanswer5() {
            return possibleanswer5;
   }   
   public String getpossibleanswer6() {
            return possibleanswer6;
   }   
   public String getpossibleanswer7() {
            return possibleanswer7;
   }   
   public String getpossibleanswer8() {
            return possibleanswer8;
   }   
   public String getpossibleanswer9() {
            return possibleanswer9;
   }   
   public String getpossibleanswer10() {
            return possibleanswer10;
   }   
   public String getpossibleanswer11() {
            return possibleanswer11;
   }   
   public String getpossibleanswer12() {
            return possibleanswer12;
   }   
   public String getpossibleanswer13() {
            return possibleanswer13;
   }   
   public String getpossibleanswer14() {
            return possibleanswer14;
   }   
   public String getpossibleanswer15() {
            return possibleanswer15;
   }   
   public String getpossibleanswer16() {
            return possibleanswer16;
   }   
   public String getpossibleanswer17() {
            return possibleanswer17;
   }   
   public String getpossibleanswer18() {
            return possibleanswer18;
   }      
   
   public void setpossibleanswer1(String possibleanswer1) { this.possibleanswer1 = possibleanswer1; }
   public void setpossibleanswer2(String possibleanswer2) { this.possibleanswer2 = possibleanswer2; }
   public void setpossibleanswer3(String possibleanswer3) { this.possibleanswer3 = possibleanswer3; }
   public void setpossibleanswer4(String possibleanswer4) { this.possibleanswer4 = possibleanswer4; }
   public void setpossibleanswer5(String possibleanswer5) { this.possibleanswer5 = possibleanswer5; }
   public void setpossibleanswer6(String possibleanswer6) { this.possibleanswer6 = possibleanswer6; }
   public void setpossibleanswer7(String possibleanswer7) { this.possibleanswer7 = possibleanswer7; }
   public void setpossibleanswer8(String possibleanswer8) { this.possibleanswer8 = possibleanswer8; }
   public void setpossibleanswer9(String possibleanswer9) { this.possibleanswer9 = possibleanswer9; }
   public void setpossibleanswer10(String possibleanswer10) { this.possibleanswer10 = possibleanswer10; }
   public void setpossibleanswer11(String possibleanswer11) { this.possibleanswer11 = possibleanswer11; }
   public void setpossibleanswer12(String possibleanswer12) { this.possibleanswer12 = possibleanswer12; }
   public void setpossibleanswer13(String possibleanswer13) { this.possibleanswer13 = possibleanswer13; }
   public void setpossibleanswer14(String possibleanswer14) { this.possibleanswer14 = possibleanswer14; }
   public void setpossibleanswer15(String possibleanswer15) { this.possibleanswer15 = possibleanswer15; }
   public void setpossibleanswer16(String possibleanswer16) { this.possibleanswer16 = possibleanswer16; }
   public void setpossibleanswer17(String possibleanswer17) { this.possibleanswer17 = possibleanswer17; }
   public void setpossibleanswer18(String possibleanswer18) { this.possibleanswer18 = possibleanswer18; }
   


   // The next methods control navigation through 
   // the wizard. Each returns a PageReference for one of the pages 
   // in the wizard. Note that the redirect attribute does not need to 
   // be set on the PageReference because the URL does not need to change 
   // when users move from page to page. 
    
   public PageReference step1() {
      return Page.osmStep1;
   }
   public PageReference step2() {
      return Page.osmStep2;
   }
   public PageReference step3() {
      return Page.osmStep3;
   }
   public PageReference step4() {
      return Page.osmStep4;
   }
   public PageReference step5() {
      return Page.osmStep5;
   }
   public PageReference step6() {
      return Page.osmStep6;
   }
   public PageReference step7() {
      return Page.osmStep7;
   }
   public PageReference step8() {
      return Page.osmStep8;
   }
   public PageReference step9() {
      return Page.osmStep9;
   }
   
   // This method cancels the wizard, and returns the user to the  
   // Participants tab 
    
    public PageReference cancel() {
            PageReference participantPage = new ApexPages.StandardController(participant).view();
            participantPage.setRedirect(true);
            return participantPage; 
    }
    
   // This method performs the final save for all objects, and 
   // then navigates the user to the detail page for the new 
   // participant assessment. 
    
   public PageReference save() {
          PageReference p = null;
          
          if (this.family == null || this.participant == null || this.assessment == null) {
          	p = Page.failure;
          	p.getParameters().put('error','noParam');
          } else {
          	try {
          		// Create the participant assessment. Before inserting, use the id field 
          // from the active participant and assessment that are passed via custom button to create 
          // the relationship between the participant and assessment and the participant assessment. 
        
          pAsmt.Participant__c = participant.id;
          pAsmt.Assessment__c = assessment.id;
          insert pAsmt;
    
          // Create the participant answers. Before inserting, use the id field 
          // that's created once the participant assessment is inserted to create 
          // the relationship between the participant assessment and the participant answer. 
          
          pAns1.Participant_Assessment__c = pAsmt.id;
          pAns1.Possible_Answer__c = possibleanswer1;
          insert pAns1;
          pAns2.Participant_Assessment__c = pAsmt.id;
          pAns2.Possible_Answer__c = possibleanswer2;
          insert pAns2;
          pAns3.Participant_Assessment__c = pAsmt.id;
          pAns3.Possible_Answer__c = possibleanswer3;
          insert pAns3;
          pAns4.Participant_Assessment__c = pAsmt.id;
          pAns4.Possible_Answer__c = possibleanswer4;
          insert pAns4;
          pAns5.Participant_Assessment__c = pAsmt.id;
          pAns5.Possible_Answer__c = possibleanswer5;
          insert pAns5;
          pAns6.Participant_Assessment__c = pAsmt.id;
          pAns6.Possible_Answer__c = possibleanswer6;
          insert pAns6;
          pAns7.Participant_Assessment__c = pAsmt.id;
          pAns7.Possible_Answer__c = possibleanswer7;
          insert pAns7;
          pAns8.Participant_Assessment__c = pAsmt.id;
          pAns8.Possible_Answer__c = possibleanswer8;
          insert pAns8;
          pAns9.Participant_Assessment__c = pAsmt.id;
          pAns9.Possible_Answer__c = possibleanswer9;
          insert pAns9;
          pAns10.Participant_Assessment__c = pAsmt.id;
          pAns10.Possible_Answer__c = possibleanswer10;
          insert pAns10;
          pAns11.Participant_Assessment__c = pAsmt.id;
          pAns11.Possible_Answer__c = possibleanswer11;
          insert pAns11;
          pAns12.Participant_Assessment__c = pAsmt.id;
          pAns12.Possible_Answer__c = possibleanswer12;
          insert pAns12;
          pAns13.Participant_Assessment__c = pAsmt.id;
          pAns13.Possible_Answer__c = possibleanswer13;
          insert pAns13;
          pAns14.Participant_Assessment__c = pAsmt.id;
          pAns14.Possible_Answer__c = possibleanswer14;
          insert pAns14;
          pAns15.Participant_Assessment__c = pAsmt.id;
          pAns15.Possible_Answer__c = possibleanswer15;
          insert pAns15;
          pAns16.Participant_Assessment__c = pAsmt.id;
          pAns16.Possible_Answer__c = possibleanswer16;
          insert pAns16;
          pAns17.Participant_Assessment__c = pAsmt.id;
          pAns17.Possible_Answer__c = possibleanswer17;
          insert pAns17;
          pAns18.Participant_Assessment__c = pAsmt.id;
          pAns18.Possible_Answer__c = possibleanswer18;
          insert pAns18;
          
          //create participant interaction for OSM completion
          pInt.Interaction_Date__c = pAsmt.Assessment_Date__c;
          pInt.Interaction_Summary__c = 'OSM Completed';
          pInt.Participant__c = participant.id;
          insert pInt;
          	} catch (Exception e) {
          		p = Page.failure;
          		p.getParameters().put('error','noInsert');
          	}
          }
          
          if (p == null) {
          	p = new ApexPages.StandardController(pAsmt).view();
          }
          p.setRedirect(true);
          return p;
      }
}

 

The test method, which tells me the controller is hitting exception: "List has no rows for assignment to sObject" on line 307 which I marked in the above controller code...

 

@isTest
public class TestosmController {
	
    public static testMethod void myUnitTest(){
        
        PageReference pageRef1 = Page.osmStep1;
        Test.setCurrentPage(pageRef1);
        
        osmController controller = new osmController();
        String savePage = controller.save().getUrl();
        
        // verify that page fails without parameters
        System.assertEquals('/apex/failure?error=noParam', savePage);
        
        
        Profile pf = [Select Id from Profile where Name = 'Sys Admin'];

        User u = new User();
        u.FirstName = 'Test';
        u.LastName = 'User';
        u.Email = 'testuser@test123456789.com';
        u.CompanyName = 'test.com';
        u.Title = 'Test User';
        u.Username = 'testuser@test123456789.com';
        u.Alias = 'testuser';
        u.CommunityNickname = 'Test User';
        u.TimeZoneSidKey = 'America/Mexico_City';
        u.LocaleSidKey = 'en_US';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.ProfileId = pf.Id;
        u.LanguageLocaleKey = 'en_US';
        insert u;

        system.runAs(u){

            Family__c o = new Family__c();
            o.Family_Type__c = 'Other';
            insert o;

            system.assertEquals(o.Family_Income__c, null);

            //Test payments on insert
            Participant__c p1 = new Participant__c();
            p1.Family__c = o.Id;
            p1.First_Name__c = 'Testy';
            p1.Last_Name__c = 'Testoferson';
            p1.Social_Security_Number__c = '999999998';
            p1.Date_of_Birth__c = Date.newInstance(2000, 1, 1);
            p1.Disabled__c = 'No';
            p1.Education__c = 'Unknown';
            p1.Gender__c = 'Unknown';
            p1.Ethnicity__c = 'No Response';
            p1.Race__c = 'African American';
            p1.Primary_Language__c = 'Spanish';
            p1.Health_Insurance__c = 'Yes';
            p1.SSN_Status__c = 'Verified';
            p1.Food_Stamps__c = 'No';
            p1.Veteran_Code__c = 'No';
            p1.WIC__c = 'No';
            p1.Marital_Status__c = 'Single';
            insert p1;

        // Add parameters to page URL 
        ApexPages.currentPage().getParameters().put('id', p1.id);
                       
        }
    }
}

 I'm trying to insert the id of a freshly inserted participant into the query string parameter in my test method and it is still returning null, any idea what I'm doing wrong?

 

Thank you!

Hi tell me the good example how to write test classes
  • March 27, 2012
  • Like
  • 0

Dear all,

 

I have created the following test class to test a trigger that works.  The trigger changes the value on a dependend object's picklist according to the choice of the parent object.

The test reaches only 55%.

It needs to reach 75% coverage.

Below is the test code and way beyod the trigger.

Any suggestions are more than welcome!

 

Test Code:

 

@isTest
public class testChangeofStatusinAssociate {
static testMethod void testChangeofStatusinAssociate(){
List<Account> accounts = new List<Account>{};
for(Integer i = 0; i < 30; i++){
Account a = new Account(Name = 'Test Account ' + i,
Associate_s_Title__c='dd',Status__c='Under Deactivation');
accounts.add(a);

List<Location__c> Location = new List<Location__c>{};
Location__c b = new Location__c(Location_Name__c='AAA',
Status__c='Under Deactivation');
Location.add(b);
insert Location;
}

test.startTest();
insert accounts;
test.stopTest();
}
}

 

Trigger:

trigger updateactive on Account (after update, after insert){
Location__c[] StatusToUpdate = new Location__c[]{};
for(Account  c : trigger.new ){
for(Location__c cp: [ 
select id,name,Status__c from Location__c  
where Associate__c =:c.id]){
if ( c.Status__c == 'Under Deactivation'){
cp.Status__c = 'Under Deactivation';
}
StatusToUpdate.add(cp);
}
}
if(StatusToUpdate.size()>0) 
update StatusToUpdate;
}

Hi,

 

I have a input textbox and a command button in a page. When the user enters some text and clicks on the button i am invoking a method in Apex class.

 

The same functionality should be performed even when the user enters some text and clicks enter key.

 

I think this can be achieved using accessKey property of commandButton. I tried but it didn't worked.

 

Can someone please help me on this.

Hi,

 

I could't manage to figure out how (if its possible) to translate the Standard Object / Standard field labels ( eg, Account objct / "Account name" field)?

I tried to use the Translation Workbench, but there I can only change the custom field labels.

I am new to Email Services and Apex and wanting to learn effective methods for testing classes that implement the Messaging.InboundEmailHandler.  Please refer me to any good material for learning test methods and best practices for conducting them.  Please see the class code (ProcessApplicant) and Initial Test method Apex Class.  I'm only getting 16% code coverage using the current test method.

 

Thanks,

SCR

/* * This class implements Message.InboundEmailHandler to accept inbound applications for a given position. * It results in a new Candidate (if one doesnt already exist) and Job Description record being created. * Any binary email attachments are automatically transfered to the Job Description as attachments on the Notes and Attachments related list. */ global class ProcessApplicant implements Messaging.InboundEmailHandler { private static final String PHONE_MATCH = 'Phone:'; global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope envelope) { // Indicates if the message processes successfully, permits an error response to be sent back to the user. Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); try { // Obtain the applicants name and the position they are applying for from the email. String firstName = email.fromname.substring(0,email.fromName.indexOf(' ')); String lastName = email.fromname.substring(email.fromName.indexOf(' ')); String positionName = email.subject; String emailBody = email.plainTextBody; String emailFrom = envelope.fromAddress; // Process the email information. processApplicantEmail(firstName, lastName, positionName, emailBody, emailFrom, email.binaryAttachments); // Success. result.success = true; } catch (Exception e) { // Return an email message to the user. result.success = false; result.message = 'An error occured processing your message. ' + e.getMessage(); } return result; } public static void processApplicantEmail(String firstName, String lastName, String positionName, String emailBody, String emailFrom, Messaging.InboundEmail.BinaryAttachment[] binaryAttachments) { // Obtain the applicants Phone number from the message body. integer phoneMatchIdx = emailBody.indexOf(PHONE_MATCH); if(phoneMatchIdx==-1) throw new ProcessApplicantException('Please state your phone number following Phone: in your message.'); String phoneNumber = emailBody.substring(phoneMatchIdx+PHONE_MATCH.length()); // Resolve the Id of the Position they are applying for. Id positionId; try { // Lookup the Position they are applying for by name. Position__c position = [select Id from Position__c where name = :positionName limit 1]; positionId = position.Id; } catch (Exception e) { // Record not found. throw new ProcessApplicantException('Sorry the position ' + positionName + ' does not currently exist.'); } // Candidate record for the applicant sending this email. Candidate__c candidate; try { // Attempt to read an existing Candidate by email address. Candidate__c[] candidates = [select Id from Candidate__c where email__c = :emailFrom]; candidate = candidates[0]; } catch (Exception e) { // Record not found, create a new Candidate record for this applicant. candidate = new Candidate__c(); candidate.email__c = emailFrom; candidate.first_name__c = firstName; candidate.last_name__c = lastName; candidate.phone__c = phoneNumber; insert candidate; } // Create the Job Application record. Job_Application__c jobApplication = new Job_Application__c(); jobApplication.Position__c = positionId; jobApplication.Candidate__c = candidate.id; insert jobApplication; // Store any email attachments and associate them with the Job Application record. if (binaryAttachments!=null && binaryAttachments.size() > 0) { for (integer i = 0 ; i < binaryAttachments.size() ; i++) { Attachment attachment = new Attachment(); attachment.ParentId = jobApplication.Id; attachment.Name = binaryAttachments[i].Filename; attachment.Body = binaryAttachments[i].Body; insert attachment; } } // Confirm receipt of the applicants email Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses( new String[] { emailFrom }); mail.setSubject('Your job application has been received'); mail.setPlainTextBody('Thank you for submitting your resume. Your application will be reviewed to determine if an interview will be scheduled.'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } /* * Represents errors that have occured interpretting the incoming email message */ class ProcessApplicantException extends Exception { } }

 

 

//The ProcessApplicant Test: public class ProcessApplicantTest { public static testMethod void ProcessApplicantTest(){ System.debug('Im starting a method'); // Create a new email, envelope object and Attachment Messaging.InboundEmail email = new Messaging.InboundEmail(); Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope(); email.subject = 'test'; email.plainTextBody = 'Hello, this a test email body. for testing purposes only. Bye'; envelope.fromAddress = 'user@acme.com'; // setup controller object ProcessApplicant catcher = new ProcessApplicant(); catcher.handleInboundEmail(email, envelope); } }

 

  • July 21, 2009
  • Like
  • 0