0

Need help to modify a trigger to update the Case status if Order status changes. below trigger is working but seems if for only one Order status change. need to modify the trigger if ALL Order records related to Case have status changed, then Case status updated . thanks

trigger OrderTrigger on Order (after insert, after update, after delete) {

    public String currentCaseId {get;set;}
    List<Order> OrderList = new List<Order>();
    for (Order Ord:trigger.new) {
        OrderList.add(Ord);
        currentCaseId=Ord.Case__c;
    }

    List<Case> CaseList = new List<Case>([select Id,Status from Case where Id = :currentCaseId]);

    String caseApproved = 'Approved';
    String caseCompleted = 'Completed';
    String caseInvalid = 'Invalid';
    String caseRejected = 'Rejected';
    String caseNew = 'New';
    String caseIncomplete = 'Incomplete';
    String caseInProgress = 'In-Progress';

    for (Order currentOrd: OrderList) {
        for (Case ordCase: CaseList) {
            if(currentOrd.Status == caseApproved) {
                ordCase.Status = caseCompleted;
            }
            if(currentOrd.Status == caseInvalid) {
                ordCase.Status = caseRejected;
            }
            if(currentOrd.Status == caseNew || currentOrd.Status == caseIncomplete) {
                ordCase.Status = caseInProgress;
            }
        }
    }
    update CaseList;
}
0

You need to bulkify your code like:

Update: Use Set<sObject> instead of List<sObject>. If many orders have same associated case, the last order status will be update to the case.

trigger OrderTrigger on Order (after insert, after update, after delete) {

    Set<Id> caseIds = new Set<Id>();
    for (Order Ord :Trigger.new) {
        caseIds.add(Ord.Case__c); //Get all orders associated case ids
    }

    Map<Id, Case> caseMap = new Map<Id, Case>([SELECT Id, Status FROM Case WHERE Id IN :caseIds]);//Get corresponding case details
    Map<Id, Case> mapCase = new Map<Id, Case>();

    String caseApproved = 'Approved';
    String caseCompleted = 'Completed';
    String caseInvalid = 'Invalid';
    String caseRejected = 'Rejected';
    String caseNew = 'New';
    String caseIncomplete = 'Incomplete';
    String caseInProgress = 'In-Progress';

    //For insert and update trigger
    if(Trigger.isInsert || Trigger.isUpdate){
        for (Order currentOrd : Trigger.new) {
            Case ordCase = caseMap.get(currentOrd.Case__c)

            if(currentOrd.Status == caseApproved) {
                ordCase.Status = caseCompleted;
            }
            if(currentOrd.Status == caseInvalid) {
                ordCase.Status = caseRejected;
            }
            if(currentOrd.Status == caseNew || currentOrd.Status == caseIncomplete) {
                ordCase.Status = caseInProgress;
            }
            mapCase.put(ordCase.Id, ordCase);
        }
    }
    else if(Trigger.isDelete){
        for (Order currentOrd : Trigger.new) {
            //----your logic----
        }
    }
    update mapCase.values();
}

Note: Always have a handler class for trigger.

  • Thanks @Reshma for your quick response. will try this out too. thank u. – vlink Jan 3 at 1:54
  • got this error " variable does not exist: Ord " in Line 22. thanks for assistance. – vlink Jan 3 at 2:41
  • Updated my code. Please try and let me know. @vlink – Reshma Jan 3 at 5:36
  • thanks Reshma, I got this error now "duplicate value found: <unknown> duplicates value on record with id: <unknown>". in Line 0. can't trace that error msg. – vlink Jan 3 at 6:04
  • yes I tried it. – vlink Jan 3 at 6:08

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.