0

I am very new to Salesforce Triggers. Can someone help me to start this coding?

We have two custom objects say ‘ObjectB’ which is detail object of ‘ObjectA’. There are two custom fields on ObjectB as AssessmentDate (Date Type) and CreditValue (Number type) and another custom field on ObjectA as Date10CreditsAchieved. I want to get the AssessmentDate value from the ObjectB once the Sum of the CreditValue first reach to 10. Then that assessment date should be updated into Date10CredityAchieved filed on ObjectB. That AssessmentDate should not update further when more Assessment records are entered

Many Thanks

1 Answer 1

0

If that's the only functionality you're looking for, I see no reason not to use workflow rule combined with field update instead of after update trigger.

What you could do is create a workflow rule - navigate to Workflow Rules (Setup > Create > Workflow & Approvals > Workflow rules) and click 'New Rule'. On the first page ('Step 1: Select object') select 'ObjectB' as the object to which this workflow rule applies. Proceed to next page ('Step 2: Configure Workflow Rule'). In the 'Evaluation Criteria' section, next to 'Evaluate the rule when a record is:' select 'created, and any time it’s edited to subsequently meet criteria' (it is important to select this option and not the one which fires the rule every time the record is edited, since you obviously want the rule to fire only on the first time when CreditValue rises above 10, and not anytime after that). In the 'Rule Criteria' section select 'ObjectB: CreditValue' from the Field dropdown, 'greater' from the Operator dropdown, and finally enter '10' in the Value textbox. Click 'Save & Next' to proceed to final step ('Step 3: Specify Workflow Actions'). On the final page, in the 'Immediate Workflow Actions' section, select 'New Field Update' as the Workflow Action, which will lead you to field update wizzard.

On the New Field Update page, set 'ObjectB' as Object and 'ObjectA' > 'Date10CreditsAchieved' as Field To Update. In the 'Specify New Field Value' section, select the 'Use a formula to set the new value' option and click 'Show formula editor' link. Once the pane for entering formula is presented, click 'Insert Field' button and navigate to ObjectB > AssesmentDate and confirm selection with 'Insert'.

With your Field Update and Workflow Rule set up and ready, the only thing remaining is to activate the Workflow Rule by clicking the 'Activate' button on the workflow rule page.

PS. If you're looking for a trigger-based solution specifically, please let me know and I'll remove the workflow-based answer above. But essentially, approach is almost the same - your trigger would have to be an after update trigger on ObjectB. In the trigger, you would reference the old ObjectB via Trigger.old (returns list of ObjectB__c records which triggered your after update trigger, in your case practically list of only one ObjectB__c record). Similarly, new values would be retrieved through Trigger.new list. You could then assign sole items of these lists to sObject variables of ObjectB__c type and examine the CreditValue field on both of them - if the CreditValue of the old ObjectB is lower than 10 and new CreditValue is greater than 10, then you would have to query the master ObjectA, and set the Date10CreditsAchieved:

trigger ObjectBTrigger on ObjectB__c (after update) {
    List<ObjectB__c> oldBs = Trigger.old;
    List<ObjectB__c> newBs = Trigger.new;

    ObjectB__c oldB = null;
    ObjectB__c newB = null;

    if(oldBs.size() > 0)
        oldB = oldBs[0];

    if(newBs.size() > 0)
        newB = newBs[0];

    if(oldB != null && newB != null) {
        if(oldB.CreditValue__c < 10 && newB.CreditValue__c > 10) {
            ObjectA__c objA = [SELECT Id, Date10CreditsAchieved__c FROM ObjectA__c WHERE Id = :newB.ObjectA__c LIMIT 1];
            objA.Date10CreditsAchieved__c = newB.AssessmentDate__c;
            upsert objA;
        }
    }
}
4
  • Many Thanks for your answer I tried with Workflow. I think you missed the point here: not the single value of Credit value to reach 10, But the trigger should fire when the sum of Credit value of existing records reaches 10 (after update OR after Insert)
    – Jay Weera
    Commented Feb 3, 2015 at 2:01
  • Sorry, indeed I have. I'll update my answer, but you could still use the workflow-based solution. The addition would consist of a formula field on (let's call it CreditValuesSum) the parent object summing up the values from the children records. Then you could just edit the workflow rule and select 'ObjectA: CreditValuesSum' from the Field dropdown.
    – dino ursic
    Commented Feb 3, 2015 at 2:25
  • Thanks you very much for your solution. I will try both options so that I can learn trigger as well.
    – Jay Weera
    Commented Feb 3, 2015 at 20:17
  • I tried with workflow option, It looks like impossible as CreditValuesSum is sitting on the parent object. But we setup the workflow rule on the child object. we can't select the CrditvaluesSum field in the Rule Criteria section.
    – Jay Weera
    Commented Feb 3, 2015 at 23:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .