0

received this error on apex exception.. Any help ?

Apex script unhandled trigger exception by user/organization: 0052500000Bzp4a/00D25000000DwLO Source organization: 00D0Y000000sW5j (null) CP_CaseTrigger: execution of BeforeUpdate

caused by: System.QueryException: List has more than 1 row for assignment to SObject

Class.CP_CaseHandlerNew.updateUrgencyFlag: line 771, column 1 Trigger.CP_CaseTrigger: line 26, column 1

Apex Trigger

trigger CP_CaseTrigger on Case ( before insert, before update, after insert, after update ) {
    
    if(Trigger.isBefore){ 

        if(trigger.isInsert){
            CP_CaseHandlerNew.beforeInsert(trigger.new);
        }
        if( Trigger.isUpdate ){
            CP_CaseHandlerNew.updateUrgencyFlag(Trigger.newMap,Trigger.oldMap);

        }
    }
}

Apex Method

public static void updateUrgencyFlag(Map<Id, Case> newCaseMap, Map<Id, Case> oldCaseMap){

       integer count = 0;
       integer stCount = 0;
       for(date d = system.today(); d <= system.today().adddays(4); d = d.adddays(1)){
               
               DateTime dt = (DateTime)d;
               if(d == system.today() && (dt.format('EEEE') == 'Saturday' || dt.format('EEEE') == 'Sunday')){
                   stCount = 2;
               }
               else if(d != system.today() && (dt.format('EEEE') == 'Saturday' || dt.format('EEEE') == 'Sunday')){
                   count = 2;
               }
           }
           Date stDt = system.today().adddays(stCount);
           date endDt = system.today().adddays(4 + count);
           system.debug('Start Date---' + stDt);
           system.debug('EndDate---' + endDt);
           if(newCaseMap.values().ET_Expected_Drrwadown_Date__c != null){
            for(Case caseRec : newCaseMap.values()){
               
                if(caseRec.ET_Expected_Drrwadown_Date__c.format('EEEE') == 'Saturday' || caseRec.ET_Expected_Drrwadown_Date__c.format('EEEE') == 'Sunday'){
                    
                    caseRec.ET_Urgent_Flag__c = FALSE;
                }
                else{
                                   
                    if(caseRec.ET_Expected_Drrwadown_Date__c <= endDt && caseRec.ET_Expected_Drrwadown_Date__c >= stDt){   
                        caseRec.ET_Urgent_Flag__c = TRUE;                    
                    }else{
                        caseRec.ET_Urgent_Flag__c = FALSE;
                    }  
                }
            }
       }
 }
1
  • A note for those of us in the Americas (timezones west of the prime meridian) or people having colleagues therein. Casting a Date as a Datetime will cause Datetime's .format() to move the date to the previous day. Use one of the iterations of Datetime.newInstance() instead to avoid that. E.g. Datetime myDT = Datetime.newInstance(myDate, Time.newInstance(0, 0, 0, 0));
    – Derek F
    Commented Jan 29 at 13:17

1 Answer 1

0

You are trying to get the value from a list in the below line without specifying the index.

 if(newCaseMap.values().ET_Expected_Drrwadown_Date__c != null){

Try to use -

 if(newCaseMap.values()[0].ET_Expected_Drrwadown_Date__c != null){

And I think you want to use this under the for-loop not outside. Otherwise, it won't make any sense.

Like this -


if(caseRec.ET_Expected_Drrwadown_Date__c != null && caseRec.ET_Expected_Drrwadown_Date__c.format('EEEE') == 'Saturday' || caseRec.ET_Expected_Drrwadown_Date__c.format('EEEE') == 'Sunday'){

You must log in to answer this question.

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