-1

I have requirement like :

One custom object (Issue) it is child object of CASE object and it is has checkbox field (primary issue). It has to be checked only for first record created from case and for rest all record created from case it should not be checked.

How to implement this logic?

1 Answer 1

0

To achieve this, you need to create a trigger on the Issue object that checks the Primary Issue checkbox field only for the first record created from a Case.

trigger IssueTrigger on Issue__c (after insert) {
        Set<Id> caseIds = new Set<Id>();
        for (Issue__c issue : Trigger.new) {
                caseIds.add(issue.Case__c);
        }
        List<Case> cases = [SELECT Id, (SELECT Id, Primary_Issue__c FROM Issues__r ORDER BY CreatedDate ASC) FROM Case WHERE Id IN :caseIds];
        List<Issue__c> issuesToUpdate = new List<Issue__c>();
        for (Case c : cases) {
                if (!c.Issues__r.isEmpty()) {
                        Issue__c firstIssue = c.Issues__r[0];
                        if (!firstIssue.Primary_Issue__c) {
                                firstIssue.Primary_Issue__c = true;
                                issuesToUpdate.add(firstIssue);
                        }
                }
        }
        // Update the Issues
        update issuesToUpdate;
}

This trigger will run after any new Issue is inserted and check if it is the first related Issue for its parent Case. If it is, it will set the Primary Issue checkbox to true. Otherwise, it will leave it as false.

Note: Change the object name, Field name and relationship name according to your org.

You must log in to answer this question.

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