Tag Archives: Workflow Rule

Create One to One Relationship in Salesforce

Basically Salesforce offers two types of relationship:

  • One To Many Relationship
  • Many To Many Relationship (Using the concept of Junction object)

Sometimes we may need One To One relationship, But unfortunately Salesforce doesn’t allow any direct methodology to build One To one relationship.

Let’s consider the scenario that we want to establish a One to One relationship between two custom objects Employee__c and PAN_Card__c.

So, here are few ways to implement One To One relationship between two objects in Salesforce. We can achieve this by using configuration only and can also achieve this by using code to make it more scalable.

Option 1:

  • Create a lookup field on PAN_Card__c to Employee__c.
  • Create a custom field on the PAN_Card__c object and make the field unique. This field would be used to hold the ID of the associated Employee__c. Hide this field from all page layouts.
  • Create a Workflow rule on PAN_Card__c. For any change of the lookup field on PAN_Card__c object, update the custom field on the PAN_Card__c object with the value of the associated Employee Id.

We have established a one to one relationship between PAN_Card__c and Employee__c. When we try to add a second PAN_Card__c to the Employee__c, the “unique” constraint would be violated and an error would be thrown. This approach will work on both standard and custom object.

Option 2:

  • Create a master detail relationship on PAN_Card__c to Employee__c object.
  • Create a roll up summary field on Employee__c object of PAN_Card__c with count type.
  • Create a validation rule on Employee__c object rollup summary field to check if count > 1.

In this way also, We have established a one to one relationship between PAN_Card__c and Employee__c. So it will throw an error if Employee__c has more than one PAN Card.

Option 3:

  • Create lookup fields on both objects PAN_Card__c and Employee__c, to each other.
  • Write triggers, for any change on these lookups, to either copy the record ID into the other object’s lookup field when the other object’s lookup field is empty, or disallow the change to the original record when the other object’s lookup field is already populated with a different ID from the original record.

This is already having a one-to-onePassport relation.

Option 4:

  • Create a trigger on PAN_Card__c object to check if the PAN Card record already exists for an Employee. If it exist, then throw an error, else allow the user to create.
    Here is the example for Employee__c and PAN_Card__c object:

    trigger PANCardValidation on PAN_Card__c (before insert, before update) {
        Set<id> employeeIds = new Set<id>();
        Map<id, Employee__c> mapEmployee = new Map<id, Employee__c>();
     
        for (PAN_Card__c p : trigger.New) {
            employeeIds.add(p.Employee__c);
        }
     
        List<Employee__c> lstEmployee = [SELECT Id, Name FROM Employee__c WHERE Id IN : employeeIds];
        if (!lstEmployee.isEmpty()) {
            for (Employee__c emp : lstEmployee) {
                mapEmployee.put(emp.Id, emp);
            }
     
            for (PAN_Card__c p : trigger.New) {
                if (mapEmployee.containsKey(p.Employee__c)) {
                    p.addError('A PAN Card already exist for the employee - ' + mapEmployee.get(p.Employee__c).Name);
                }
            }
        }
    }
    

Salesforce Workflow Rule Evaluation Criteria

created :

  • The workflow rule will be evaluated the rule criteria when a record is created. If the rule criteria is met, run the rule.
  • With this option, the rule never runs more than once per record.

created, and every time it’s edited :

  • The workflow rule will be evaluated the rule criteria each time a record is created or updated. If the rule criteria is met, run the rule.
  • With this option, the rule repeatedly runs every time a record is edited, as long as the record meets the rule criteria.
  • We cannot add time-dependent actions to the rule if you select this option.

created, and any time it’s edited to subsequently meet criteria :

  • The workflow rule will be evaluated the rule criteria each time a record is created or updated.
  • For a new record, run the rule if the rule criteria is met.
  • For an updated record, run the rule only if the record is changed from not meeting the rule criteria to meeting the rule criteria.
  • With this option, the rule can run multiple times per record, but it won’t run when the record edits are unrelated to the rule criteria.
  • For example, Suppose the Director wants to be notified when an Opportunity value is > $50,000. Using this criteria, the rule would fire when the Opportunity is created with a value $60,000 and the Director would get an email notifying him of this Opportunity. Now if the record is changed from a value of $60,000 to $75,000 the rule does not fire again because it has already met the criteria at its initial creation. Now if the reverse happened, and the value went from $60,000 to $40,000 nothing would happen since the record doesn’t meet the criteria for the rule. But, if in another subsequent change, the value increased to $55,000 the criteria would again be met, and since the record didn’t previously meet the criteria during the last change, the rule would again fire and the Director would receive a new alert.