• AngiB.ax1285
  • NEWBIE
  • 0 Points
  • Member since 2012

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

Hello all,

 

We have a Managed Application with a page that works off of a custom object we have built.

(Actually we have 2 managed applications that have virtually identical pages - one has been released for about 1 year now and one is pending security review)

BOTH have the identical problem.

 

On these pages depending on the choice the user makes with one field (drop down list) the page will render different lookup fields for them to choose from.

So if I choose 'User' then a User object lookup appears

If I choose 'Account' then an Account object lookup appears

If I choose 'Contact' then a Contact object lookup appears

etc.

 

So we are making use of the 'rendered' attribute on the apex:inputfield on the page to show/hide as required.

 

This works PERFECTLY in our development org as well as many other orgs on which our customers have it installed.

EXCEPT any org that is running PROFESSIONAL EDITION.

 

The VF Page and Controller full code is included in full below.

 

We have read EVERYTHING we can get our hands on about PROFESSIONAL EDITION and we cannot find any reasonable explanation why this code will work for everyone else but not in PE.

 

Any assistance anyone can lend us in unravelling this mystery would be GREATLY appreciated.

Thanks

AngiB

 

---------------------------

VF Page:

 

<apex:page controller="Assign_Equipment" tabStyle="Equipment__c">

<style type="text/javascript">
function DisableLookUp()
{
DisableLookUp();
return false;
}
</style>
  <apex:form id="form">
  <apex:actionFunction action="{!DisableLookUp}" name="DisableLookUp" reRender="form"/>
    <apex:pageBlock >
    <apex:pageBlockSection title="Assign Equipment" columns="1">
    <apex:outputField value="{!obj.Name}"/>
    <apex:outputField value="{!obj.Equipment_Tag__c}"/>
    <!--apex:outputField value="{!obj.Assigned_Type__c}"/>-->
    <apex:outputField value="{!obj.Assigned_Name__c}"/>     
     </apex:pageBlockSection>
     <apex:pageBlockSection title="Assign Equipment" columns="8" id="pageBlock">
     <table id="tab1">
     <tr id="row1">
     <td id="col1">
     <apex:inputField value="{!obj1.Assigned_Date__c}" required="true"/>
     </td>
     </tr>
     <tr id="row2">
     <td id="col1">
     <apex:inputField value="{!obj.Assigned_Type__c}" id="Assign" required="true" onchange="DisableLookUp();"/> 
     <apex:inputField value="{!obj1.Assigned_User__c}" id="User" rendered="{!RenderUser}"/>
     <apex:inputField value="{!obj1.Assigned_Account__c}" id="Acc" rendered="{!RenderAccount}"/>
     <apex:inputField value="{!obj1.Assigned_Contact__c}" id="Cont" rendered="{!RenderContact}"/>
     <apex:inputField value="{!obj1.Assigned_Opportunity__c}" id="Opp" rendered="{!RenderOpportunity}" />
     </td>
     </tr>
     </table>
     </apex:pageBlockSection>
    <apex:pageBlockSection Columns="3">
     <apex:commandButton value="Assign" action="{!Assign}"/>
     <apex:commandButton value="Cancel" action="{!Cancel}"/>
    </apex:pageBlockSection>    

    </apex:pageBlock>  
  </apex:form>
  <script type="Text/javascript">
 var hd_value = {!ActiveChk}; //For Active CheckBox True
 //alert(hd_value);
 var curr_url = document.location.href;
 //alert(curr_url);
 var temp = new Array();
 temp = curr_url.split('=');
 var EQ_id = temp[1];
 //alert(EQ_id);
 var E_ID;
 if(EQ_id.indexOf('&')> -1)
 {
     temp = EQ_id.split('&');
     E_ID = temp[0];
 }
 else
 {
     E_ID = EQ_id;
 }
 //alert(E_ID);
 var temp1 = new Array();
 temp1 = curr_url.split('apex');
 var int_url = temp1[0];
 //alert(int_url);
 var fnl_url = int_url+E_ID;
 //alert(fnl_url);
 if(hd_value == false)
 {
     alert("You cannot Assign Inactive Equipment . Please change the Equipment to Active.");
     window.location = fnl_url;
 }
  </script>
</apex:page>

 

Controller Class:

