+ Start a Discussion
hal9001hal9001 

help needed with schema and/or trigger

I'm working on an Application with the schema shown in the picture below.  I'm getting stuck on the part where I use a trigger to populate the Transaction Type.  I considered using Master-Detail relationships for Transaction__c.Debit_Account__c to Account__c and for Transaction__c.Credit_Account to Account__c, but since there is a limit of two Master-Detail relationships per object, I would lose the relationship between the Batch header and Transaction.

 

Do I need to change the schema, and if so, to what? How do I code the trigger to populate the Transaction Type from Transaction Definitions?

 



sfdcfoxsfdcfox

Schema is okay (but Account_Type__c could be a picklist?), so you should not have a problem with a trigger. What's the design goal here? Maybe there's a better way?

hal9001hal9001

Thanks for the reply.  It is encouraging that you said I should not have a problem with a trigger, because I'm satisfied with the schema.

 

But I am a beginner, and I'm having trouble getting started.  I've learned other (admittedly easier) languages by looking at examples, but I'm having trouble getting started with Apex.  I've created several useful applications within Salesforce.com and now I want to do some coding as well.  I've taken a one-day class hosted by Salesforce.com where we went through the Force.Com Workbook "Build Your First App in the Cloud" and I've read Jason Ouellette's "Development with the Force.Com Platform" and did the examples.

 

But when I sit down and try to do something on my own, I can't even get started.  How hard can it be to create this trigger?

 

I think I need a Sherpa, someone to guide me through.  Any takers?  A local company says they can help, but only if I buy 10 hours at $150/hr.  Should I try Odesk?

hal9001hal9001

I added formula fields to both Transaction and Transaction Definition to concatenate the debit and credit account types.  I'm working on the trigger, and I think I'm almost there!  

hal9001hal9001

Please help -- I'm almost there!

 

If I do this, I can save the trigger, but I get this error: Attempt to de-reference a null object

 

trigger validateTransaction2 on Transaction__c (before insert, before update) {

 

    Map<String, Transaction_Definition__c> entries = new Map<String, Transaction_Definition__c>(

        [select TransactionTypeKey__c,Transaction_Definition__c.Name from Transaction_Definition__c]);

    

    for (Transaction__c trans : Trigger.new)

        trans.Transaction_Type__c = entries.get(trans.TransactionTypeKey__c).Name;

}

 

  

If I do this, I get: Save Error: Invalid foreign key relationship: Transaction_Definition__c. Transaction_Definition__c

 

trigger validateTransaction2 on Transaction__c (before insert, before update) {

  

    Map<String, Transaction_Definition__c> entries = new Map<String, Transaction_Definition__c>(

        [select TransactionTypeKey__c,Transaction_Definition__c.Name from Transaction_Definition__c]);

    

    for (Transaction__c trans : Trigger.new)

        trans.Transaction_Type__c = entries.get(trans.TransactionTypeKey__c).Transaction_Definition__c.Name;

 

}

 

 

 

1)  How can I look up Transaction_Definition__c.Name using TransactionTypeKey__c, which exists in both Transaction__c and Transaction_Definition__c?

 

2)  I’m 100% certain that the value being looked up (Transaction__c.TransactionTypeKey__c) exists in Transaction_Definition__c, but is there any way to popup some dialog boxes to show the data as the trigger is executing?

 

3)  This is a bulkified trigger.  If the value being looked up is not found, I want only the bad transactions to fail, not all (assuming the case of a batch upload).  Any suggestions?

 

ddsouzaddsouza

Sorry, typo in my last reply. Try this :

 

trigger validateTransaction2 on Transaction__c (before insertbefore update) {

  

    Map<String, Transaction_Definition__c> entries = new Map<String, Transaction_Definition__c>();

    for( Transaction_Definition__c def :   [select TransactionTypeKey__c, Name from Transaction_Definition__c]) {

          entries.put(def.TransactionTypeKey__c, def);

   }

    

    for (Transaction__c trans : Trigger.new) {

       Transaction_Definition__c def = entries.get(trans.TransactionTypeKey__c);

       if(def != null)

            trans.Transaction_Type__c = def.Name;

    }

 

}