• Babugarh
  • NEWBIE
  • 5 Points
  • Member since 2010

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

Hi

 

I was looking at if there are any good practices in using triggers.

 

Say, should I slice down all the code in a trigger and move it across into a class/controller ?

 

If I were to use more of the trigger's features like trigger.oldmap or trigger.newmap or trigger.new or trigger.old.  Is it still a good practice to still move these into the controllers (through assignments) to functions?

 

If you think that those are not good practices, please do let me know.

 

This question is in regard with long term maintenance perspective. 

I have a query below, I would like to filter the Contact object based on the filters on the campaign Members. I understand that I can do that through for loops.  But I am trying to see if there are other ways and update myself.

 

 

 

Select 
        c.Id, 
        c.Account.xyz__c,
        (Select 
               Id, 
               Status 
        From 
               CampaignMembers 
        WHERE 
               status = 'sent') 
From 
        Contact c

 

Please do note that the above query will go as a dynamic SOQL and the dynamic SOQL will build the account fields in the query based on parameters set in the custom setting.

 

"The Class"

 

public class KarmagedonHoras {
  

static testmethod void KarmagedonHoras() {

Asistencia_Tecnica__c[] a =new Asistencia_Tecnica__c [] {
new Asistencia_Tecnica__c(Name= 'Prueba12', Total_Horas_Profesionales__c=0, Equipo_Instalado__c='02iA0000000LhK3')
};
insert a;

User[] acctQuery10 = [SELECT Valor_Hora_Profesional__c FROM User WHERE User.id ='005A0000000i5tN'];

Horas_Profesionales__c[] tr1 =new Horas_Profesionales__c[]{
new Horas_Profesionales__c(Asistencia_Tecnica__c=a[0].id, Cantidad__c =1 )
};
insert tr1;

Asistencia_Tecnica__c[] acctQuery11 = [SELECT Total_Horas_Profesionales__c FROM Asistencia_Tecnica__c WHERE Name ='Prueba12'];

System.assertEquals(acctQuery11[0].Total_Horas_Profesionales__c , 1000) ; 


tr1[0]. Cantidad__c = 2;

update tr1;

Asistencia_Tecnica__c[] acctQuery12 = [SELECT Total_Horas_Profesionales__c FROM Asistencia_Tecnica__c WHERE Name ='Prueba12'];

System.assertEquals(acctQuery12[0].Total_Horas_Profesionales__c ,2000) ; 

delete tr1;

Asistencia_Tecnica__c[] acctQuery13 = [SELECT Total_Horas_Profesionales__c FROM Asistencia_Tecnica__c WHERE Name ='Prueba12'];

System.assertEquals(acctQuery13[0].Total_Horas_Profesionales__c ,0) ;
}
}

 

"The Trigger"

 