public with sharing class Assign_Equipment {

    public ELTON__Equipment__c obj {get;set;}
    public ELTON__Equipment__c obj1 {get;set;}
    public ELTON__Equipment_Assignment__c objEAOld {get;set;}
    public string EId;
    public User us{get;set;}
    public ELTON__Equipment__c objEquipment{get;set;}
    public User objUser{get;set;}
    public Account objAcc{get;set;}
    public Contact objCon{get;set;} 
    public Opportunity objOpp{get;set;} 
    List<ELTON__Equipment_Assignment__c> lstEA = new List<ELTON__Equipment_Assignment__c>();       
    List<ELTON__Equipment_Assignment__c> EAsToUpdate = new List<ELTON__Equipment_Assignment__c>();       

    //Variable Declaration
    public boolean RenderUser {get; set;}
    public boolean RenderAccount {get; set;}
    public boolean RenderContact {get; set;}
    public boolean RenderOpportunity {get; set;}
    public string Name{get;set;}
    public string Type{get;set;}
    public boolean ActiveChk {get;set;}

    public Assign_Equipment()
    {
      EId = ApexPages.CurrentPage().getParameters().get('id');
      Init();
    }

    public void Init()
    {
        try
        {
           RenderUser = false;
           RenderAccount = false;
           RenderContact = false;
           RenderOpportunity = false;
           obj1 = new ELTON__Equipment__c();
           obj = [select Id,Name,ELTON__Active__c,ELTON__Equipment_Tag__c,
             ELTON__Assigned_Account__c,ELTON__Assigned_Address__c,ELTON__Assigned_City__c,ELTON__Assigned_Contact__c,
             ELTON__Assigned_Country__c,ELTON__Assigned_Date__c,ELTON__Assigned_Email__c,ELTON__Assigned_Mobile__c,
             ELTON__Assigned_Name__c,ELTON__Assigned_Opportunity__c,ELTON__Assigned_Other_Phone__c,ELTON__Assigned_Phone__c,
             ELTON__Assigned_State_Province__c,ELTON__Assigned_Type__c,ELTON__Assigned_User__c,ELTON__Assigned_Zip_Postal_Code__c   
             from ELTON__Equipment__c where Id=:EId];  
      
           if(obj.ELTON__Assigned_Name__c!=null){Name = obj.ELTON__Assigned_Name__c;}
           if(obj.ELTON__Assigned_Type__c!=null){Type = obj.ELTON__Assigned_Type__c;}
                      
           ActiveChk = obj.ELTON__Active__c;
           if(Test.isRunningTest())  
           {/* Do Nothing */}
           else 
           {obj.ELTON__Assigned_Type__c=null;
           }
        }
        catch(Exception ee)
        {
            
        }
    }
    
    public void DisableLookUp()
    {
        try
        {
            if(obj.ELTON__Assigned_Type__c == 'User')
            {
            RenderUser = true;
            RenderAccount = false;
            RenderContact = false;
            RenderOpportunity = false;
            }
            else if(obj.ELTON__Assigned_Type__c == 'Account')
            {
            RenderAccount = true;
            RenderUser = false;
            RenderContact = false;
            RenderOpportunity = false;
            }
            else if(obj.ELTON__Assigned_Type__c == 'Contact')
            {
            RenderContact = true;
            RenderAccount = false;
            RenderUser = false;
            RenderOpportunity = false;
            }
            else if(obj.ELTON__Assigned_Type__c == 'Opportunity')
            {
            RenderOpportunity = true;
            RenderAccount = false;
            RenderUser = false;
            RenderContact= false;
            }
            else
            {
            RenderAccount =false;
            RenderUser = false;
            RenderContact = false;
            RenderOpportunity = false;
            }
        }
        catch(Exception ee)
        {
            
        }
    }
    
    public PageReference Assign()
    {
    try
      {

      //Testing stuff
      if(Test.isRunningTest())
      {
         obj1.ELTON__Assigned_Type__c = obj.ELTON__Assigned_Type__c;
         obj1.ELTON__Assigned_User__c = obj.ELTON__Assigned_User__c;            
         obj1.ELTON__Assigned_Account__c = obj.ELTON__Assigned_Account__c;
         obj1.ELTON__Assigned_Opportunity__c = obj.ELTON__Assigned_Opportunity__c;
         obj1.ELTON__Assigned_Contact__c = obj.ELTON__Assigned_Contact__c;                                    
         obj1.ELTON__Assigned_Date__c = obj.ELTON__Assigned_Date__c;            
          
      }

      // null out obj fields 
      obj.ELTON__Assigned_Account__c = null;
      obj.ELTON__Assigned_Address__c = '';
      obj.ELTON__Assigned_City__c = '';
      obj.ELTON__Assigned_Contact__c = null;
      obj.ELTON__Assigned_Country__c = '';
      obj.ELTON__Assigned_Email__c = '';
      obj.ELTON__Assigned_Mobile__c = '';
      obj.ELTON__Assigned_Opportunity__c = null;
      obj.ELTON__Assigned_Other_Phone__c = '';
      obj.ELTON__Assigned_Phone__c = '';
      obj.ELTON__Assigned_State_Province__c = '';
      obj.ELTON__Assigned_User__c = null;
      obj.ELTON__Assigned_Zip_Postal_Code__c = '';

      // set fields from page
      obj.ELTON__Assigned_Date__c =obj1.ELTON__Assigned_Date__c;     
      
      if(obj.ELTON__Assigned_Type__c == 'Opportunity')
      {
             if(obj1.ELTON__Assigned_Opportunity__c != null)
             {
              objOpp = [select id,Name from Opportunity 
                                   where Id=:obj1.ELTON__Assigned_Opportunity__c];
             
              if(objOpp.Name!=null)
              {
                  obj.ELTON__Assigned_Name__c=objOpp.Name;
              }

              obj.ELTON__Assigned_Opportunity__c = obj1.ELTON__Assigned_Opportunity__c;
             
               }
      }
      else if(obj.ELTON__Assigned_Type__c == 'User')
      {
     
         if(obj1.ELTON__Assigned_User__c!=null)
         {
          objUser = [select id,Name,MobilePhone,Email,Phone,Street,State,PostalCode,Country,City,Department from User where Id=:obj1.ELTON__Assigned_User__c];

          if(objUser.Street == null){objUser.Street = '';}
          if(objUser.City == null){objUser.City = '';}
          if(objUser.Country == null){objUser.Country = '';}
          if(objUser.Email == null){objUser.Email = '';}
          if(objUser.MobilePhone == null){objUser.MobilePhone = '';}
          if(objUser.Phone == null){objUser.Phone = '';}                    
          if(objUser.State == null){objUser.PostalCode = '';}
          if(objUser.PostalCode == null){objUser.PostalCode = '';}                    
                                                            
          obj.ELTON__Assigned_Address__c = objUser.Street;          
          obj.ELTON__Assigned_City__c = objUser.City;
          obj.ELTON__Assigned_Country__c = objUser.Country;
          obj.ELTON__Assigned_Email__c=objUser.Email;
          obj.ELTON__Assigned_Mobile__c =objUser.MobilePhone;
          obj.ELTON__Assigned_Name__c=objUser.Name;
          obj.ELTON__Assigned_Other_Phone__c='';
          obj.ELTON__Assigned_Phone__c =objUser.Phone;
          obj.ELTON__Assigned_State_Province__c = objUser.State;
          obj.ELTON__Assigned_Zip_Postal_Code__c = objUser.PostalCode;
          
          obj.ELTON__Assigned_User__c = obj1.Elton__Assigned_User__c;
          
         }
     }
     else if(obj.ELTON__Assigned_Type__c == 'Account')
     {
         if(obj1.ELTON__Assigned_Account__c!=null)
         {
          objAcc = [select id,Name,Phone,BillingCity,BillingCountry ,BillingPostalCode,BillingState,BillingStreet from Account where Id=:obj1.ELTON__Assigned_Account__c];

          if(objAcc.BillingStreet == null){objAcc.BillingStreet = '';}
          if(objAcc.BillingCity == null){objAcc.BillingCity = '';}          
          if(objAcc.BillingCountry == null){objAcc.BillingCountry = '';}    
          if(objAcc.Phone == null){objAcc.Phone = '';}      
          if(objAcc.BillingState == null){objAcc.BillingState = '';}          
          if(objAcc.BillingPostalCode == null){objAcc.BillingPostalCode = '';}          
          
          obj.ELTON__Assigned_Address__c = objAcc.BillingStreet;          
          obj.ELTON__Assigned_City__c = objAcc.BillingCity;
          obj.ELTON__Assigned_Country__c = +' '+objAcc.BillingCountry;
          obj.ELTON__Assigned_Email__c='';
          obj.ELTON__Assigned_Mobile__c = '';
          obj.ELTON__Assigned_Name__c = objAcc.Name;
          obj.ELTON__Assigned_Other_Phone__c='';
          obj.ELTON__Assigned_Phone__c =objAcc.Phone;
          obj.ELTON__Assigned_State_Province__c = objAcc.BillingState;
          obj.ELTON__Assigned_Zip_Postal_Code__c = +' '+objAcc.BillingPostalCode;
          
          obj.ELTON__Assigned_Account__c = obj1.ELTON__Assigned_Account__c;
         }
     }
     else if(obj.ELTON__Assigned_Type__c == 'Contact')
     {
         if(obj1.ELTON__Assigned_Contact__c!=null)
         {
          objCon= [select id,Name,Phone,Email,OtherPhone,MobilePhone,Department,MailingCity,MailingCountry,MailingPostalCode,MailingState,MailingStreet from Contact where Id=:obj1.ELTON__Assigned_Contact__c];

          if(objCon.MailingStreet == null){objCon.MailingStreet = '';}
          if(objCon.MailingCity == null){objCon.MailingCity = '';}
          if(objCon.MailingCountry == null){objCon.MailingCountry = '';}
          if(objCon.Email == null){objCon.Email = '';}
          if(objCon.MobilePhone == null){objCon.MobilePhone = '';}
          if(objCon.OtherPhone == null){objCon.OtherPhone = '';}
          if(objCon.Phone == null){objCon.Phone = '';}
          if(objCon.MailingState == null){objCon.MailingState = '';}
          if(objCon.MailingPostalCode == null){objCon.MailingPostalCode = '';}          
                                                            
          obj.ELTON__Assigned_Address__c = objCon.MailingStreet;
          obj.ELTON__Assigned_City__c = objCon.MailingCity;
          obj.ELTON__Assigned_Country__c = objCon.MailingCountry;
          obj.ELTON__Assigned_Email__c = objCon.Email;
          obj.ELTON__Assigned_Mobile__c =objCon.MobilePhone;
          obj.ELTON__Assigned_Name__c=objCon.Name;
          obj.ELTON__Assigned_Other_Phone__c =objCon.OtherPhone;
          obj.ELTON__Assigned_Phone__c=objCon.Phone;
          obj.ELTON__Assigned_State_Province__c = objCon.MailingState;
          obj.ELTON__Assigned_Zip_Postal_Code__c = objCon.MailingPostalCode;

          obj.ELTON__Assigned_Contact__c = obj1.ELTON__Assigned_Contact__c;
          
         }
    }

    // If the Assigned_Type was left null then basically do no updates
    // Not the most elegant of solutions but easy enough to explain
    if(obj.ELTON__Assigned_Type__c != null)
    {
       // Update the Equipment Object
       update obj;

       // Update previous Equipment Assignment Record - Any without a return date get the Assigned_Date as returned Date
       List<ELTON__Equipment_Assignment__c> lstEA = 
          [select Id,ELTON__Account__c,ELTON__Address__c,ELTON__City__c,ELTON__Contact__c,
          ELTON__Country__c,ELTON__Assigned_Date__c,ELTON__Email__c,ELTON__Mobile__c,
          ELTON__Name__c,ELTON__Opportunity__c,ELTON__Other_Phone__c,ELTON__Phone__c,
          ELTON__State_Province__c,ELTON__Assigned_Type__c,ELTON__User__c,ELTON__Zip_Postal_Code__c,
          ELTON__Return_Date__c 
          from ELTON__Equipment_Assignment__c where ELTON__Equipment__c=:EId and ELTON__Return_Date__c=null]; 
    
      if (lstEA.size() > 0)
      {             
      for(ELTON__Equipment_Assignment__c objEAOld: lstEA)
         {                
         objEAOld.ELTON__Return_Date__c=obj.Assigned_Date__c;
         EAsToUpdate.add(objEAOld);             
         }             
      update EAsToUpdate;            
      }

      // Create Equipment Assignment Record for new Assignment
      ELTON__Equipment_Assignment__c objEA = new ELTON__Equipment_Assignment__c ();
      objEA.ELTON__Account__c=obj.ELTON__Assigned_Account__c;
      objEA.ELTON__Address__c=obj.ELTON__Assigned_Address__c;
      objEA.ELTON__Assigned_Date__c=obj.ELTON__Assigned_Date__c;
      objEA.ELTON__Assigned_Type__c = obj.ELTON__Assigned_Type__c;
      objEA.ELTON__City__c=obj.ELTON__Assigned_City__c;
      objEA.ELTON__Contact__c=obj.ELTON__Assigned_Contact__c;
      objEA.ELTON__Country__c=obj.ELTON__Assigned_Country__c;
      objEA.ELTON__Email__c=obj.ELTON__Assigned_Email__c;
      objEA.ELTON__Equipment__c = EId;
      objEA.ELTON__Mobile__c=obj.ELTON__Assigned_Mobile__c;
      objEA.ELTON__Name__c=obj.ELTON__Assigned_Name__c;
      objEA.ELTON__Opportunity__c = obj.ELTON__Assigned_Opportunity__c;
      objEA.ELTON__Other_Phone__c=obj.ELTON__Assigned_Other_Phone__c;
      objEA.ELTON__Phone__c=obj.ELTON__Assigned_Phone__c;
      objEA.ELTON__Return_Date__c=null;
      objEA.ELTON__State_Province__c=obj.ELTON__Assigned_State_Province__c;
      objEA.ELTON__User__c=obj.ELTON__Assigned_User__c;
      objEA.ELTON__Zip_Postal_Code__c=obj.ELTON__Assigned_Zip_Postal_Code__c;

      insert objEA ; 
    } 
   } 
 catch(Exception ee)
 {
 
 }
PageReference page1= new ApexPages.Standardcontroller(obj).view();
                page1.setRedirect(true);
                return page1;
   
}

    public PageReference Cancel(){
         
         PageReference Page= new PageReference('/'+EId);
                Page.setRedirect(true);
                return Page;
    }

 

 

 

I have the following trigger.

 

To summarize: when a record is inserted into the object of this trigger if there is a related parent record it looks up and sets 2 fields on that record.

 

trigger SetDJELFromDepreciationSchedule on Depreciation_Journal_Entry_Line__c (Before Insert) 
{      
     for(AcctSol__Depreciation_Journal_Entry_Line__c objDJEL : Trigger.new)    
     {      
        if (objDJEL.AcctSol__Depreciation_Schedule__c != null)
        {          
           AcctSol__Depreciation_Schedule__c TheDS = 
              [select AcctSol__Accum_Depr_GL_Acct__c, AcctSol__Depr_Exp_GL_Acct__c from         
              AcctSol__Depreciation_Schedule__c where Id  
              = :ObjDJEL.AcctSol__Depreciation_Schedule__c];                        
           if(objDJEL.AcctSol__Credit_GL_Account__c == null)          
           {              
            objDJEL.AcctSol__Credit_GL_Account__c = TheDS.AcctSol__Accum_Depr_GL_Acct__c;          
           }          

           if(objDJEL.AcctSol__Debit_GL_Account__c == null)          
           {              
            objDJEL.AcctSol__Debit_GL_Account__c = TheDS.AcctSol__Depr_Exp_GL_Acct__c;          
           }     
      }    
  } 
}

 This code was written back in May and I can confirm that it works. But now all of a sudden the Force.com Security Source Code scanner is rejecting it.

 

CWE ID 10540

This rule identifies Apex where SOQL (salesforce.com Object Query Language) or SOSL (salesforce.com Object Search

Language) statements are executed within a loop.

 

I would ask how this code passed a test a couple weeks ago and now fails but I guess I don't really care - 'tis not my business to understand the rules - just to obey them :-)

 

