• cashworth
  • NEWBIE
  • 85 Points
  • Member since 2010

  • Chatter
    Feed
  • 3
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 19
    Replies

I want to knw whether a checkbox was ticked or not.

 

Something like Object.fieldName.isChecked().

 

How do I do that?

  • January 23, 2012
  • Like
  • 0

is it possible to use a trigger/apex to create an opportunity automatically from a contact once a contact is created?

  • September 16, 2011
  • Like
  • 0

Looking to see if anyone has information on what the related list name is for Campaign Influence on Opportunities. Working on a tabbed Opportunities page and its the only list where I cannot find the API name. I have tried APEX Explorere and searching the boards but no luck yet. The best info i have come across so far were some older posts indicating that this list is not exposed to the API but was on the roadmap.

 

Anyone have more up-to-date information on this?

 

Just so we are 100% clear on what I am looking to do, here is a code snippet from my VF page:

<apex:tab label="Campaign" name="Campaign" id="tabCamp">
         <apex:relatedList subject="{!Opportunity}" list="CampaignInfluence" />
      </apex:tab>

 Thx!

Hey developer gurus...

 

Running into a snag while trying to get coverage on an Inbound email handler. Looking to see if anyone can point me down the right path to getting a test method constructed properly so I can more this class into production. The class is "working" in sandbox perfectly but the test coverage I am getting is only 25%.

 

This class performs the following:

 

1. Receives the inbound email and dissects the envelope and email creating string variables where necessary

2. Looks up an existing account in the system and matches it to one of the toAddresses (Quote_Address__C field).

3. Looks for an existing contact where the contact's email matches the fromAddress in the envelope AND the account id from step 2.

4. Inserts a Quote Request record against the account with the contact added to that record.

5. If no contact is found, inserts a new contact on that account and then creates the quote request record.

6. Captures any attachments and adds them to the Quote Request record.

7. Sends an email back to the sender letting them know their request was received.

8. If no account is found, sends an error email back to the sender letting them know their request could not be processed.

 

I used a series of IF statements to qualify each of the 3 conditions that would come up to construct the logic behind how the email was handles and what record(s) were created. This seemed like the most reliable method to get the email handler to work 100% reliably.

 

Here's the class:

 

