Friday, April 19, 2013

One to One Relationship in Salesforce


We all know about the 1-M && M-M relationship(using junction Object) in salesforce.
Some times we may need 1-1 relationship among two objects.
but Unfortunately Salesforce doesnot allow any direct methodolgy to build 1-1 relationship

We can achieve the same by workflow or trigger. But workflow is more flexible & the easier way

Steps
-------
Say Two Objects OBJECT-A and OBJECT-B

1- create a look up field in one object
    say in OBJECT-A, there is a lookup to OBJECT-B
    field Name= obj-B (say)

2- Create another textfield at same Object with unique property (used to hold same obj-B field value)
     fieldName= unique_Obj-B (say)

3- Define a workFlow Rule /Trigger to  Update the custom "unique_Obj-B" field with the value of the associated OBJECT-B ID.(i.e the field value of "obj-B")

Now we have done with 1-1 Relationship
When we try to add a second same OBJECT-B to the OBJECT-A, the "unique" constraint in OBJECT-A (field "unique_Obj-B")would be violated and an error would be thrown.....

Thursday, April 18, 2013

Triggers and Order of Execution


Few days back I faced a situation , in which it was needed to update a field depending upon a Roll-up Summary field Value through trigger.
FYI we can not get the RollUpSummary field value inside trigger, because of sequence of execution of trigger(after, before) & RollUpSummary field value.

So here I am posting sequence of Trigger , automation rules ,validation rule etc execution in Salesforce.com .
The following is the order salesforce logic is applied to a record.

1- Old record loaded from database (or initialized for new inserts)
2- New record values overwrite old values
3- System Validation Rules
4- All Apex “before” triggers 
5- Custom Validation Rules
6- Record saved to database (but not committed)
7- Record reloaded from database
8- All Apex “after” triggers 
9- Assignment rules
10- Auto-response rules
11- Workflow rules
12- Escalation rules
13- Parent Rollup Summary Formula value updated (if present)
14- Database commit
15- Post-commit logic (sending email)

For Detail explanation please refer
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm 

notes: There is no way to control the order of execution within each group above