I was wondering if someone could show me the easiest way to solve this problem ?

(I have some theories but I am also very new at this so I want to check with someone with more experience)

 

Thanks in Advance for your Assistance,

AngiB

 

I have created a VF page and controller apex as follows:

 

The Apex Class.... 

public class GenPortableFAData    

{

    public GenPortableFAData(ApexPages.StandardController controller) {      

    }

    public Fixed_Asset__c[] getGenPortableFAData()

    {

    string CId;        

    CId = ApexPages.CurrentPage().getParameters().get('id');        

    Configuration__c [] CFList;        

    Fixed_Asset__c [] FAList;          

    CFList = [select Configuration__c.Id from Configuration__c where Configuration__c.Id = :CId];        

    FAList = [select Fixed_Asset__c.Id, Fixed_Asset__c.Asset_Tag__c, 

        Fixed_Asset__c.Description__c,   Fixed_Asset__c.Location_Info__c, Fixed_Asset__c.Condition__c

        from Fixed_Asset__c where AcctSol__Fixed_Asset__c.Configuration__r.Id = :CId];          

     return FAList;      

    }    

}

 

The VF Page:

 

<apex:page standardController="Configuration__c" extensions="GenPortableFAData" contentType="text/tab-separated-values#S01_Data.txt">

<apex:repeat value="{!GenPortableFAData}" var="fixedasset">

