Saturday 26 April 2014

Child to parent updation using Master- Detail relationship.


Child to parent updation using Master- Detail relationship. Not possible workflow, Only possible on Trigger.

Ex:-
---
trigger OpportunityStageUpdate on Opportunity (after update) {
 //create list of Opportunity IDs
    List < ID > oid = New List < ID > ();
    //find Opportunity to add to list
    for (Opportunity o: Trigger.new) {
        if (o.StageName== 'Closed Lost') {
            oid.add(o.ID);
        }
    }
    List < ProfessionalServicesRequest__c> PSRUpdateList= [SELECT Id,Status__c from ProfessionalServicesRequest__c WHERE Opportunity__c in : oid];
   
    for (ProfessionalServicesRequest__c objPSR: PSRUpdateList) {
        objPSR.Status__c = 'Request Cancelled';
    }
    update PSRUpdateList;
}

Monday 7 April 2014

Choosing Workflow or Trigger for Field Updates

Keep in mind that you should always use a workflow when possible!  Only use triggers when you cannot accomplish something using a workflow.

See below for which to use and why:

----Master-detail Relationship----

1) Updating parent picklist value based on child picklist values (Workflow or Trigger)
>> Workflow, because with master-detail relationship a child object can update the parent

2) Updating child picklist value based on parent picklist values (Workflow or Trigger)
>> Trigger, because you cannot update all children records from a workflow rule, no mater what the relationship is

----Lookup Relationship----

 
3) Updating parent picklist value based on child picklist values (Workflow or Trigger)
>> Trigger, because you can only update the parent record in a workflow if it is a master-detail relationship

4) Updating child picklist value based on parent picklist values (Workflow or Trigger)
>> Trigger, because you cannot update all children records from a workflow rule, no mater what the relationship is

----No Relationship Between Objects----

5) Updating parent picklist value based on child picklist values (Workflow or Trigger)
>> Trigger, because without a relationship the workflow will not know which record to change

6) Updating child picklist value based on parent picklist values (Workflow or Trigger)
>> Trigger, because without a relationship the workflow will not know which record to change