0

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.

1 Answer 1

1

This is what is giving you the error:

Alias__c currNum = [select SAP_Identifier__c, id from Alias__c where id =:aliasids and SAP_Identifier__c = null ];

If you are in a before insert then the ID of the record will be null and thus no results are returned throwing the error

You will need to modify your code to handle the logic of a before insert

2
  • I changed it to After Insert/After Update no luck
    – EricSSH
    Commented Feb 19, 2015 at 17:58
  • actually i'm getting a different error now, thanks
    – EricSSH
    Commented Feb 19, 2015 at 18:04

You must log in to answer this question.

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