{!fixedasset.Id}{!fixedasset.Asset_Tag__c}{!fixedasset.Description__c}{!fixedasset.Location_Info__c}{!fixedasset.Condition__c}

</apex:repeat>

</apex:page>

 

It is working fine in that the records that I want to appear are appearing - it is creating the text file with the name I provided and it's all good... somewhat.

 

But I have a few problems....

1. I need the columns to be delimited by TABs and I don't know what contentType to use ?

2. The output file contains a single blank line at the top of the file and then the data records - how do I lose the blank line ?

3. I need to add a last column for each record that is the current system date/time (e.g System.today()) ?

 

Any assistance that you could provide would be GREATLY appreciated !!

The Newest of Newbies

AngiB

 



I have a test class for a fairly large class that can be run either via a button or as a scheduled task

(not related to my previous post of today about the || operator – different class)

 

Whether the code is run via a button or scheduled it does NOT have any external input – it just evaluates data in various source custom objects and creates one or more records in a set of 3 different destination custom objects (a parent, child and grandchild structure based on the source objects).

 

I did not write ANY of the code involved – someone else was contracted to do that. But I am attempting to learn by example and this example brings various questions to my mind. 

 

So my test class is….

 

@isTest
private class Test_ScheduleClass_DepCalculForCalPeriod {

