function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Adam PietraszekAdam Pietraszek 

SOQL query in Apex trigger

I'm trying to write a simple Apex trigger (which I've piecemealed from the internet) and it keeps returning my own info instead of what I'm actually querying for.

I have a custom object where I will import ~3000 rows of data using data loader. One of the fields being imported is "Territory Manager." Based on that field, which is a lookup field, I need to auto-populate a second field called "Regional Manager" - also a lookup field.

The code I have now keeps populating the "Regional" field with MY manager instead of the "Territory Manager's" manager.

I am very new to Apex so it'll probably be something obvious to you but if you could help me out I'd appreciate it.
 
trigger getRSMofTM on Equipment_Performance__c (before insert) 
{
    for (Equipment_Performance__c area : trigger.new) 
    {
        if(area.Territory_Manager__c != null)
        {
            Id mgrid = [select id, managerid from user where id =: userinfo.getUserId()][0].managerid;
            string managername = [select id, name from user where id =: mgrid][0].name;
            system.debug('=================' + managername);
            area.Regional_Manager__c = mgrid;
        }
    }
}

 
Best Answer chosen by Adam Pietraszek
Steven NsubugaSteven Nsubuga
here is the bulkified version. 
trigger getRSMofTM on Equipment_Performance__c (before insert) 
{
	Set <String> territoryManagerIds = new Set <String>();
    for (Equipment_Performance__c area : trigger.new) 
    {
        if(area.Territory_Manager__c != null)
        {
			territoryManagerIds.add(area.Territory_Manager__c);
        }
    }
	Map <Id, user> managerIdMap = new Map <String, String>([SELECT Id, managerid FROM user WHERE Id IN:territoryManagerIds]);
	for (Equipment_Performance__c area : trigger.new)
	{
		if(area.Territory_Manager__c != null)
        {
            area.Regional_Manager__c = territoryManagerIds.get(area.Territory_Manager__c).managerid;
        }
	}
	
}

 

All Answers

Steven NsubugaSteven Nsubuga
here is the bulkified version. 
trigger getRSMofTM on Equipment_Performance__c (before insert) 
{
	Set <String> territoryManagerIds = new Set <String>();
    for (Equipment_Performance__c area : trigger.new) 
    {
        if(area.Territory_Manager__c != null)
        {
			territoryManagerIds.add(area.Territory_Manager__c);
        }
    }
	Map <Id, user> managerIdMap = new Map <String, String>([SELECT Id, managerid FROM user WHERE Id IN:territoryManagerIds]);
	for (Equipment_Performance__c area : trigger.new)
	{
		if(area.Territory_Manager__c != null)
        {
            area.Regional_Manager__c = territoryManagerIds.get(area.Territory_Manager__c).managerid;
        }
	}
	
}

 
This was selected as the best answer
Adam PietraszekAdam Pietraszek
Bulkified? I looked this up and it refers to updating more than 100 records, this is correct? So I have to use this version to prevent the DML errors I assume? Thanks!
Steven NsubugaSteven Nsubuga
Yes, bulkified updates 200 records at a time, that is how apex triggers operate.
Adam PietraszekAdam Pietraszek
Thank you for the code. I wanted to give it a try but I'm having 2 issues.

I am getting an error on Line 11: 
Invalid initializer type List<User> found for Map<String,String>: expected a Map with the same key and value types, or a valid SObject List
and Line 16:
Method does not exist or incorrect signature: void get(Id) from the type Set<String>

Not sure how to fix this since I haven't dealt with Maps
Steven NsubugaSteven Nsubuga
trigger getRSMofTM on Equipment_Performance__c (before insert) 
{
	Set <String> territoryManagerIds = new Set <String>();
    for (Equipment_Performance__c area : trigger.new) 
    {
        if(area.Territory_Manager__c != null)
        {
			territoryManagerIds.add(area.Territory_Manager__c);
        }
    }
	Map <Id, user> managerIdMap = new Map <Id, user>([SELECT Id, managerid FROM user WHERE Id IN:territoryManagerIds]);
	for (Equipment_Performance__c area : trigger.new)
	{
		if(area.Territory_Manager__c != null)
        {
            area.Regional_Manager__c = managerIdMap.get(area.Territory_Manager__c).managerid;
        }
	}
	
}

 
Adam PietraszekAdam Pietraszek
Thank you! I'm going to write my test class and deploy it!
Adam PietraszekAdam Pietraszek
Thanks again! I wrote my test class and it validated perfectly. I chose your Best Answer