• DeveloperSalesforce
  • NEWBIE
  • 75 Points
  • Member since 2014

  • Chatter
    Feed
  • 2
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 4
    Replies
Hey there,

I have a bit of code for a controller which I found online. This was created by Jeff Douglas as a means of creating a custom lookup. I have written loads of standard controler test classes, but i have never written one for a custom controller. i was hoping i could get a hand in writing one.

Thank you in advance for your time, below is my code:
 
public with sharing class CustomEPELookupController {
    
  public EPE_Course__c EPE {get;set;} // new EPE to create
  public List<EPE_Course__c> results{get;set;} // search results
  public string searchString{get;set;} // search keyword
  
  public CustomEPELookupController() {
    EPE = new EPE_Course__c();
    // get the current search string
    searchString = System.currentPageReference().getParameters().get('lksrch');
    runSearch();  
  }
   
  // performs the keyword search
  public PageReference search() {
    runSearch();
    return null;
  }
  
  // prepare the query and issue the search command
  private void runSearch() {
    // TODO prepare query string for complex serarches & prevent injections
    results = performSearch(searchString);               
  } 
  
  // run the search and return the records found. 
  private List<EPE_Course__c> performSearch(string searchString) {
 
    String soql = 'select id, name from EPE_Course__c';
    if(searchString != '' && searchString != null)
      soql = soql +  ' where name LIKE \'%' + searchString +'%\'';
    soql = soql + ' limit 25';
    System.debug(soql);
    return database.query(soql); 
 
  }
  
  // save the new account record
  public PageReference saveEPE() {
    insert EPE;
    // reset the account
    EPE = new EPE_Course__c();
    return null;
  }
  
  // used by the visualforce page to send the link to the right dom element
  public string getFormTag() {
    return System.currentPageReference().getParameters().get('frm');
  }
    
  // used by the visualforce page to send the link to the right dom element for the text box
  public string getTextBox() {
    return System.currentPageReference().getParameters().get('txt');
  }
 
}

 
Hi all,

I have a test class which has passed when i run it, however the code coverage is at 72%.
I have tried to get the test class above 75% however i have had no luck.
Is there something I am missing?

Trigger:
trigger HRcheckedTimeTrigger1 on Match_Day_Check_List__c (before insert, before update) {

if(Trigger.isInsert){
        for(match_day_check_list__c HR: trigger.new){
        
          HR.All_Staff_Covered_Last_Modified__c = DateTime.Now();
          HR.Uniform_Checks_Last_Modified__c = DateTime.Now();
          HR.Diciplinaries_Last_Modified__c = DateTime.Now();
          HR.Casual_Starter_Checks_Last_Modified__c = DateTime.Now();
          HR.Grievances_Last_Modified__c = DateTime.Now();
          
          }
       }
       if(Trigger.isUpdate){
    
     for(match_day_check_list__c HR: trigger.new){
            match_day_check_list__c oldHR = Trigger.oldMap.get(HR.ID);
            
           if(oldHR.All_Staff_Covered__c != HR.All_Staff_Covered__c){
                HR.All_Staff_Covered_Last_Modified__c = DateTime.Now();
            }
            
            if(oldHR.Uniform_Checks__c != HR.Uniform_Checks__c){
                HR.Uniform_Checks_Last_Modified__c = DateTime.Now();
            }
            
             if(oldHR.Diciplinaries__c != HR.Diciplinaries__c){
                HR.Diciplinaries_Last_Modified__c = DateTime.Now();
            }
            
            if(oldHR.Diciplinaries__c != HR.Diciplinaries__c){
                HR.Diciplinaries_Last_Modified__c = DateTime.Now();
            }
            
            if(oldHR.Casual_Starter_Checks__c != HR.Casual_Starter_Checks__c ){
                HR.Casual_Starter_Checks_Last_Modified__c = DateTime.Now();
            }
            
             if(oldHR.Grievances__c != HR.Grievances__c ){
                HR.Grievances_Last_Modified__c = DateTime.Now();
            }


}

}
}