    static testMethod void myUnitTest() {
ScheduleClass_DepCalculationForCalPeriod objDepForCalPeriod = new ScheduleClass_DepCalculationForCalPeriod();
        objDepForCalPeriod.execute(null);
        system.assert(true);
    }
}

 

The class being tested – called ScheduleClass_DepCalculationForCalPeriod() contains:

 

global with sharing class ScheduleClass_DepCalculationForCalPeriod implements Schedulable
{
    global void execute(SchedulableContext SC)
    {
        Cls_DepCalculationForCalPeriod ObjDCP = new Cls_DepCalculationForCalPeriod();
    }
}

 

Last but not least the class called within that -  Cls_DepCalculationForCalPeriod() is the very large class that contains the actual logic. It is seriously too big to post – and using conventional testing methods we are fairly certain that it does what it needs to do.

 

But I am totally IN LOVE with test classes and I think this methodology is AMAZING – so I am on a quest to make sure test classes ACTUALLY  test properly and thoroughly.

 

Question 1:

How is it possible that I am getting a 60% Code coverage on Cls_DepCalculationForCalPeriod?

I guess re-wording that question it seems to me that this test class does NOTHING - how can it be giving 60% ? 

 

Question 2:

Should my test class not be setting up a series of records in my ‘source’ objects and then somehow looking at the records generated in the destination objects and making sure they are what would be expected based on the test data I created in my test class?

 

Question 3 (a and b):

Along that train of thought how can I write a test class that IGNORES all existing data? What I mean is Cls_DepCalculationForCalPeriod will look at EVERY record in the source tables and generate data that will potentially include both any test data I set up as well as any data that happens to be sitting in the org at the time the test runs. How can I force a ‘pristine’ data set for the test?

 

Any assistance you can provide would be greatly appreciated.

 

AngiB 

 

 

 

I have an Apex class that is run either via a button on a page or as a scheduled task.

 

