[SalesForce] Trigger to update Account from Case field

Every time I think I have got these triggers figured out I have to create something new and I am stupid all over again! This time I have to update a field on the account with either 'R', 'Y' or 'G' (Actually it will eventually be a stoplight). The date to update that field is on the case depending on case status, age, etc there is a field that shows either 'R', 'Y' or 'G'. What I need to trigger to do it to look at all cases and if any of them are 'R' update the account field to 'R'. If they are not 'R' or 'Y' then mark it 'G' and if they are neither 'R' or 'G' mark it Yellow. (If you can help me past the error I would really appreciate it! Finally getting the Apex training in June!!)

I have come up with the code below, I know it is probably not right, but I cannot get past the first error to figure out the rest. I keep getting this error:

Error: Compile Error: Didn't understand relationship 'Case' in FROM
part of query call. If you are attempting to use a custom
relationship, be sure to append the '__r' after the custom
relationship name. Please reference your WSDL or the describe call for
the appropriate names. at line 13 column 51

Here is the code

trigger UpdateAccountFromContacts on Account(before update) {
Set<Id> accountIds = new Set<Id>();
for (Account a : Trigger.new)
accountIds.add(a.Id);
Map<Id, Account> accounts = new Map<Id, Account>( [ Select a.Id, (Select CaseStatusCalcGRY__c From Case Where CaseStatusCalcGRY__c != null) from Account a where a.ID in :accountIds] );
for (Account a : Trigger.new) {

    Account accFromMap = accounts.get(a.Id);

    if (accFromMap.Case.size() > 0) {

(CaseStatusCalcGRY__c == 'R') {
    Acc_CaseStatusCalcGRY = 'R';
} else if (CaseStatusCalcGRY__c != 'R') && (CaseStatusCalcGRY__c != 'Y') {
    Acc_CaseStatusCalcGRY__c = 'G';
} else if (CaseStatusCalcGRY__c != 'R') && (CaseStatusCalcGRY__c != 'G') {
    Acc_CaseStatusCalcGRY__c = 'Y';
} else {
    Acc_CaseStatusCalcGRY__c = null;
}

}
}

Best Answer

The problem you're having here doesn't have anything to do with triggers. As the error message clearly states (I haven't validated your other code, just fixed the problem you're facing)

Error: Compile Error: Didn't understand relationship 'Case' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 13 column 51

The problem you're having is that in your inner query in the FROM clause you're using Case, which isn't a known child relationship for Accounts. It is Cases (multiple), when you would do this in custom object relationships you would have your child relationshipname appended with __r

Map accounts = new Map( [ Select a.Id, (Select CaseStatusCalcGRY__c From Case Where CaseStatusCalcGRY__c != null) from Account a where a.ID in :accountIds] );

You can easily find the child relationship names (you'd have to insert in the from clause for an inner query) via these ways:

1. Eclipse / Force.com IDE Open your project and select your salesforce.schema file (which is in the root of your project), make sure you have the lastest version by Refreshing your Schema.

Then go to to object you wish to find the child relationships from (in your case Account), Open it, Go to Child Relationships and find the object you wish to use in your inner query (Case here), open that, and there you'll find the Relationship Name this you'll have to use in your FROM clause

enter image description here

2. Force.com Explorer Very similar to the last, but in the Force.com Explorer tool now.

enter image description here

I'm sure there are plenty more ways, as this information can be read from the API, but these 2 are the onces I use the most.

Related Topic