Test Class:
@istest(seealldata=true)
public class checkHRMatchReadyTest{
public static testmethod void HRcheckedTimeTrigger1(){
match_day_check_list__c hr = new match_day_check_list__c ();
hr.Fixture__c = [select id from CS_Fixture__c limit 1].id;
hr.Match_Plan__c = [select id from Pre_Match__c limit 1].id;
insert hr;


hr.All_Staff_Covered__c=true;
hr.All_Staff_Covered__c=false;
hr.Uniform_Checks__c = true;
hr.Uniform_Checks__c = false;
hr.Diciplinaries__c = true;
hr.Diciplinaries__c = false;
hr.Casual_Starter_Checks__c = true;
hr.Casual_Starter_Checks__c = false;
hr.Grievances__c = true;
hr.Grievances__c = false;
hr.HR_Match_Day_Ready__c = true;
hr.HR_Match_Day_Ready__c = false;
hr.HR_Sign_off__c = 'Helen Burns';
update hr;
}
}


Many thanks
1. enter Name into XXX field (Lookup USer)
2.Then modify the code to update the owner of the contact to the Name
   ownerId=Name

Any good idea to create trigger--Contact(before insert)??
Hi,

public with sharing class ScotiaRecruitmentContactHelper {

        @future
        public static void UpdateRecContactRecordType(Set<Id> contactRecords,Set<Id> newUserIds)
         {

     RecordType cRecordType = [select Id from RecordType where Name = 'XXX' and SobjectType = 'Contact' limit 1];
             
              list<User> lstUsers=[select Id,Email,Title,MobilePhone,Phone from User where Id in :newUserIds]; 
          
              for(Contact xCnt : [select User__c,RecordTypeId,email,title,Company_Name__c,MobilePhone,Phone
                                    from Contact  where id in :contactRecords]) {
                  xCnt.RecordTypeId = cRecordType.Id;
                  xCnt.User__c=lstUsers[0].id;
                  xCnt.email=lstUsers[0].Email;
                  xCnt.title=lstUsers[0].Title;
                  xCnt.Company_Name__c='Scotia Home Financing Solution';
                  xCnt.MobilePhone=lstUsers[0].MobilePhone;
                  xCnt.Phone=lstUsers[0].Phone;
                  lstContact.add(xCnt);
              }
              
             system.debug('lstContact'+lstContact); 
             update lstContact;

    }


And one more 

public class UserChangeLogUpdate{
@future
    public static void createUserChangeLogNew (Map<Id, Boolean>
{

}
    insert newUCLog;
}



I have a scenario, where it's updating in trigger and in apex class,
Pls help to recolve:

@Future:System.AsyncException: Future method cannot be called from a future or batch method: HelperclassUpdate.createUserChange(MAP<Id,Boolean>)
Hi, 

I have a requirement while converting/creating a new user linked to contact.
1. Contact-->convert Usr-->created new user
then, Flag all notes and attachments as “private” for notes and attachments created prior to converting user.
How to achive this? Pls help
Hi, 

I have requirement here:

When XYZ contact is created, check if a XYZ Company with exact Company Name field on Account exists.
If exists, link contact to the account. If Company does not exist, then create a new XYZ Account and set owner.

trigger TriggerContactInsert on Contact (before insert) {
List<String> cntCompName = new List<String>();

   for(Contact xcnt: trigger.new)
    {
      cntCompName.add(xcnt.Company_Name__c);
    }

   list<Account> acctLst = [select Id, Name,(select Company_Name__c from Contacts) from Account where Name IN :cntCompName];
   system.debug('Account' + acctLst);
  
   Map<id,List<Contact>> mapofCmpNames = new Map<id,List<Contact>>();

    for(Account l : acctlst)
    {
       
        for(List<Contact> e : l.Contacts)
        {
            mapofCmpNames.put(l.id, e);
        }
    }
  
   for(Contact xcnt: trigger.new)
   {
        List<Contact> existingCmp = mapofCmpNames.get(xcnt.Company_Name__c);
        for(Contact xcntc: existingCmp) {
        if(xcnt.Company_Name__c  == xcntc.Company_Name__c)
        {
            //Link contact to Account
        }
       else
      {
         //create new account
     }
   }
}
}


Is there any other approach I can follow?
Hey there,

I have a bit of code for a controller which I found online. This was created by Jeff Douglas as a means of creating a custom lookup. I have written loads of standard controler test classes, but i have never written one for a custom controller. i was hoping i could get a hand in writing one.

Thank you in advance for your time, below is my code:
 
public with sharing class CustomEPELookupController {
    
  public EPE_Course__c EPE {get;set;} // new EPE to create
  public List<EPE_Course__c> results{get;set;} // search results
  public string searchString{get;set;} // search keyword
  
  public CustomEPELookupController() {
    EPE = new EPE_Course__c();
    // get the current search string
    searchString = System.currentPageReference().getParameters().get('lksrch');
    runSearch();  
  }
   
  // performs the keyword search
  public PageReference search() {
    runSearch();
    return null;
  }
  
  // prepare the query and issue the search command
  private void runSearch() {
    // TODO prepare query string for complex serarches & prevent injections
    results = performSearch(searchString);               
  } 
  
  // run the search and return the records found. 
  private List<EPE_Course__c> performSearch(string searchString) {
 
    String soql = 'select id, name from EPE_Course__c';
    if(searchString != '' && searchString != null)
      soql = soql +  ' where name LIKE \'%' + searchString +'%\'';
    soql = soql + ' limit 25';
    System.debug(soql);
    return database.query(soql); 
 
  }
  
  // save the new account record
  public PageReference saveEPE() {
    insert EPE;
    // reset the account
    EPE = new EPE_Course__c();
    return null;
  }
  
  // used by the visualforce page to send the link to the right dom element
  public string getFormTag() {
    return System.currentPageReference().getParameters().get('frm');
  }
    
  // used by the visualforce page to send the link to the right dom element for the text box
  public string getTextBox() {
    return System.currentPageReference().getParameters().get('txt');
  }
 
}

 
Hi all,

I have a test class which has passed when i run it, however the code coverage is at 72%.
I have tried to get the test class above 75% however i have had no luck.
Is there something I am missing?

Trigger:
trigger HRcheckedTimeTrigger1 on Match_Day_Check_List__c (before insert, before update) {

if(Trigger.isInsert){
        for(match_day_check_list__c HR: trigger.new){
        
          HR.All_Staff_Covered_Last_Modified__c = DateTime.Now();
          HR.Uniform_Checks_Last_Modified__c = DateTime.Now();
          HR.Diciplinaries_Last_Modified__c = DateTime.Now();
          HR.Casual_Starter_Checks_Last_Modified__c = DateTime.Now();
          HR.Grievances_Last_Modified__c = DateTime.Now();
          
          }
       }
       if(Trigger.isUpdate){
    
     for(match_day_check_list__c HR: trigger.new){
            match_day_check_list__c oldHR = Trigger.oldMap.get(HR.ID);
            
           if(oldHR.All_Staff_Covered__c != HR.All_Staff_Covered__c){
                HR.All_Staff_Covered_Last_Modified__c = DateTime.Now();
            }
            
            if(oldHR.Uniform_Checks__c != HR.Uniform_Checks__c){
                HR.Uniform_Checks_Last_Modified__c = DateTime.Now();
            }
            
             if(oldHR.Diciplinaries__c != HR.Diciplinaries__c){
                HR.Diciplinaries_Last_Modified__c = DateTime.Now();
            }
            
            if(oldHR.Diciplinaries__c != HR.Diciplinaries__c){
                HR.Diciplinaries_Last_Modified__c = DateTime.Now();
            }
            
            if(oldHR.Casual_Starter_Checks__c != HR.Casual_Starter_Checks__c ){
                HR.Casual_Starter_Checks_Last_Modified__c = DateTime.Now();
            }
            
             if(oldHR.Grievances__c != HR.Grievances__c ){
                HR.Grievances_Last_Modified__c = DateTime.Now();
            }


}

}
}

Test Class:
@istest(seealldata=true)
public class checkHRMatchReadyTest{
public static testmethod void HRcheckedTimeTrigger1(){
match_day_check_list__c hr = new match_day_check_list__c ();
hr.Fixture__c = [select id from CS_Fixture__c limit 1].id;
hr.Match_Plan__c = [select id from Pre_Match__c limit 1].id;
insert hr;


hr.All_Staff_Covered__c=true;
hr.All_Staff_Covered__c=false;
hr.Uniform_Checks__c = true;
hr.Uniform_Checks__c = false;
hr.Diciplinaries__c = true;
hr.Diciplinaries__c = false;
hr.Casual_Starter_Checks__c = true;
hr.Casual_Starter_Checks__c = false;
hr.Grievances__c = true;
hr.Grievances__c = false;
hr.HR_Match_Day_Ready__c = true;
hr.HR_Match_Day_Ready__c = false;
hr.HR_Sign_off__c = 'Helen Burns';
update hr;
}
}


Many thanks
i hav custon button "create deal" , which on click calls this action.
Name__c is a (look up fields to account) on test 1 object
<apex:page standardController="Test_1__c"   extensions="CreateDeal" >
<script type="text/javascript">
window.onload = function () {
//document.frmLead.sendButton.click();
dealInJS();
}
</script>
<apex:form > 
  
  <apex:pageMessages escape="false" />
  
  
     <apex:outputPanel layout="block" style="font-weight:bold;text-align:center"> 
         Converting Deal. Please wait...
     </apex:outputPanel> 
     
<apex:actionFunction action="{!deal}" name="dealInJS" >
</apex:actionFunction>
</apex:form>
</apex:page>


public class CreateDeal
 {
  
 
    private id accountId{get;set;} 
     public Test_1__c b{get;set;}

    
    public CreateDeal(ApexPages.StandardController controller) 
    {
    accountId= ApexPages.currentPage().getParameters().get('id');
    system.debug('+++'+accountId);
    }
    
    public  pageReference  deal()
    {
     Test_1__c  b = [Select id,Name__c,Name11__c,Number__c,Phone__c from Test_1__c  where id =:accountId];
     Test_2__c a = new Test_2__c();
     {
     
      a.Name11__c=b.Name11__c;
      a.Number__c=b.Number__c;
      a.Phone__c=b.Phone__c;
       a.Name__c=b.Name__c;
      }
      insert a;
    
    PageReference pageRef = new PageReference('/'+a.id);
    pageRef.setRedirect(true);
    return pageRef;
    }


}

how to write test class for this, i read few documentation but i cant prepare the test class
Hi, 

I have requirement here:

When XYZ contact is created, check if a XYZ Company with exact Company Name field on Account exists.
If exists, link contact to the account. If Company does not exist, then create a new XYZ Account and set owner.

trigger TriggerContactInsert on Contact (before insert) {
List<String> cntCompName = new List<String>();

   for(Contact xcnt: trigger.new)
    {
      cntCompName.add(xcnt.Company_Name__c);
    }

   list<Account> acctLst = [select Id, Name,(select Company_Name__c from Contacts) from Account where Name IN :cntCompName];
   system.debug('Account' + acctLst);
  
   Map<id,List<Contact>> mapofCmpNames = new Map<id,List<Contact>>();

    for(Account l : acctlst)
    {
       
        for(List<Contact> e : l.Contacts)
        {
            mapofCmpNames.put(l.id, e);
        }
    }
  
   for(Contact xcnt: trigger.new)
   {
        List<Contact> existingCmp = mapofCmpNames.get(xcnt.Company_Name__c);
        for(Contact xcntc: existingCmp) {
        if(xcnt.Company_Name__c  == xcntc.Company_Name__c)
        {
            //Link contact to Account
        }
       else
      {
         //create new account
     }
   }
}
}


Is there any other approach I can follow?