It is long and does many things - I am just having a TINY problem with it.

I fear that what I am writing below isn't going to make ANY sense to anyone - it is very hard to describe - but I will try my best....

 

My class has what I think is called a property called 'IsTest'.

When the class is called via the page button or scheduled task IsTest is set to FALSE.

When the class is called from inside it's test class IsTest is set to TRUE.

The purpose of IsTest is to allow the test class to ACTUALLY execute all the code in the class since it is very date sensitive.

 

 

A variable called 'Today' exists - in the example I am running I am choosing that to be a specific date (Feb 29 2012).

 

The code is looping around through the records in a custom object - some of them are Monthly and some Annually....

 

 

if(obj.AcctSol__Depreciation_Schedule__r.AcctSol__Depreciation_cycle__c !='' && obj.AcctSol__Depreciation_Schedule__r.AcctSol__Depreciation_cycle__c == 'Monthly')

                        {

                            try{

                            

                            Date endDate = today.addMonths(1);

                            endDate = endDate.toStartOfMonth();

                            endDate = endDate.addDays(-1);

                            if(endDate == today || isTest == true)

                            {

                           // a bunch of stuff happens 

                        }

                    }

                }

else if(obj.AcctSol__Depreciation_Schedule__r.AcctSol__Depreciation_cycle__c !='' && obj.AcctSol__Depreciation_Schedule__r.AcctSol__Depreciation_cycle__c == 'Annually')

                   {

                        try

                        {

                           

                            integer Curr_Year = today.year();

                            Date endDate_yr;

                            if(objconfg!=null && objconfg.Fiscal_year_start__c!=null)

                            {

  endDate_yr = Date.valueOf(Curr_Year+'-    '+objconfg.Fiscal_year_start__c+'-01');

                            endDate_yr = endDate_yr.addDays(-1);

                           

                           

                            }

                            if(today == endDate_yr || isTest == true)

                        {

                                                                  

                           // a bunch of stuff happens 

                        }

                    }

                 }

 

The problem is that when IsTest is FALSE and I set my date to be the proper one (Feb 29 2012) based on our data...

 

The first If statement duplicated below evalutes to TRUE and the code within that IF executes...

                            if(endDate == today || isTest == true)

 

BUT The second If statement duplicated below evalutes to FALSE and the code within that IF DOES NOT execute...

                            if(today == endDate_yr || isTest == true)

  

To confirm that the problem isn't with the date related to the data If I change that second IF to be:

                            if(today == endDate_yr)

 

Then the second If statement evaluates properly to TRUE and the code within that IF executes.

 

Based on that little experiment I am confident that the first component of the OR is evaluating to be TRUE.

 

So why is the code inside my second if not ever executing unless isTest is TRUE?

 

For the moment I have to leave the second if as if(today == endDate_yr) so that in a production situation the code actually works.

But this has dropped my code coverage from 83 down to 52 on this class so I would like to figure this out.

 

Not to mention clearly OR (||) is working differently than I think it does - it would be good to understand.

 

Thanks in advance for any assistance you can provide me (THE DEFINITION of an Apex Newbie)

 

We have a managed application and we are expanding a custom object that used to allow ONLY ONE RECORD to allow multiple records.

 

So that means when one of our customers gets the soon to be new version of the application I can GUARANTEE that they either have 0 or 1 record in the AcctSol__Configuration__c object.

 

We are also adding a lookup relationship to AcctSol__Fixed_Asset__c to AcctSol__Configuration__c and I that will be mandatory on all records.

 

I am nothing if not lazy so I was looking for the easiest way to get that lookup relationship populated and I came up with the following trigger on AcctSol__Configuration__c:

 

trigger MoreThanOneConfig on AcctSol__Configuration__c (Before Insert) {
 
List<AcctSol__Configuration__c> lstCF = [SELECT Id, Name FROM AcctSol__Configuration__c];
List<AcctSol__Fixed_Asset__c> lstFA = [SELECT Id, Name, AcctSol__Configuration__c FROM AcctSol__Fixed_Asset__c where AcctSol__Configuration__c=null];
List<AcctSol__Fixed_Asset__c> AssetsToUpdate = new List<AcctSol__Fixed_Asset__c>();
 
 if(Trigger.isInsert)
 {
   for(AcctSol__Configuration__c objFA : Trigger.new)
   {
         if (lstCF.size() == 1)
         {
             for(AcctSol__Fixed_Asset__c FA: lstFA)
             {
                FA.AcctSol__Configuration__c = lstCF.get(0).Id;
                AssetsToUpdate.add(FA);
             }
             update AssetsToUpdate;
         }
   }
 }
}



It appears to work.

So when you create the SECOND AcctSol__Configuration__c record this will set all EXISTING AcctSol__Fixed_Asset__c.Configuration__c lookups to the previously existing AcctSol__Configuration__c value.

On the 1st, 3rd, 4th etc records it will do nothing.

 

From a business logic point of view this is perfect.

 

My question is… is there anything wrong with this approach from a technical standpoint?

I have just added a trigger to our org on a custom object called Fixed_Asset__c.

The test class I built for the new trigger PASSES.