trigger TriggerKarmagedonHoras on Horas_Profesionales__c(after insert, after update, after delete) 
{
Horas_Profesionales__c newTransaction;

if (Trigger.isInsert || Trigger.isUpdate)
 newTransaction =  System.Trigger.new[0];

if (Trigger.isDelete)
 newTransaction = System.Trigger.old[0];
 
  Asistencia_Tecnica__c a ;
 a = [select id, Total_Horas_Profesionales__c from Asistencia_Tecnica__c where id = 
 :newTransaction.Asistencia_Tecnica__c limit 1];

if (newtransaction.Total_Horas_Profesionales__c!= 0) {
if (Trigger.isInsert) {
if (a.Total_Horas_Profesionales__c== null) a.Total_Horas_Profesionales__c= 0;
 if (Trigger.isInsert && System.Trigger.new[0].Total_Horas_Profesionales__c!= null) {
  a.Total_Horas_Profesionales__c+= newtransaction.Total_Horas_Profesionales__c;
 }
} else {
 Horas_Profesionales__c oldTransaction =  System.Trigger.old[0];
 if (Trigger.isUpdate) {
   if (System.Trigger.new[0].Total_Horas_Profesionales__c!= null)
     a.Total_Horas_Profesionales__c+= newtransaction.Total_Horas_Profesionales__c;
   if (System.Trigger.old[0].Total_Horas_Profesionales__c!= null)
     a.Total_Horas_Profesionales__c-= oldtransaction.Total_Horas_Profesionales__c;
 }
 if (Trigger.isDelete && System.Trigger.old[0].Total_Horas_Profesionales__c!= null) { 
  a.Total_Horas_Profesionales__c-= oldtransaction.Total_Horas_Profesionales__c;
 }
}
update a;


}

 

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

My First Post

Error

 

Error | Line 20 | Column 1

System.AssertException: Assertion Failed: Expected: 0, Actual: 1000

 

Error | Line 29 | Column 1

System.AssertException: Assertion Failed: Expected: 0, Actual: 1000

 

Error | Line 35 | Column 1

System.AssertException: Assertion Failed: Expected: 0, Actual: 1000

 

 

Cueck

I have a query below, I would like to filter the Contact object based on the filters on the campaign Members. I understand that I can do that through for loops.  But I am trying to see if there are other ways and update myself.

 

 

 

Select 
        c.Id, 
        c.Account.xyz__c,
        (Select 
               Id, 
               Status 
        From 
               CampaignMembers 
        WHERE 
               status = 'sent') 
From 
        Contact c

 

Please do note that the above query will go as a dynamic SOQL and the dynamic SOQL will build the account fields in the query based on parameters set in the custom setting.

 

A Little Background:

We have developed some feedback loops that aggregate (textual) categorical information from Opportunity Line Items and push it back to the Opportunity, Account, and Contact objects. These feedback fields and their source fields are only used by 2 out of 5 of the groups in our company that use Opportunities, so we've created a Custom Settings dataset for storing the opt-in status of these different groups for various automated enhancements we've developed.

 

The Scenario:

From an after-update Trigger on Opportunities, I need an efficient way to check whether any of the record types within the batch qualify for the feedback summary automation. This controls what fields are included in the SOQL query to fetch extended opportunity data and whether the summary ,method is invoked from the trigger at all. Which is a better way to do this. I could

  1. Perfrom and Aggregate Query grouped by RecordTypes and filtered by the Opportunity IDs from the Trigger. This would then provide a much reduced set of values to iterate through, but would count against my query governor limits without truly providing new data. 
  2. Iterate through the entire set of Opportunities to cull the set of distinct record types. This seems like a bit of a waste since the Opportunity records themselves don't contain any other information that could be processed at this time and they will all have to be iterated over again later when they've been returned with their extended details.
  3. Iterate through the set of Opportunities until a single RecordType qualifies for the extended details. This would solve the immediate scenario and provide minor efficiencies over #2, but is less extensible - ie if we later introduce another automated, opt--in, enhancement this would become a bit useless.

 

What do people think? Personally, I have a strong preference for #1, but I try to make a habit of questioning my preferences...  

    • August 26, 2010
    • Like
    • 0
    Hi,

    I am trying to create a plugin, which communicated between 2 salesforce instance. Example creation of an account in once instance creates the same in another.

    Can i create and export an APEX class as WSDL from one instance and use it on another? If yes, how do i login into other instance.

    OR is there some better way to do this? Please advise.

    GreatG

    • December 02, 2008
    • Like
    • 0
        We have requirement for a custom field MS Created Date having data type as date/Time. I loaded the field using data loader. But the date is getting loaded correctly but the time is not get loaded properly.
     For eg. If i load date/ time through dataloader as 04/10/2006 09:40:40:000 (dd/mm/yyyy hh:mm:ss:SSS format ), but in the sfdc it is getting loaded as
    04/10/2006 06:10(dd/mm/yyyy hh:mm:ss:SSS format) so there is a 3 hour 30 minute difference is there.
    Using data loader i used the format as yyyy-MM-dd'T'HH:mm:ss.SSS'Z' format.
    I tried all the format which are given in the dataloader document as shown below.
    •yyyy-MM-dd'T'HH:mm:ss.SSS Pacific Standard Time
    • yyyy-MM-dd'T'HH:mm:ss.SSSPacific Standard Time
    • yyyy-MM-dd'T'HH:mm:ss.SSS PST
    • yyyy-MM-dd'T'HH:mm:ss.SSSPST
    • yyyy-MM-dd'T'HH:mm:ss.SSS GMT-08:00
    • yyyy-MM-dd'T'HH:mm:ss.SSSGMT-08:00
    • yyyy-MM-dd'T'HH:mm:ss.SSS -800
    • yyyy-MM-dd'T'HH:mm:ss.SSS-800
    • yyyy-MM-dd'T'HH:mm:ss
    • yyyy-MM-dd HH:mm:ss
    • yyyyMMdd'T'HH:mm:ss

    But also I am getting the same incorrect value with no change in the time.
    I am using the Time Zone as "GMT+01:00) Central European Time (Europe/Paris)"
    in SFDC and my computer time zone is GMT +5:30.
    Please help me to resolve the problem.
    Thanks in advance for your solution.