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;
}
}
}
}
}
.format()
to move the date to the previous day. Use one of the iterations ofDatetime.newInstance()
instead to avoid that. E.g.Datetime myDT = Datetime.newInstance(myDate, Time.newInstance(0, 0, 0, 0));