But when running another test class that causes updates to Fixed_Asset__c objects I get the following failure.

 

Error Message    System.LimitException: AcctSol:Too many SOQL queries: 101

Stack Trace         Trigger.AcctSol.InsuranceRollUpFixedAssets: line 61, column 1

 

My trigger code can be seen below.

 

I am VERY new to Force.com so undoubtedly have done something silly that is causing this. Any assistance that you could provide would be greatly Appreciated.

 

------

My Trigger Code:

 

trigger InsuranceRollUpFixedAssets on Fixed_Asset__c (after delete, after insert, after update, after undelete) {

 

    //Limit the size of list by using Sets which do not contain duplicate elements

 

    set<Id> InsuranceIds = new set<Id>();

  

    // Adding

    if(trigger.isInsert){

        for(Fixed_Asset__c p : trigger.new){

            InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

        }

    }

 

    // Updating

    if (trigger.isUpdate){

            for(Fixed_Asset__c p : trigger.new){

                InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

            }

            for(Fixed_Asset__c p : trigger.old){

                InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

            }

    }

 

    // Deleting

    if(trigger.isDelete){

        for(Fixed_Asset__c p : trigger.old){

            InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

        }

    }

 

    // Undeleting

    if(trigger.isUnDelete){

        for(Fixed_Asset__c p : trigger.new){

            InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

        }

    }

 

    //Map will contain one Insurance Id to one sum value

 

    map<Id,Double> InsuranceMap1 = new map <Id,Double>();

    map<Id,Double> InsuranceMap2 = new map <Id,Double>();

 

    List<aggregateResult> results = [select AcctSol__Insurance_Policy_LU__c,sum(AcctSol__Remaining_Orginal_Cost__c),sum(AcctSol__Original_Cost__c)

                                    from Fixed_Asset__c where AcctSol__Insurance_Policy_LU__c IN :InsuranceIds group by AcctSol__Insurance_Policy_LU__c];

    system.debug('Test**************'+results);

 

    //Produce a sum of Fixed_Asset__c and add them to the map

    //use group by to have a single Insurance Id with a single sum value

 

    for(AggregateResult q : results){

        InsuranceMap1.put((Id)q.get('AcctSol__Insurance_Policy_LU__c'),(Double)q.get('expr0'));

        system.debug('Test**************'+InsuranceMap1);

        InsuranceMap2.put((Id)q.get('AcctSol__Insurance_Policy_LU__c'),(Double)q.get('expr1'));    

    }

 

    List<Insurance__c> InsurancesToUpdate = new List<Insurance__c>();

 

    //Run the for loop on Insurance using the non-duplicate set of Insurance Ids

    //Get the sum value from the map and create a list of Insurances to update

 

    List<Insurance__c> first_o = [Select Id, Total_Asset_Book_Value__c,Total_Original_Cost__c from Insurance__c where Id IN :InsuranceIds];

 

    for(Insurance__c o : first_o){

        Double BookValueSum = (InsuranceMap1.get(o.Id) == null) ? 0:InsuranceMap1.get(o.Id); // if null value is zero

        o.Total_Asset_Book_Value__c = BookValueSum;

 

        Double OrigCostSum = (InsuranceMap2.get(o.Id) == null) ? 0:InsuranceMap2.get(o.Id); // if null value is zero

        o.Total_Original_Cost__c = OrigCostSum;

 

        InsurancesToUpdate.add(o);

    }

 

    update InsurancesToUpdate;

 

}

I have an Apex class that is run either via a button on a page or as a scheduled task.

 

It is long and does many things - I am just having a TINY problem with it.

I fear that what I am writing below isn't going to make ANY sense to anyone - it is very hard to describe - but I will try my best....

 

My class has what I think is called a property called 'IsTest'.

When the class is called via the page button or scheduled task IsTest is set to FALSE.

When the class is called from inside it's test class IsTest is set to TRUE.

The purpose of IsTest is to allow the test class to ACTUALLY execute all the code in the class since it is very date sensitive.

 

 

A variable called 'Today' exists - in the example I am running I am choosing that to be a specific date (Feb 29 2012).

 

The code is looping around through the records in a custom object - some of them are Monthly and some Annually....

 

 

if(obj.AcctSol__Depreciation_Schedule__r.AcctSol__Depreciation_cycle__c !='' && obj.AcctSol__Depreciation_Schedule__r.AcctSol__Depreciation_cycle__c == 'Monthly')

                        {

                            try{

                            

                            Date endDate = today.addMonths(1);

                            endDate = endDate.toStartOfMonth();

                            endDate = endDate.addDays(-1);

                            if(endDate == today || isTest == true)

                            {

                           // a bunch of stuff happens 

                        }

                    }

                }