global class EmailQuoteRequest1 implements Messaging.InboundEmailHandler {

    public Contact vCon;
    public Account vAcc;
    public Quote_Request__c qr;
    public string toAddr;
    public string emailaddress;
    public String firstName;
    public String lastName;
    public String Subject;
    public String myPlainText;
    

 
   global Messaging.InboundEmailResult handleInboundEmail(
     Messaging.InboundEmail email,
     Messaging.InboundEnvelope envelope) {
    
         Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
         
         // Captures the sender's email address and name
         String emailAddress = envelope.fromAddress;           
         String lastName= email.fromName.substring(0,email.fromName.indexOf(' '));
         String firstName = email.fromName.substring(email.fromName.indexOf(' '));
         
         // Captures the email subject
         String Subject = email.Subject;
         
         // Add the email plain text into the local variable
         String myPlainText= '';
           myPlainText = email.plainTextBody;     
           
         //Capture the To: Addresses
         String[] toAddr = email.toAddresses; 
         
         //Check that toAddress list != null
         if (toAddr != null){          <---- I get coverage to here then nothing
         
         //Lookup Account from toAddress list
         if ([select count() from Account where Quote_Address__c in :toAddr] > 0)  {
             Account vAcc = [Select Id, OwnerId, Quote_Address__c 
             From Account
             Where Quote_Address__c in :toAddr 
             Limit 1];
             
             //Look for existing contact in the system
             if ([select count() from Contact where 
             AccountId = :vAcc.Id
             and
             Email = :envelope.fromAddress] > 0) {
                 Contact vCon = [Select Id 
                 from Contact
                 where Email = :envelope.fromAddress 
                 Limit 1];             
             
                 //Add the new Quote Request
                 Quote_Request__c[] qr = new Quote_Request__c[0];
                 qr.add(new Quote_Request__c(Request_Detail__c =  myPlainText,
                 Quote_Subject__c = Subject,
                 Customer_Contact__c = vCon.id,
                 Account__c =  vAcc.Id));
                 
                 try {                   
                   insert qr;
                   
                   //Insert Attachments if any
                     if (email.binaryAttachments != 
                     null && email.binaryAttachments.size() > 0)
                     {
                     for (integer i = 0 ; i < email.binaryAttachments.size() ; i++)
                     {
                     Attachment aB = new Attachment(ParentId = qr[0].Id,
                     Name = email.binaryAttachments[i].filename,
                     Body = email.binaryAttachments[i].body);
                     insert aB;
                     }
                     }
         
                     if (email.textAttachments != 
                     null && email.textAttachments.size() > 0)
                     {
                     for (integer i = 0 ; i < email.textAttachments.size() ; i++)
                     {
                     Attachment aT = new Attachment(ParentId = qr[0].Id,
                     Name = email.textAttachments[i].filename,
                     Body = Blob.valueOf(email.textAttachments[i].body));
                     insert aT;
                     }
                     }
                   
                   } catch (System.DmlException e)
                   { result.success = false;
                     result.message = 'failed to attach to account.';
                   System.debug('ERROR: Not able to create Quote Request: ' + e);
                 } 
                 
                     // Send return email to sender confirming receipt of request
                     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                     mail.setToAddresses( new String[] { emailAddress });
                     mail.setSubject('Your quote request has been received');
                     mail.setPlainTextBody('Thank you for submitting your request. { existing contact }. Your Account Manager will be in touch shortly to discuss the details of your request.');
                     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
                     
                 
 

Hoping to get some guidance and maybe lift the fog I seem to be in with understanding test methods for Apex Classes. I have gotten the test methods for triggers down fairly well but am still struggling with getting coverage on my classes that query records and return them to visualforce pages dynamically. I have 3 classes/VF pages that are plaguing me on coverage at the moment. The first is listed below. Any examples and/or recommendations would be appreciated.

 

Lead Inbox:

This is a Class and VF page that we created as a home page component. It returns and displays unread leads for our users filters by lead source so that they can be aware of new Leads assigned to them that need their attention. The class and page is designed to get the leads and also return a count of how many unread leads they have. I have used sharing on this class as the lists generated need to be specific to the rep or manager viewing the inbox on their homepage. I also have a reference to a sorting class on here that allows the reps to sort the list in the VF page.

 

Here's the class. The code works perfectly but I currently have 0% coverage as I have no test method on it.

 

public with sharing class LeadInbox {


 
public PageReference ViewData() {
return null;
}

List<Lead> rqs;
public String sortField {get; set;}
public String previousSortField {get; set;}

 public List<Lead> getLead() {

  if(rqs == null){
  rqs = [select Id, Name, Company, LeadSource, Status, OwnerId, CreatedDate from Lead 
  
  Where (
  (LeadSource='Partner Referral'
   OR
  LeadSource='Employee Referral'
   OR
  LeadSource='External Referral Program'
   OR
  LeadSource='Management Networking'
   OR
  LeadSource='Sales Inquiry (Web)'
   OR
  LeadSource='Telemarketing'
  )

  AND
  IsUnreadByOwner = TRUE
  AND
  Status != 'Disqualified')
  ORDER BY Lead.CreatedDate Desc
  Limit 4 ];
  }
  return  rqs;
 }


public void doSort(){
        String order = 'asc';
        
        /*This checks to see if the same header was click two times in a row, if so 
        it switches the order.*/
        if(previousSortField == sortField){
            order = 'desc';
            previousSortField = null;
        }else{
            previousSortField = sortField;
        }
       
        //To sort the table we simply need to use this one line, nice!
        superSort.sortList(rqs,sortField,order);
    }


public Integer getUnreadLeads() {

return [

select count() from Lead

where (IsConverted = False
 AND 
  IsUnreadByOwner = TRUE
 AND
  Status != 'Disqualified'
 AND
  (LeadSource ='Partner Referral'
   OR
  LeadSource='Employee Referral'
   OR
  LeadSource='External Referral Program'
   OR
  LeadSource='Management Networking'
   OR
  LeadSource='Sales Inquiry (Web)'
   OR
  LeadSource='Telemarketing'
  )

];

}

 

and the VF page: It works perfectly as well. :) 

 

<apex:page cache="true" controller="LeadInbox" tabStyle="Lead" showHeader="false">
<html>

<body>

<apex:form id="test">
  <apex:pageblock title="">

    <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>     
             <td width="13%" height="120">
              
                 <table>
                 
 <tr><td align="center"><font size="5" color="#0000CC"><a href="/00Q?fcf=00B30000005veVy" target="_parent"><b>{!UnreadLeads}</b></a></font></td></tr>                
 <tr><td align="center"> <font size="3" color="#0000CC">Unread Referral Leads</font></td></tr>
                    
                 </table>       
            </td>
    
            <td width="87%">
        <apex:pageBlockTable value="{!Lead}" var="L" id="table">
                                               
            <apex:column >
                <apex:facet name="header">Action</apex:facet>
                <apex:outputLink title="" value="/{!L.id}" target="_blank" style="font-weight:bold">View</apex:outputLink>&nbsp;        
            </apex:column>    
            
           <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Lead.Fields.Name.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Name" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!L.Name}"/>
                </apex:column>

            
           <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Lead.Fields.Company.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Company" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!L.Company}"/>
                </apex:column>
            
            <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Lead.Fields.LeadSource.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="LeadSource" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!L.LeadSource}"/>
                </apex:column>
            
            <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Lead.Fields.Status.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Status" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!L.Status}"/>
                </apex:column>
            
            <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Lead.Fields.OwnerId.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="OwnerId" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!L.OwnerId}"/>
                </apex:column>
            
            <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Lead.Fields.CreatedDate.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="CreatedDate" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!L.CreatedDate}"/>
                </apex:column>
            
        </apex:pageBlockTable>
        
      </td>
      
     </tr>   
    </table>    
        
  </apex:pageblock>
    
</apex:form>

</body>
</html>
</apex:page>

Any help on developing a test method on this would be appreciated. Its just a query and page render so I know its can't be THAT complicated. I have read through all the documentation on writing test methods and something about it for classes just doesn't make sense to me.

 

I want to knw whether a checkbox was ticked or not.

 

Something like Object.fieldName.isChecked().

 

How do I do that?

  • January 23, 2012
  • Like
  • 0

I'm trying to write a trigger that updates the child records on an Opportunity when a formula field - Contract Status - shows the value Cancelled/Recalled.  I'm not trying to update the Contract Status field, I just referencing it so that the trigger only fires when it shows that one value.

 

Here's the trigger and error message.  I'm at a loss as to how to fix this.

 

trigger CancelSuppHistory on Opportunity (before update) {

If(Trigger.isUpdate){
    
     Set<ID> ids = new Set<ID>();
     list<Opportunity> updOpps =
     [SELECT Id,
                 (Select Id, opportunity__c, active_media_plan__c, active_art__c
                  from Media_Plan_History__r ) FROM Opportunity
                 WHERE Id in :ids];        
                
                 List<Media_Plan_History__c> SHToUpdate = new List<Media_Plan_History__c>();

         //Then loop through each parent object in 'updated parent
         for (Opportunity Opp : updOpps)
         {
             if(opp.contract_status__c = 'Cancelled/Recalled')
            
                //and loop thru each kid in the child set}
               for(Media_Plan_History__c sh : Opp.Media_Plan_History__r)
               {
                         ///update logic from above
                          if(sh.active_media_plan__c = TRUE)

{
       sh.active_media_plan__c =  FALSE;
      shToUpdate.add(sh);
}
                }
        }
       if( !shToUpdate.isEmpty)
       {
            update shToUpdate;
       }
}


}

 

 

Error: Compile Error: Field is not writeable: Opportunity.Contract_Status__c at line 17 column 17

 

Is there a way to write a trigger that will fire when an AccountContactRole is added or deleted to/from an account? According to the documentation, the Account Trigger does not fire for related child objects, which means that there is no way to update information from the assignment of a given role back up to the parent account until the user edits the account.

 

Any Ideas?


Thanks,


Robin

  • December 02, 2011
  • Like
  • 0

Hello,

 

I am trying to create a trigger on the Contract record that will update 2 email fields on the contract record from fields on the releated opportunity record.

 

Here is my code:

trigger UpdateContractEmails on Contract (before insert, before update, after insert, after update){ 

for (Contract c : trigger.new){

   Opportunity opp = [SELECT Id FROM Opportunity WHERE Id = :c.Opportunity__c]; 
   
c.Account_Manager_Email__c = opp.Account_Manager_Email__c;
c.Account_Executive_Email__c = opp.Account_Executive_Email__c;
update c;

}
}

 

And here is the error I am getting on the record when I am trying to save:

 

Apex trigger UpdateContractEmails caused an unexpected exception, contact your administrator: UpdateContractEmails: execution of BeforeInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Opportunity.Account_Manager_Email__c: Trigger.UpdateContractEmails: line 7, column 30

 

Any ideas on what changes I need to make to the code to make this work? I would use a formula field to port this info over, but I need these fileds to be filed type email address so I can use them in an email alert.

 

Thanks!

  • December 02, 2011
  • Like
  • 0

Does anyone know where I can find the details of Account search page? I would like to create a custom controller and vf page to add more details and need a starting point.

 

  • November 16, 2011
  • Like
  • 0
Could someone provide the Apex code for a trigger that automatically pulls the opportunity creator/owner to the sales team on the oppty? 

Here is an example: 

User: John Smith
Profile: CDS

Whenever this guy creates (owns) an oppty, i would like him to immediately be added to the Sales Team with the team role "Sales - CDS"
* all of the team roles say "sales - " and then the department which always matches the profile name.

Can anyone help? :-)

is it possible to use a trigger/apex to create an opportunity automatically from a contact once a contact is created?

  • September 16, 2011
  • Like
  • 0

tthis is the trigger i have written and in my trigger i am unable to cover this part   i have given trigger.new and old please help me how to cover this part

 

 

 

    

trigger trgCaptureConsultants on Opportunity (before Insert, before Update) {

    If(Trigger.New.Size() == 1)
    {
        If(Trigger.IsInsert)
        {
            String ProfileName = [SELECT Profile.Name FROM User where Id = :Trigger.New[0].OwnerId].Profile.Name;    
            
            If(ProfileName.contains('Sales'))//&& (Trigger.New[0].Sales_Consultant__c == Null || Trigger.New[0].Sales_Consultant__c == '')
            {
                Trigger.New[0].Sales_Consultant__c = Trigger.New[0].OwnerId;
                If(Trigger.New[0].Sales_Consultan__c == null)
                {
                Trigger.New[0].Sales_Consultan__c = Trigger.New[0].OwnerId;
                }


            }
            if(ProfileName.contains('Process') ) //&& (Trigger.New[0].Process_Consultant__c == Null || Trigger.New[0].Process_Consultant__c == ''))
            {    
                Trigger.New[0].Process_Consultant__c = Trigger.New[0].OwnerId;
                If(Trigger.New[0].Process_Consultant_User__c == null)
                {
                Trigger.New[0].Process_Consultant_User__c = Trigger.New[0].OwnerId;
                }
            }
            if( ProfileName.contains('Evaluation')) 
            {
                Trigger.New[0].Process_Consultant__c = Trigger.New[0].OwnerId;
                Trigger.New[0].Process_Consultant_User__c = Trigger.New[0].OwnerId;
            }
        } 
        Else If(Trigger.IsUpdate)
        {
            String ProfileName = [SELECT Profile.Name FROM User where Id = :Trigger.New[0].OwnerId].Profile.Name;    
            If(Trigger.New[0].
OwnerId != Trigger.Old[0].OwnerId)
            {
                
                
                If(
ProfileName.contains('Sales'))
                {
                    Trigger.
New[0].Sales_Consultant__c = Trigger.New[0].OwnerId;
                    
                    If(
Trigger.New[0].Sales_Consultant__c != null)
                    {
                    Trigger.
New[0].Sales_Consultan__c = Trigger.New[0].OwnerId;
                    }

                }
                if(
ProfileName.contains('Process'))
                 {
                    Trigger.
New[0].Process_Consultant__c = Trigger.New[0].OwnerId;
                    
                    If(
Trigger.New[0].Process_Consultant__c != null)
                    {
                    Trigger.
New[0].Process_Consultant_User__c = Trigger.New[0].OwnerId;
                    }
                 }
                 
            }

            if(ProfileName.contains('Evaluation'))
            {
                Trigger.New[0].Process_Consultant__c = Trigger.New[0].OwnerId;
                Trigger.New[0].Process_Consultant_User__c = Trigger.New[0].OwnerId;
            }
                
                    
        }
        
    
    } 

this is the test case i have written i am getting inly 50 percent plz help me resolve this issue

 

@isTest
private class trgCaptureConsultants_TESTS{
static testMethod void mytestclass(){
      Account a = [Select id, name from account limit 1 ];
   user u = [SELECT Profile.Name FROM User where profile.name = 'Sales Consultant' limit 1 ];
  
   
Opportunity o = new Opportunity();
o.name = 'raja';
o.OwnerId  = u.id;
o.Accountid = a.id;
o.StageName = 'Opened';
o.CloseDate = system.today();
o.Process_Consultant__c = '0053000000203VvAAI';
o.Sales_Consultan__c = null ;
o.StageName = 'Evaluation Sale Made';
o.Apply_For_Exception_Withdrawal_Refund__c = False ;
try
{
insert o;
}
catch (Exception e)
{}

Case c = new Case ();
c.accountid = a.id;
c.OwnerId  = o.Process_Consultant__c;
c.Opportunity_Name__c  = o.id; 
c.Status = 'New';
c.Origin = 'Email';
c.Case_Reason__c = 'Process';
c.Type = 'Suggestions';
      try{
         Insert c;
Update c;
         }
         catch (Exception e)
      {}
            
            
       }  
      }

 


Hi Guys

 

Very new to the developer boards as I am just starting to learning this side of salesforce, I am wondering is it possible with a trigger to;

 

At a certain point in the week say a Friday evening lock a record and create a new record, which is a clone of the previous week which can be edited until the following week until the same cut off point? Is this possible with a trigger and how complicated would it be? Any feed back or tips would be greatly appreciated, below is a quick screen of what i am tiring to achieve :)

 

Thanks

 

J



 

What i am trying to achieve

Hey developer gurus...

 

Running into a snag while trying to get coverage on an Inbound email handler. Looking to see if anyone can point me down the right path to getting a test method constructed properly so I can more this class into production. The class is "working" in sandbox perfectly but the test coverage I am getting is only 25%.

 

This class performs the following:

 

1. Receives the inbound email and dissects the envelope and email creating string variables where necessary

2. Looks up an existing account in the system and matches it to one of the toAddresses (Quote_Address__C field).

3. Looks for an existing contact where the contact's email matches the fromAddress in the envelope AND the account id from step 2.

4. Inserts a Quote Request record against the account with the contact added to that record.

5. If no contact is found, inserts a new contact on that account and then creates the quote request record.

6. Captures any attachments and adds them to the Quote Request record.

7. Sends an email back to the sender letting them know their request was received.

8. If no account is found, sends an error email back to the sender letting them know their request could not be processed.

 

I used a series of IF statements to qualify each of the 3 conditions that would come up to construct the logic behind how the email was handles and what record(s) were created. This seemed like the most reliable method to get the email handler to work 100% reliably.

 

Here's the class:

 

global class EmailQuoteRequest1 implements Messaging.InboundEmailHandler {

    public Contact vCon;
    public Account vAcc;
    public Quote_Request__c qr;
    public string toAddr;
    public string emailaddress;
    public String firstName;
    public String lastName;
    public String Subject;
    public String myPlainText;
    

 
   global Messaging.InboundEmailResult handleInboundEmail(
     Messaging.InboundEmail email,
     Messaging.InboundEnvelope envelope) {
    
         Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
         
         // Captures the sender's email address and name
         String emailAddress = envelope.fromAddress;           
         String lastName= email.fromName.substring(0,email.fromName.indexOf(' '));
         String firstName = email.fromName.substring(email.fromName.indexOf(' '));
         
         // Captures the email subject
         String Subject = email.Subject;
         
         // Add the email plain text into the local variable
         String myPlainText= '';
           myPlainText = email.plainTextBody;     
           
         //Capture the To: Addresses
         String[] toAddr = email.toAddresses; 
         
         //Check that toAddress list != null
         if (toAddr != null){          <---- I get coverage to here then nothing
         
         //Lookup Account from toAddress list
         if ([select count() from Account where Quote_Address__c in :toAddr] > 0)  {
             Account vAcc = [Select Id, OwnerId, Quote_Address__c 
             From Account
             Where Quote_Address__c in :toAddr 
             Limit 1];
             
             //Look for existing contact in the system
             if ([select count() from Contact where 
             AccountId = :vAcc.Id
             and
             Email = :envelope.fromAddress] > 0) {
                 Contact vCon = [Select Id 
                 from Contact
                 where Email = :envelope.fromAddress 
                 Limit 1];             
             
                 //Add the new Quote Request
                 Quote_Request__c[] qr = new Quote_Request__c[0];
                 qr.add(new Quote_Request__c(Request_Detail__c =  myPlainText,
                 Quote_Subject__c = Subject,
                 Customer_Contact__c = vCon.id,
                 Account__c =  vAcc.Id));
                 
                 try {                   
                   insert qr;
                   
                   //Insert Attachments if any
                     if (email.binaryAttachments != 
                     null && email.binaryAttachments.size() > 0)
                     {
                     for (integer i = 0 ; i < email.binaryAttachments.size() ; i++)
                     {
                     Attachment aB = new Attachment(ParentId = qr[0].Id,
                     Name = email.binaryAttachments[i].filename,
                     Body = email.binaryAttachments[i].body);
                     insert aB;
                     }
                     }
         
                     if (email.textAttachments != 
                     null && email.textAttachments.size() > 0)
                     {
                     for (integer i = 0 ; i < email.textAttachments.size() ; i++)
                     {
                     Attachment aT = new Attachment(ParentId = qr[0].Id,
                     Name = email.textAttachments[i].filename,
                     Body = Blob.valueOf(email.textAttachments[i].body));
                     insert aT;
                     }
                     }
                   
                   } catch (System.DmlException e)
                   { result.success = false;
                     result.message = 'failed to attach to account.';
                   System.debug('ERROR: Not able to create Quote Request: ' + e);
                 } 
                 
                     // Send return email to sender confirming receipt of request
                     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                     mail.setToAddresses( new String[] { emailAddress });
                     mail.setSubject('Your quote request has been received');
                     mail.setPlainTextBody('Thank you for submitting your request. { existing contact }. Your Account Manager will be in touch shortly to discuss the details of your request.');
                     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
                     
                 
 

Hoping to get some guidance and maybe lift the fog I seem to be in with understanding test methods for Apex Classes. I have gotten the test methods for triggers down fairly well but am still struggling with getting coverage on my classes that query records and return them to visualforce pages dynamically. I have 3 classes/VF pages that are plaguing me on coverage at the moment. The first is listed below. Any examples and/or recommendations would be appreciated.

 

Lead Inbox:

This is a Class and VF page that we created as a home page component. It returns and displays unread leads for our users filters by lead source so that they can be aware of new Leads assigned to them that need their attention. The class and page is designed to get the leads and also return a count of how many unread leads they have. I have used sharing on this class as the lists generated need to be specific to the rep or manager viewing the inbox on their homepage. I also have a reference to a sorting class on here that allows the reps to sort the list in the VF page.

 

Here's the class. The code works perfectly but I currently have 0% coverage as I have no test method on it.

 

public with sharing class LeadInbox {


 
public PageReference ViewData() {
return null;
}

List<Lead> rqs;
public String sortField {get; set;}
public String previousSortField {get; set;}

 public List<Lead> getLead() {

  if(rqs == null){
  rqs = [select Id, Name, Company, LeadSource, Status, OwnerId, CreatedDate from Lead 
  
  Where (
  (LeadSource='Partner Referral'
   OR
  LeadSource='Employee Referral'
   OR
  LeadSource='External Referral Program'
   OR
  LeadSource='Management Networking'
   OR
  LeadSource='Sales Inquiry (Web)'
   OR
  LeadSource='Telemarketing'
  )

  AND
  IsUnreadByOwner = TRUE
  AND
  Status != 'Disqualified')
  ORDER BY Lead.CreatedDate Desc
  Limit 4 ];
  }
  return  rqs;
 }


public void doSort(){
        String order = 'asc';
        
        /*This checks to see if the same header was click two times in a row, if so 
        it switches the order.*/
        if(previousSortField == sortField){
            order = 'desc';
            previousSortField = null;
        }else{
            previousSortField = sortField;
        }
       
        //To sort the table we simply need to use this one line, nice!
        superSort.sortList(rqs,sortField,order);
    }


public Integer getUnreadLeads() {

return [

select count() from Lead

where (IsConverted = False
 AND 
  IsUnreadByOwner = TRUE
 AND
  Status != 'Disqualified'
 AND
  (LeadSource ='Partner Referral'
   OR
  LeadSource='Employee Referral'
   OR
  LeadSource='External Referral Program'
   OR
  LeadSource='Management Networking'
   OR
  LeadSource='Sales Inquiry (Web)'
   OR
  LeadSource='Telemarketing'
  )

];

}

 

and the VF page: It works perfectly as well. :) 

 

<apex:page cache="true" controller="LeadInbox" tabStyle="Lead" showHeader="false">
<html>

<body>

<apex:form id="test">
  <apex:pageblock title="">

    <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>     
             <td width="13%" height="120">
              
                 <table>
                 
 <tr><td align="center"><font size="5" color="#0000CC"><a href="/00Q?fcf=00B30000005veVy" target="_parent"><b>{!UnreadLeads}</b></a></font></td></tr>                
 <tr><td align="center"> <font size="3" color="#0000CC">Unread Referral Leads</font></td></tr>
                    
                 </table>       
            </td>
    
            <td width="87%">
        <apex:pageBlockTable value="{!Lead}" var="L" id="table">
                                               
            <apex:column >
                <apex:facet name="header">Action</apex:facet>
                <apex:outputLink title="" value="/{!L.id}" target="_blank" style="font-weight:bold">View</apex:outputLink>&nbsp;        
            </apex:column>    
            
           <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Lead.Fields.Name.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Name" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!L.Name}"/>
                </apex:column>

            
           <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Lead.Fields.Company.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Company" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!L.Company}"/>
                </apex:column>
            
            <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Lead.Fields.LeadSource.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="LeadSource" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!L.LeadSource}"/>
                </apex:column>
            
            <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Lead.Fields.Status.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Status" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!L.Status}"/>
                </apex:column>
            
            <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Lead.Fields.OwnerId.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="OwnerId" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!L.OwnerId}"/>
                </apex:column>
            
            <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Lead.Fields.CreatedDate.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="CreatedDate" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!L.CreatedDate}"/>
                </apex:column>
            
        </apex:pageBlockTable>
        
      </td>
      
     </tr>   
    </table>    
        
  </apex:pageblock>
    
</apex:form>

</body>
</html>
</apex:page>

Any help on developing a test method on this would be appreciated. Its just a query and page render so I know its can't be THAT complicated. I have read through all the documentation on writing test methods and something about it for classes just doesn't make sense to me.

 

Hi all!

 

I have only one or two weeks experience with Apex and Visualforce, and no coding experience, so please bear with me.

I altered the MassEdit visualforce page to the specifications of my supervisor, but we now we need to be able to sort the fields.

Having no experience with Apex or Java or anything, I was hoping I could sort of copy/paste the code from this Sorting Tables tutorial and replace the components that made sense.  It's not really working out.

 

Not being in Developer Edition, I'm having to use the Force.com IDE through Eclipse.

Here's the class:

 

 

public with sharing class massEditSort {

    List<Product_Forecasting__c> fcs;
    public String sortField {get; set;}
    public String previousSortField {get; set;}
    
    public List<Product_Forecasting__c> getFcs() {
        if(fcs == null){
            fcs = [select Forecast_For_Month__c, Style__c, Color_Code__c, Product_Description__c, Color__c, LQ2__c, Forecasted_Units_Q2_c__c, LQ3__c, Forecasted_Units_Q3_2010__c, Forecasted_Units_Q4_2010__c from Product_Forecasting__c];
        }
        return fcs;
    }
    
    public void doSort(){
        String order = 'asc';
        
        /*This checks to see if the same header was click two times in a row, if so 
        it switches the order.*/
        if(previousSortField == sortField){
            order = 'desc';
            previousSortField = null;
        }else{
            previousSortField = sortField;
        }
       
        //To sort the table we simply need to use this one line, nice!
        superSort.sortList(fcs,sortField,order);
    }
}

 

 

Now when I try to save this, I get an error that reads: Average test coverage across all Apex Classes and Triggers is 74%, at least 75% test coverage is required

And also the error: Save error: Method does not exist or incorrect signature: superSort.sortList(LIST:SOBJECT:Product_Forecasting__c, String, String)

 

Here is the Visualforce Page I am trying to get to work with this controller:

 

 

<apex:page controller="massEditSort">
    <apex:form >
        
	<apex:pageBlock >
		Note: All modifications made on the page will be lost if Cancel button is clicked without clicking the Save button first.<br />
		<p style="color:red"><u>Do not click the "Agree With Forecast" checkbox if you update or change any other field.</u></p>
	</apex:pageBlock>
	<apex:pageBlock >
	<apex:pageBlockButtons >
		<apex:commandButton value="Save" action="{!save}"/>
		<apex:commandButton value="Cancel" action="{!cancel}"/>
	</apex:pageBlockButtons>            

	<apex:pageBlockTable value="{!selected}" var="o" id="table">
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Product_Forecasting__c.Fields.Forecast_For_Month__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Forecast For Month:" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!o.Forecast_For_Month__c}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Product_Forecasting__c.Fields.Style__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Style#" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!o.Style__c}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Product_Forecasting__c.Fields.Color_Code__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Color Code" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!o.Color_Code__c}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Product_Forecasting__c.Fields.Product_Description__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Product Description" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!o.Product_Description__c}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Product_Forecasting__c.Fields.Color__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Color" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!o.Color__c}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Product_Forecasting__c.Fields.LQ2__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="LQ2 2009" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!o.LQ2__c}"/>
                </apex:column>
                 <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Product_Forecasting__c.Fields.Forecasted_Units_Q2_c__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Forecasted Units Q2 2010" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:inputField value="{!o.Forecasted_Units_Q2_c__c}"/>
                </apex:column>
		 <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Product_Forecasting__c.Fields.LQ3__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="LQ3 2009" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!o.LQ3__c}"/>
                </apex:column>
		 <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Product_Forecasting__c.Fields.Forecasted_Units_Q3_2010__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Forecasted Units Q3 2010" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:inputField value="{!o.Forecasted_Units_Q3_2010__c}"/>
                </apex:column>
		 <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Product_Forecasting__c.Fields.LQ4__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="LQ4 2009" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!o.LQ4__c}"/>
                </apex:column>
		 <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="{!$ObjectType.Product_Forecasting__c.Fields.Forecasted_Units_Q4_2010__c.Label}" action="{!doSort}" rerender="table">
                            <apex:param name="sortField" value="Forecasted Units Q4 2010" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:inputField value="{!o.Forecasted_Units_Q4_2010__c}"/>
                </apex:column>
		<apex:column headerValue="Agree With Forecast">
			<apex:inputField value="{!a.Agree_With_Forecast__c}"/>
		</apex:column>
		<apex:column headerValue="Comments on Style Forecast">
			<apex:inputTextarea value="{!a.Comments_on_Style_Forecast__c}" style="width:160px"/>
		</apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

Any help will be greatly appreciated!

I understand if it's one of those "This guy has no idea what's going on, and it would be way too much work to fix this garbage for him." :)

 

 

  • May 13, 2010
  • Like
  • 0