1

I have a trigger that is supposed to work like this "If an account has a case attached to it with a Customer Save record type then the LE_Status__c field on the account is updated to "Red". It is not working and I assume it has something to do with the account query, but I'm not sure what is wrong. Here is the trigger.

Trigger UpdateLEStatus on Case (after insert) {
    Set<Id> UpdatedCaseIds = new Set<Id>(); 

    Id recordTypeId = Schema.SObjectType.Case.RecordTypeInfosByName.get('Customer Save').RecordTypeId;

    for (Case c: Trigger.new) {
        if (c.RecordTypeId == recordTypeId) {
            UpdatedCaseIds.add(c.id);
        }    
    }
    List<Account> AccountForUpdating = [SELECT LE_Status__c
                                                       FROM Account
                                                       WHERE Account.Id IN: UpdatedCaseIds];
    for (Account item: AccountForUpdating) {
        item.LE_Status__c = 'Red';
    }
    update AccountForUpdating;
}
1
  • You can just use WHERE Id IN : instead of Account.Id.
    – dphil
    Commented Feb 3, 2015 at 14:51

1 Answer 1

3

Your UpdatedCaseIds holds case ids and your select is filtering Accounts based on these ids which will not return any! You need to create a UpdatedAccountIds set..

Trigger UpdateLEStatus on Case (after insert) { Set UpdatedAccountIds = new Set();

Id recordTypeId = Schema.SObjectType.Case.RecordTypeInfosByName.get('Customer Save').RecordTypeId;

for (Case c: Trigger.new) {
    if (c.RecordTypeId == recordTypeId) {
        UpdatedAccountIds.add(c.accountid);
    }    
}
List<Account> AccountForUpdating = [SELECT LE_Status__c
                                                   FROM Account
                                                   WHERE Account.Id IN: UpdatedAccountIds];
for (Account item: AccountForUpdating) {
    item.LE_Status__c = 'Red';
}
update AccountForUpdating;
2
  • when I tried this it gives me an compile error and I'm wondering what I did wrong. I am new to development. typically I'm on the admin side. Do you have any guidance? Thank you Commented Feb 3, 2015 at 15:42
  • What is the error? You may need to change some of the fields to match your schema. Commented Feb 4, 2015 at 11:15

You must log in to answer this question.

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