else if(obj.AcctSol__Depreciation_Schedule__r.AcctSol__Depreciation_cycle__c !='' && obj.AcctSol__Depreciation_Schedule__r.AcctSol__Depreciation_cycle__c == 'Annually')

                   {

                        try

                        {

                           

                            integer Curr_Year = today.year();

                            Date endDate_yr;

                            if(objconfg!=null && objconfg.Fiscal_year_start__c!=null)

                            {

  endDate_yr = Date.valueOf(Curr_Year+'-    '+objconfg.Fiscal_year_start__c+'-01');

                            endDate_yr = endDate_yr.addDays(-1);

                           

                           

                            }

                            if(today == endDate_yr || isTest == true)

                        {

                                                                  

                           // a bunch of stuff happens 

                        }

                    }

                 }

 

The problem is that when IsTest is FALSE and I set my date to be the proper one (Feb 29 2012) based on our data...

 

The first If statement duplicated below evalutes to TRUE and the code within that IF executes...

                            if(endDate == today || isTest == true)

 

BUT The second If statement duplicated below evalutes to FALSE and the code within that IF DOES NOT execute...

                            if(today == endDate_yr || isTest == true)

  

To confirm that the problem isn't with the date related to the data If I change that second IF to be:

                            if(today == endDate_yr)

 

Then the second If statement evaluates properly to TRUE and the code within that IF executes.

 

Based on that little experiment I am confident that the first component of the OR is evaluating to be TRUE.

 

So why is the code inside my second if not ever executing unless isTest is TRUE?

 

For the moment I have to leave the second if as if(today == endDate_yr) so that in a production situation the code actually works.

But this has dropped my code coverage from 83 down to 52 on this class so I would like to figure this out.

 

Not to mention clearly OR (||) is working differently than I think it does - it would be good to understand.

 

Thanks in advance for any assistance you can provide me (THE DEFINITION of an Apex Newbie)

 

I have just added a trigger to our org on a custom object called Fixed_Asset__c.

The test class I built for the new trigger PASSES.

But when running another test class that causes updates to Fixed_Asset__c objects I get the following failure.

 

Error Message    System.LimitException: AcctSol:Too many SOQL queries: 101

Stack Trace         Trigger.AcctSol.InsuranceRollUpFixedAssets: line 61, column 1

 

My trigger code can be seen below.

 

I am VERY new to Force.com so undoubtedly have done something silly that is causing this. Any assistance that you could provide would be greatly Appreciated.

 

------

My Trigger Code:

 

trigger InsuranceRollUpFixedAssets on Fixed_Asset__c (after delete, after insert, after update, after undelete) {

 

    //Limit the size of list by using Sets which do not contain duplicate elements

 

    set<Id> InsuranceIds = new set<Id>();

  

    // Adding

    if(trigger.isInsert){

        for(Fixed_Asset__c p : trigger.new){

            InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

        }

    }

 

    // Updating

    if (trigger.isUpdate){

            for(Fixed_Asset__c p : trigger.new){

                InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

            }

            for(Fixed_Asset__c p : trigger.old){

                InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

            }

    }

 

    // Deleting

    if(trigger.isDelete){

        for(Fixed_Asset__c p : trigger.old){

            InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

        }

    }

 

    // Undeleting

    if(trigger.isUnDelete){

        for(Fixed_Asset__c p : trigger.new){

            InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

        }

    }

 

    //Map will contain one Insurance Id to one sum value

 

    map<Id,Double> InsuranceMap1 = new map <Id,Double>();

    map<Id,Double> InsuranceMap2 = new map <Id,Double>();

 

    List<aggregateResult> results = [select AcctSol__Insurance_Policy_LU__c,sum(AcctSol__Remaining_Orginal_Cost__c),sum(AcctSol__Original_Cost__c)

                                    from Fixed_Asset__c where AcctSol__Insurance_Policy_LU__c IN :InsuranceIds group by AcctSol__Insurance_Policy_LU__c];

    system.debug('Test**************'+results);

 

    //Produce a sum of Fixed_Asset__c and add them to the map

    //use group by to have a single Insurance Id with a single sum value

 

    for(AggregateResult q : results){

        InsuranceMap1.put((Id)q.get('AcctSol__Insurance_Policy_LU__c'),(Double)q.get('expr0'));

        system.debug('Test**************'+InsuranceMap1);

        InsuranceMap2.put((Id)q.get('AcctSol__Insurance_Policy_LU__c'),(Double)q.get('expr1'));    

    }

 

    List<Insurance__c> InsurancesToUpdate = new List<Insurance__c>();

 

    //Run the for loop on Insurance using the non-duplicate set of Insurance Ids

    //Get the sum value from the map and create a list of Insurances to update

 

    List<Insurance__c> first_o = [Select Id, Total_Asset_Book_Value__c,Total_Original_Cost__c from Insurance__c where Id IN :InsuranceIds];

 

    for(Insurance__c o : first_o){

        Double BookValueSum = (InsuranceMap1.get(o.Id) == null) ? 0:InsuranceMap1.get(o.Id); // if null value is zero

        o.Total_Asset_Book_Value__c = BookValueSum;

 

        Double OrigCostSum = (InsuranceMap2.get(o.Id) == null) ? 0:InsuranceMap2.get(o.Id); // if null value is zero

        o.Total_Original_Cost__c = OrigCostSum;

 

        InsurancesToUpdate.add(o);

    }

 

    update InsurancesToUpdate;

 

}