2

I'm having some issues with some code I've adapted to work for my instance that was posted by @Brian Mansfield.

We have invoices pushed into a custom object and they link through to accounts fine, but I'm trying to get it linked to a user where possible to allow easier reporting.

On the invoice item there is a field "Sales_Rep_1" which should match the alias of a user. The code is working to some degree, but it seems to be case-sensitive. E.g. if the invoice has Sales_rep_1 value of "TEST" and there's a user with an alias "test", it throws a wobbly and I get the following error:

Error:Apex trigger SetSalesRep caused an unexpected exception, contact your administrator: SetSalesRep: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.SetSalesRep: line 19, column 1

Would be grateful if someone can find a fix for this.

Many thanks

trigger SetSalesRep on Invoice_Item__c (before update, before insert) {
    Set<String> users = new Set<String>();

    for (Invoice_Item__c collectNumFromOrder : Trigger.new) {
        users.add(collectNumFromOrder.Sales_Rep_1__c);
    }

    List<User> UserList = [SELECT id, alias FROM USER WHERE alias IN :users];

    Map<String, User> InvoiceToUserMap = new Map<String, User>();

    for (User c : UserList) {
        InvoiceToUserMap.put(c.alias, c);
    }

    for (Invoice_Item__c o : Trigger.new) {
        if (!InvoiceToUserMap.Isempty() )  {

             o.User__c = InvoiceToUserMap.get(o.Sales_Rep_1__c).id;

        }
        else {
            o.User__c = null;
        }
    }
}

1 Answer 1

2

You should check whether value exist in your map using containskey Method and null for sales_rep_1__c

trigger SetSalesRep on Invoice_Item__c (before update, before insert) {
    Set<String> users = new Set<String>();

    for (Invoice_Item__c collectNumFromOrder : Trigger.new) {
         if(collectNumFromOrder.Sales_Rep_1__c!=null){
            users.add(collectNumFromOrder.Sales_Rep_1__c.tolowercase());
         }
    }

    List<User> UserList = [SELECT id, alias FROM USER WHERE alias IN :users];

    Map<String, User> InvoiceToUserMap = new Map<String, User>();

    for (User c : UserList) {
        InvoiceToUserMap.put(c.alias.tolowsercase(), c);
    }

    for (Invoice_Item__c o : Trigger.new) {
        if (!InvoiceToUserMap.Isempty() && o.Sales_Rep_1__c!=null && InvoiceToUserMap.containsKey(o.Sales_Rep_1__c.tolowercase()) )  {

             o.User__c = InvoiceToUserMap.get(o.Sales_Rep_1__c.tolowercase()).id;

        }
        else {
            o.User__c = null;
        }
    }
}
5
  • This would avoid the error, but the logic would still be faulty as it will continue to be case sensitive.
    – feddus
    Commented Apr 27, 2016 at 14:31
  • that is why added tolowercase method in code
    – Himanshu
    Commented Apr 27, 2016 at 14:35
  • I would suggest using map of string to id than to user object itself. Commented Apr 27, 2016 at 18:20
  • @Himanshu That worked a treat. It is now case insensitive and populates my user field correctly
    – Gargs
    Commented Apr 28, 2016 at 9:51
  • @Gargs you can accept his answer by clicking the ( V ) button on the left of it. That'll show other users this worked and that the question got answered. Commented Apr 28, 2016 at 10:58

You must log in to answer this question.

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