5

I keep getting this error on this code and i am not sure how to fix this.

Invalid foreign key relationship: Lead.channel_sales_manager__c

The line with the errors is in *

trigger leadassign on Lead (after insert) {

    Map<Id, String> channelSalesManagerMap = new map<Id,String>();
    Set<String> channelSalesManagerNameSet = new Set<String>();
    for (Lead leadObj : Trigger.new) {
        channelSalesManagerMap.put(leadObj.Id , leadObj.Channel_Sales_manager__c);
        channelSalesManagerNameSet.add(leadObj.Channel_Sales_manager__c);
    }

    map<Id, User> userIdUserMap = new map<Id,User>();
    for (User userObj : [SELECT Id FROM User WHERE name IN :channelSalesManagerNameSet]) {
        userIdUserMap.put(userObj.name, userObj);
    }

    for (Lead newLead : Trigger.new) {
    IF(newlead.channel_sales_manager__C == 'Unknown'){
            Group csmqueue = [SELECT Id FROM Group WHERE name = 'Channel Sales Managers' AND type = 'Queue' LIMIT 1];
                newlead.ownerID = csmqueue.Id;
        }
        **IF(userIdUserMap.contains(newlead.channel_sales_manager__c.name**)){
            newlead.ownerId = userIdUserMap.get(newlead.channel_sales_manager__C).Id;
        }

    }
}
3

Based on your responses to Eric, Lead.channel_sales_manager__c is a picklist field and not a relationship field. Assuming that the Name of the user is a picklist value this code is incorrect

map<Id, User> userIdUserMap = new map<Id,User>();
    for (User userObj : [SELECT Id FROM User WHERE name IN :channelSalesManagerNameSet]) {
        userIdUserMap.put(userObj.name, userObj);
    }

it should be

map<String, User> userIdUserMap = new map<Id,User>();
for (User userObj : [SELECT Id FROM User WHERE name IN :channelSalesManagerNameSet]) {
    userIdUserMap.put(userObj.Name, userObj);
}

and the comparison you marked with ** should be

IF(userIdUserMap.containsKey(newlead.channel_sales_manager__c)
  • That got rid of the error thanks. However, when i try to test the code by creating a lead, i am getting this error now. execution of AfterInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: User.Name: Trigger.leadassign: line 12, column 1 – user24287 Sep 6 '15 at 0:48
  • 1
    @user24287 - That error is self explanatory. Read it carefully and think about what it is saying. You should then be able to figure it out. If you still have an issue, post a new question as this one appears to be answered – Eric Sep 6 '15 at 1:33
  • In the code example the soql query needs to include the name field [select Id, Name from user ...] – Developer Wonk Sep 6 '15 at 1:39
2

You need to traverse through the relationship like this:

newlead.channel_sales_manager__r.name

Notice the __r

Even at that, __r.name will be null in the trigger context so you will have to query for the value(s).

  • Tried the __r and i was still getting same error. Invalid foreign key. – user24287 Sep 5 '15 at 23:46
  • @user24287 - Your userMap is an ID key and you are trying to find a string key. Also, are you sure that is the line that is causing the error because it should not be throwing that error there unless that field is not a lookup or MD field or you are using the wrong API name – Eric Sep 5 '15 at 23:50
  • I have a picklist field with names in it. I am trying to map it with their user IDs. – user24287 Sep 6 '15 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.