1

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;
}

}
}
  • The data 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'. – Merry Stambaugh Apr 26 '13 at 22:59
2

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.

0

Are you sure you need a trigger for this?

I would take a step back to see if you can do this declaratively first.

Some options for that would be

1) Time based workflow on the Case object that updates the parent Account's field based on R,G,Y status and the Case Age. (The following link has some tips for calculating case age by the way: http://na9.salesforce.com/help/doc/en/useful_advanced_formulas.htm)

2) Instead of a trigger to update that field, use a roll-up summary field (or set of roll-up summary fields in conjunction with a If/Else formula for displaying R,G,Y status)

  • I tried doing it declaratively and could not get it to work. I am pretty sure it needs to be a trigger as it has to look at all cases on an account and return whether or not there is a r/y/g/ case. For instance it has to use the following logic (from the field on the case called CaseStatusCalcGRY__c - – Merry Stambaugh Apr 29 '13 at 23:41
  • IF ( AND(NOT(ISPICKVAL( Status , 'closed')), ISPICKVAL( Priority , 'P2') , DATEVALUE(CreatedDate) < (Today()-7)) , "Y" , IF (OR(ISPICKVAL( Status , 'closed'),AND(ISPICKVAL( Priority , 'P2'),DATEVALUE(CreatedDate) > (Today()-7)), AND( NOT(ISPICKVAL( Priority , 'P2')),NOT(ISPICKVAL( Priority , 'P1')),DATEVALUE(CreatedDate) > (Today()-30))) , 'G', IF(OR(AND(NOT(ISPICKVAL( Status , 'closed')),ISPICKVAL( Priority , 'P1')) ,AND(NOT(ISPICKVAL( Priority , 'P2')),NOT(ISPICKVAL( Priority , 'P1')),DATEVALUE(CreatedDate) < (Today()-90))) , 'R' , null))) – Merry Stambaugh Apr 29 '13 at 23:42
  • New trigger - trigger CaseStatusUpdate on Case (after update, after insert) { List<ID> AccIDs = new List<ID>(); List<Account> updateAccList = new List<Account>(); For( Case c1 : Trigger.New){ AccIDs.add(c1.AccountId); } Map<ID,Account> AccUpdateMap = new Map<ID,Account>(); for(Account acc : [Select ID , AccCaseStatusCalcGRY__c from Account where ID in:AccIDs]) { AccUpdateMap.put(acc.ID ,acc ); } For(Case c1: Trigger.New){ Account updateacc =AccUpdateMap.get(c1.AccountID); – Merry Stambaugh Apr 30 '13 at 3:51
  • if (c1.CaseStatusCalcGRY__c == 'R') { updateacc.AccCaseStatusCalcGRY__c = 'R'; } else if (c1.CaseStatusCalcGRY__c != 'R' && c1.CaseStatusCalcGRY__c != 'G') { updateacc.AccCaseStatusCalcGRY__c = 'Y'; } else { updateacc.AccCaseStatusCalcGRY__c = 'G'; updateAccList.add(updateacc); } if(updateAccList!=null && updateAccList.size()>0) update updateAccList; } } – Merry Stambaugh Apr 30 '13 at 3:51

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.