0
votes

We've set up this process to help prioritize our cases. To do this, I want to trigger off a new case record after it comes in through email-to-case. Then query to see if it's related to an account. If it is, I want to return a value, cScore. Then insert.

This is what I have so far, it's returning this error:

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger newCaseScoring caused an unexpected exception, contact your administrator: newCaseScoring: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.newCaseScoring: line 12, column 18

Here's my code:

trigger newCaseTrigger on Case (before insert) {

if(Trigger.isInsert){
    List<Case> cList = [Select AccountId, Id, Priority_Score__c, Case_Priority_Score__c FROM Case WHERE Id IN :Trigger.new];
    List<Case> updateList = [Select AccountId, Id, Priority_Score__c, Case_Priority_Score__c FROM Case WHERE Id IN :Trigger.new];

    System.debug('This is the cList:' + cList);
    integer size;
    integer cnt=0;
    List<id> idList = New List<id>();
    for(Case cases : cList){

        idList.add(cases.AccountId);
        size = idList.size();
        System.debug('This is the idList:' + idList);
        System.debug('This is the size:' + size);       
        cnt++;
        System.debug(cnt);
    }
    List<Account> aList = [Select Id, Customer_s_Score_Total__c from Account where id =:idList];
    integer num;
    for(Id a : aList){
        for(Id b : idList){
            if(aList.Id == idList){
                num = aList.Customer_s_Score_Total__c;
                updateList.Priority_Score__c = num;
            }
        }
    }
} 

}

Thanks for the help.

1

1 Answers

5
votes

Your query in line 12 is returning 0 records because the Case does not yet have an Id. Since you're in the before insert trigger, the case has not yet been committed.

I think what you'll want to do is:

  1. Loop through the cases in Trigger.new and build a List<Id> of the Case.AccountId values.
  2. Do a query for accounts, using your List<Id> from the previous step.
  3. Loop through the cases again, updating the Case_Priority_Score__c field.

You will not need to do an insert call. Any changes you make to the case records will be saved automatically, since you're in the before insert trigger.