I created a small trigger in order to auto number a field in a custom object, I need to do this so I have an index to go off of when SAP is pulling data. (I tried auto number, with undesired results). The trigger works when I do it manually, Now I'm trying to update all the records using dataLoader.
public static void AliasAutoNumber (List<Alias__c> Alias){
Set<id> aliasids = new Set<id>();
for(Alias__c al: Alias){
aliasids.add(al.id);
}
if(aliasids.size() > 0){
try{
//get the largest current auto number
Alias__c prevNum = [select SAP_Identifier__c, id from Alias__c where SAP_Identifier__c != null order by SAP_Identifier__c Desc Limit 1];
Alias__c currNum = [select SAP_Identifier__c, id from Alias__c where id =:aliasids and SAP_Identifier__c = null ];
for(Alias__c a: Alias){
a.SAP_Identifier__c = prevNum.SAP_Identifier__c +1;
}
}catch(Exception e){}
}
}
I believe I have an issue with prevNum
and currNum
, if I remove the try/catch I get a List has no rows for assignment to sobject
. Does anyone see a potential problem? I have tried both Before/After inserts and updates. Again this trigger works when I go through and manually update the record.