+ Start a Discussion
megan.burchmegan.burch 

Help with trigger

I'm having trouble with a trigger I'm trying to write. When new accounts are loaded into my org the owner field is sometimes defaulted to a placeholder user 'x x'. There is another field on the account called market assignment which is a forumula that pulls the market assignment on the market assignment custom object. Also on the market assignement custom object is the manager which should also be the account owner. The trigger is trying to update accounts where owner = 'x x' with the manager from the corresponding market assignement. Any help would be much appreciated. Thanks!!

 

Trigger

 

Trigger marketassignmenttrigger on Account (before Insert, before Update) {
    
    Map<String, Market_Assignments__c> junctionMap = new Map<String, Market_Assignments__c>{};
    
    for (Account a : Trigger.new)
    {
        if (a.owner != null && 
        (Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(a.id).Market_assignment_copy__c == a.Market_assignment_copy__c)))
        {            
            //Ignore casing in the map
            junctionMap.put(a.Market_assignment_copy__c.toLowerCase(), null);            
        }
    }
    
    if (!junctionMap.isEmpty())
    {    
        
        for (Market_Assignments__c item : [ select Id, market_manager__c, name from Market_Assignments__c where market_manager__c IN :junctionMap.keySet()] )
        {
            junctionMap.put(item.name.toLowerCase(), item);
        }
                        
        for (Account a : Trigger.new)
        {
            if (a.ownerid == '00560000001267d')
            {
               Market_Assignments__c m = junctionMap.get(a.market_assignment_copy__c.toLowerCase());
                
                
                {
                    a.ownerid = m.market_manager__c;                
                }
                
            }
        }
    }    
}

 

Best Answer chosen by Admin (Salesforce Developers) 
ForcepowerForcepower

Megan,

 

I guess the trigger had been very geared towards updates. I think the following changes should help with the Inserts.

 

Just add the following line in bold wherever you have the first line (should be two places like that)

 

        Account a1 = accountsMap.get(a.id);
        if (a1 == null) a1 = a;

Ram

    for (Account a : Trigger.new)
    {   
        if (a.ownerid != null && Trigger.isInsert) {
            
            junctionMap.put(a.Market_assignment_copy__c, null);
            System.debug(' market assignment = ' + a.Market_assignment_copy__c);
            System.debug('a.ownerid:' + a.ownerid);
                       
        }
        else
        if (a1.ownerid != null &&
        (Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(a.id).Market_assignment_copy__c == a1.Market_assignment_copy__c))) */
        {            
            //Ignore casing in the map
            junctionMap.put(a1.Market_assignment_copy__c, null);
            System.debug(' market assignment = ' + a1.Market_assignment_copy__c);
            System.debug('a1.ownerid:' + a1.ownerid);           
        }
        
    }

All Answers

ForcepowerForcepower
Megan - what error do you get?
megan.burchmegan.burch

I'm not getting an error, but it's not changing the owner

 

Thanks!

ForcepowerForcepower

I suspect you're not getting the owner id on the trigger and may have to fetch it explicitly. You may want to put a debug statement to see what Ids you get for owner.

 

megan.burchmegan.burch

ok thanks! I'll try to figure it out.

ForcepowerForcepower
I think you may be getting the long object id that is 18 chars rather than the 15 chars you're comparing:
Try something like this:
if (a.ownerid != null && a.ownerid.startsWith( '00560000001267d'))
megan.burchmegan.burch

Thanks!

 

When I try that I get Error: Compile Error: Method does not exist or incorrect signature: [Id].startsWith(String) at line 25 column 36.

 

I also tried

 

(a.owner = 'x x);
             {
            
               Market_Assignments__c m = junctionMap.get(a.market_assignment_copy__c.toLowerCase());
                
                
                {
                    a.owner = m.market_manager__c;

 

but when I do that I get this error

 

Illegal assignment from String to SOBJECT:User at line 25 column 15

ForcepowerForcepower

May be you can assign it to a string before doing the test:

 

String ownerIdStr = a.ownerid;

if (ownerIdStr != null && ownerIdStr.startsWith( '00560000001267d'))

megan.burchmegan.burch

Thanks. I can see in the debug logs that the trigger is firing but it's still not changing anything.

ForcepowerForcepower

You may want to put a debug to print the owner id both before the if statement and also within the if - just to make sure the match is occurring based on owner id.

megan.burchmegan.burch

Thank you. I'm pretty new to this, would the way I added the debug below be correct?

Trigger marketassignmenttrigger on Account (before Insert, before Update) {
    
    Map<String, Market_Assignments__c> junctionMap = new Map<String, Market_Assignments__c>{};
    
    for (Account a : Trigger.new)
    {
        if (a.owner != null && 
        (Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(a.id).Market_assignment_copy__c == a.Market_assignment_copy__c)))
        {            
            //Ignore casing in the map
            junctionMap.put(a.Market_assignment_copy__c.toLowerCase(), null); 
            System.debug('a.ownerid:' + a.ownerid);           
        }
    }
    
    if (!junctionMap.isEmpty())
    {    
        
        for (Market_Assignments__c item : [ select Id, market_manager__c, name from Market_Assignments__c where market_manager__c IN :junctionMap.keySet()] )
        {
            junctionMap.put(item.name.toLowerCase(), item);
        }
                        
        for (Account a : Trigger.new)
        {
             String ownerIdStr = a.ownerid;

                    if (ownerIdStr != null && ownerIdStr.startsWith( '00560000001267d'))
             {
            System.debug('a.ownerid:' + a.ownerid);
               Market_Assignments__c m = junctionMap.get(a.market_assignment_copy__c.toLowerCase());
                
                
                {
                    a.ownerid = m.market_manager__c;                
                }
                
            }
        }
    }    
}

 

ForcepowerForcepower
Looks good, Megan. Just added it in one more place - before the if

Trigger marketassignmenttrigger on Account (before Insert, before Update) { Map<String, Market_Assignments__c> junctionMap = new Map<String, Market_Assignments__c>{}; for (Account a : Trigger.new) { if (a.owner != null && (Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(a.id).Market_assignment_copy__c == a.Market_assignment_copy__c))) { //Ignore casing in the map junctionMap.put(a.Market_assignment_copy__c.toLowerCase(), null); System.debug('a.ownerid:' + a.ownerid); } } if (!junctionMap.isEmpty()) { for (Market_Assignments__c item : [ select Id, market_manager__c, name from Market_Assignments__c where market_manager__c IN :junctionMap.keySet()] ) { junctionMap.put(item.name.toLowerCase(), item); } for (Account a : Trigger.new) { String ownerIdStr = a.ownerid; System.debug('a.ownerid:' + a.ownerid); if (ownerIdStr != null && ownerIdStr.startsWith( '00560000001267d')) { System.debug('Match - a.ownerid:' + a.ownerid); Market_Assignments__c m = junctionMap.get(a.market_assignment_copy__c.toLowerCase()); { a.ownerid = m.market_manager__c; } } } } }
megan.burchmegan.burch

This is what I'm getting from the debug logs

 

10:17:51.146 (146577000)|CODE_UNIT_STARTED|[EXTERNAL]|01qg0000000CfJD|marketassignmenttrigger on Account trigger event BeforeUpdate for [001g0000004vTMx]
10:17:51.147 (147217000)|SYSTEM_METHOD_ENTRY|[5]|LIST<Account>.iterator()
10:17:51.147 (147576000)|SYSTEM_METHOD_EXIT|[5]|LIST<Account>.iterator()
10:17:51.147 (147634000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
10:17:51.147 (147656000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
10:17:51.147 (147727000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
10:17:51.147 (147741000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
10:17:51.147 (147769000)|SYSTEM_METHOD_ENTRY|[16]|MAP<String,Market_Assignments__c>.isEmpty()
10:17:51.147 (147804000)|SYSTEM_METHOD_EXIT|[16]|MAP<String,Market_Assignments__c>.isEmpty()
10:17:51.961 (147826000)|CUMULATIVE_LIMIT_USAGE
10:17:51.961|LIMIT_USAGE_FOR_NS|(default)|

 

ForcepowerForcepower

I suspect the condition Trigger.oldMap.get(a.id).Market_assignment_copy__c == a.Market_assignment_copy__c

 

is not occurring. Is it the right conditon to check for? Perhaps you an disable that condition for now and see if it goes into the code that it's not hitting.

megan.burchmegan.burch

Maybe I'm making this too complicated. I can add a formula fied that pulls the correct manager from the market object but then I need that value to beocme the owner, which is where I'm struggeling.

ForcepowerForcepower
Megan, I don't think you're making this too complicated. You could probably do a Workflow instead and update the Owner Id that way. It may be the simpler approach.

I see you're testing this on an update. I doubt that you'll get the ownerid coming in unless the owner was changed as part of the update. Give the following a try just to see if you get the owner value into the trigger. Then you can just pursue the workflow path - should be fairly easy to set up.

Trigger marketassignmenttrigger on Account (before Insert, before Update) { Map<String, Market_Assignments__c> junctionMap = new Map<String, Market_Assignments__c>{}; for (Account a : Trigger.new) { System.debug('a.owner = ' + a.owner + ' a.ownerid:' + a.ownerid);
if (a.owner != null) /*if (a.owner != null && (Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(a.id).Market_assignment_copy__c == a.Market_assignment_copy__c))) */ { //Ignore casing in the map junctionMap.put(a.Market_assignment_copy__c.toLowerCase(), null); System.debug('a.ownerid:' + a.ownerid); } } if (!junctionMap.isEmpty()) { for (Market_Assignments__c item : [ select Id, market_manager__c, name from Market_Assignments__c where market_manager__c IN :junctionMap.keySet()] ) { junctionMap.put(item.name.toLowerCase(), item); } for (Account a : Trigger.new) { String ownerIdStr = a.ownerid; if (ownerIdStr != null && ownerIdStr.startsWith( '00560000001267d')) { System.debug('a.ownerid:' + a.ownerid); Market_Assignments__c m = junctionMap.get(a.market_assignment_copy__c.toLowerCase()); { a.ownerid = m.market_manager__c; } } } } }



Ram

 

megan.burchmegan.burch

Thanks. I wanted to use a workflow but the field I need to update is a lookup so I can't use a formula. Is that correct?

ForcepowerForcepower

I don't know offhand whether that's not allowed. But it should be easy enough to create a workflow and see if you can use your formula. Is the owner coming thru' on the trigger?

megan.burchmegan.burch

No the owner isn't coming through on the trigger. And a workflow won't work becuase of the lookup field.

ForcepowerForcepower

ok. Give the following a try. This is mostly oriented towards an Update rather than an Insert. I suspect you will not need the trigger on an Insert - but can address it once the Update works correcty.

 

Trigger marketassignmenttrigger on Account (before Insert, before Update) {
    
    Map<String, Market_Assignments__c> junctionMap = new Map<String, Market_Assignments__c>{};
    List<Id> accountIds = new List<Id>();
    for (Account a : Trigger.new)
    {   
    accountIds.add(a.id);
    }
    List<Account> accounts = [select id, ownerId, Market_assignment_copy__c from Account where id in :accountIds];
    Map<Id, Account> accountsMap = new Map<Id, Account>();
    for (Account a : accounts)
    {   
    accountsMap.put(a.id, a);
    }
    for (Account a : Trigger.new)
    {   
    Account a1 = accountsMap.get(a.id);
    System.debug('a1.owner = ' + a1.owner + ' a1.ownerid:' + a1.ownerid);
        if (a1.owner != null &&
        (Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(a.id).Market_assignment_copy__c == a1.Market_assignment_copy__c))) */
        {            
            //Ignore casing in the map
            junctionMap.put(a.Market_assignment_copy__c.toLowerCase(), null);
            System.debug('a.ownerid:' + a.ownerid);           
        }
    }
    
    if (!junctionMap.isEmpty())
    {    
        
        for (Market_Assignments__c item : [ select Id, market_manager__c, name from Market_Assignments__c where market_manager__c IN :junctionMap.keySet()] )
        {
            junctionMap.put(item.name.toLowerCase(), item);
        }
                        
        for (Account a : Trigger.new)
        {
         Account a1 = accountsMap.get(a.id);

             String ownerIdStr = a1.ownerid;

                    if (ownerIdStr != null && ownerIdStr.startsWith( '00560000001267d'))
             {
            System.debug('a1.ownerid:' + a1.ownerid);
               Market_Assignments__c m = junctionMap.get(a1.market_assignment_copy__c.toLowerCase());
                
                
                {
                    a.ownerid = m.market_manager__c;                
                }
                
            }
        }
    }    
}


ForcepowerForcepower

One minor change:

 

if (a1.ownerid != null

instead of

if (a1.owner != null

megan.burchmegan.burch

Thank you so much! I put that in and then got this error when I tried to update an account.

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger marketassignmenttrigger caused an unexpected exception, contact your administrator: marketassignmenttrigger: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Owner: Trigger.marketassignmenttrigger: line 18, column 1

ForcepowerForcepower

Megan,

Change this:

    System.debug('a1.owner = ' + a1.owner + ' a1.ownerid:' + a1.ownerid);

to this:
    System.debug(' a1.ownerid:' + a1.ownerid);


megan.burchmegan.burch

Thanks, I made the change and am getting this error.

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger marketassignmenttrigger caused an unexpected exception, contact your administrator: marketassignmenttrigger: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.marketassignmenttrigger: line 49, column 1

ForcepowerForcepower

I think you're dealing with a null object for m:

              Market_Assignments__c m = junctionMap.get(a1.market_assignment_copy__c.toLowerCase());
                // add this check
                if (m != null)
                {
                    a.ownerid = m.market_manager__c;                
                }

You may want to put a debug in your for statement earlier and make sure you're loading your map correctly:

 

       
        for (Market_Assignments__c item : [ select Id, market_manager__c, name from Market_Assignments__c where market_manager__c IN :junctionMap.keySet()] )
        {

            System.debug(' item name ' + item.name.toLowerCase());
            junctionMap.put(item.name.toLowerCase(), item);
        }

 

 

Ram

 

megan.burchmegan.burch

Thank you very much. I'm not getting any errors, but when I update the account the owner is still not changing.

 

 

ForcepowerForcepower

Try copying this and running it. You'll need to resort to looking at the debug logs to see what is happening. I've highlighted the portions of the code you'll need to focus on and tweak if needed:

 

Trigger marketassignmenttrigger on Account (before Insert, before Update) {
    
    Map<String, Market_Assignments__c> junctionMap = new Map<String, Market_Assignments__c>{};
    List<Id> accountIds = new List<Id>();
    for (Account a : Trigger.new)
    {   
    accountIds.add(a.id);
    }
    List<Account> accounts = [select id, ownerId, Market_assignment_copy__c from Account where id in :accountIds];
    Map<Id, Account> accountsMap = new Map<Id, Account>();
    for (Account a : accounts)
    {   
    accountsMap.put(a.id, a);
    }
    for (Account a : Trigger.new)
    {   
    Account a1 = accountsMap.get(a.id);
    System.debug('a1.owner = ' + a1.owner + ' a1.ownerid:' + a1.ownerid);
        if (a1.ownerid != null &&
        (Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(a.id).Market_assignment_copy__c == a1.Market_assignment_copy__c))) */
        {            
            //Ignore casing in the map
            junctionMap.put(a1.Market_assignment_copy__c.toLowerCase(), null);

            // do we see anything in the logs?
            System.debug('a1.ownerid:' + a1.ownerid);           
        }
    }
    
    if (!junctionMap.isEmpty())
    {    
        
        for (Market_Assignments__c item : [ select Id, market_manager__c, name from Market_Assignments__c where market_manager__c IN :junctionMap.keySet()] )
        {
            // do we see anything in the logs?

            System.debug('item name:' + item.name.toLowerCase());           
            junctionMap.put(item.name.toLowerCase(), item);
        }
                        
        for (Account a : Trigger.new)
        {
         Account a1 = accountsMap.get(a.id);

             String ownerIdStr = a1.ownerid;

                    if (ownerIdStr != null && ownerIdStr.startsWith( '00560000001267d'))
             {
            System.debug('a1.ownerid:' + a1.ownerid);
               Market_Assignments__c m = junctionMap.get(a1.market_assignment_copy__c.toLowerCase());
                
                if (m != null)
                {
                    a.ownerid = m.market_manager__c;                
                }
                
            }
        }
    }    
}

megan.burchmegan.burch

Below is the debug log. I think the problem that when it's querying the market assignments its not bringing anything back but I'm not sure why.

 

11:37:36.070 (70192000)|CODE_UNIT_FINISHED|AccountTrigger on Account trigger event BeforeUpdate for [001g0000004vu35]
11:37:36.083 (83096000)|CODE_UNIT_STARTED|[EXTERNAL]|01qg0000000CfJD|marketassignmenttrigger on Account trigger event BeforeUpdate for [001g0000004vu35]
11:37:36.083 (83463000)|SYSTEM_CONSTRUCTOR_ENTRY|[4]|<init>()
11:37:36.083 (83491000)|SYSTEM_CONSTRUCTOR_EXIT|[4]|<init>()
11:37:36.083 (83565000)|SYSTEM_METHOD_ENTRY|[5]|LIST<Account>.iterator()
11:37:36.083 (83822000)|SYSTEM_METHOD_EXIT|[5]|LIST<Account>.iterator()
11:37:36.083 (83846000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
11:37:36.083 (83864000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
11:37:36.083 (83948000)|SYSTEM_METHOD_ENTRY|[7]|LIST<Id>.add(Object)
11:37:36.083 (83974000)|SYSTEM_METHOD_EXIT|[7]|LIST<Id>.add(Object)
11:37:36.083 (83984000)|SYSTEM_METHOD_ENTRY|[5]|system.ListIterator.hasNext()
11:37:36.083 (83996000)|SYSTEM_METHOD_EXIT|[5]|system.ListIterator.hasNext()
11:37:36.084 (84286000)|SOQL_EXECUTE_BEGIN|[9]|Aggregations:0|select id, ownerId, Market_assignment_copy__c from Account where id IN :tmpVar1
11:37:36.098 (98567000)|SOQL_EXECUTE_END|[9]|Rows:1
11:37:36.098 (98709000)|SYSTEM_METHOD_ENTRY|[11]|LIST<Account>.iterator()
11:37:36.098 (98742000)|SYSTEM_METHOD_EXIT|[11]|LIST<Account>.iterator()
11:37:36.098 (98756000)|SYSTEM_METHOD_ENTRY|[11]|system.ListIterator.hasNext()
11:37:36.098 (98771000)|SYSTEM_METHOD_EXIT|[11]|system.ListIterator.hasNext()
11:37:36.098 (98844000)|SYSTEM_METHOD_ENTRY|[13]|MAP<Id,Account>.put(Object, Object)
11:37:36.098 (98874000)|SYSTEM_METHOD_EXIT|[13]|MAP<Id,Account>.put(Object, Object)
11:37:36.098 (98883000)|SYSTEM_METHOD_ENTRY|[11]|system.ListIterator.hasNext()
11:37:36.098 (98895000)|SYSTEM_METHOD_EXIT|[11]|system.ListIterator.hasNext()
11:37:36.098 (98917000)|SYSTEM_METHOD_ENTRY|[15]|LIST<Account>.iterator()
11:37:36.098 (98935000)|SYSTEM_METHOD_EXIT|[15]|LIST<Account>.iterator()
11:37:36.098 (98944000)|SYSTEM_METHOD_ENTRY|[15]|system.ListIterator.hasNext()
11:37:36.098 (98954000)|SYSTEM_METHOD_EXIT|[15]|system.ListIterator.hasNext()
11:37:36.098 (98992000)|SYSTEM_METHOD_ENTRY|[17]|MAP<Id,Account>.get(Object)
11:37:36.099 (99014000)|SYSTEM_METHOD_EXIT|[17]|MAP<Id,Account>.get(Object)
11:37:36.099 (99069000)|SYSTEM_METHOD_ENTRY|[18]|String.valueOf(Object)
11:37:36.099 (99094000)|SYSTEM_METHOD_EXIT|[18]|String.valueOf(Object)
11:37:36.099 (99117000)|SYSTEM_METHOD_ENTRY|[18]|System.debug(ANY)
11:37:36.099 (99125000)|USER_DEBUG|[18]|DEBUG| a1.ownerid:00560000001267dAAA
11:37:36.099 (99131000)|SYSTEM_METHOD_EXIT|[18]|System.debug(ANY)
11:37:36.099 (99176000)|SYSTEM_METHOD_ENTRY|[19]|MAP<Id,Account>.get(Object)
11:37:36.099 (99202000)|SYSTEM_METHOD_EXIT|[19]|MAP<Id,Account>.get(Object)
11:37:36.108 (108397000)|SYSTEM_METHOD_ENTRY|[23]|MAP<String,Market_Assignments__c>.put(Object, Object)
11:37:36.108 (108430000)|SYSTEM_METHOD_EXIT|[23]|MAP<String,Market_Assignments__c>.put(Object, Object)
11:37:36.108 (108450000)|SYSTEM_METHOD_ENTRY|[26]|String.valueOf(Object)
11:37:36.108 (108467000)|SYSTEM_METHOD_EXIT|[26]|String.valueOf(Object)
11:37:36.108 (108478000)|SYSTEM_METHOD_ENTRY|[26]|System.debug(ANY)
11:37:36.108 (108483000)|USER_DEBUG|[26]|DEBUG|a1.ownerid:00560000001267dAAA
11:37:36.108 (108488000)|SYSTEM_METHOD_EXIT|[26]|System.debug(ANY)
11:37:36.108 (108498000)|SYSTEM_METHOD_ENTRY|[15]|system.ListIterator.hasNext()
11:37:36.108 (108511000)|SYSTEM_METHOD_EXIT|[15]|system.ListIterator.hasNext()
11:37:36.108 (108530000)|SYSTEM_METHOD_ENTRY|[30]|MAP<String,Market_Assignments__c>.isEmpty()
11:37:36.108 (108558000)|SYSTEM_METHOD_EXIT|[30]|MAP<String,Market_Assignments__c>.isEmpty()
11:37:36.108 (108588000)|SYSTEM_METHOD_ENTRY|[33]|MAP<String,Market_Assignments__c>.keySet()
11:37:36.108 (108673000)|SYSTEM_METHOD_EXIT|[33]|MAP<String,Market_Assignments__c>.keySet()
11:37:36.108 (108971000)|SOQL_EXECUTE_BEGIN|[33]|Aggregations:0|select Id, market_manager__c, name from Market_Assignments__c 
11:37:36.115 (115023000)|SOQL_EXECUTE_END|[33]|Rows:0
11:37:36.115 (115084000)|SYSTEM_METHOD_ENTRY|[33]|Database.QueryLocator.iterator()
11:37:36.115 (115271000)|SYSTEM_METHOD_ENTRY|[7]|QueryLocatorIterator.QueryLocatorIterator()
11:37:36.115 (115282000)|SYSTEM_METHOD_EXIT|[7]|QueryLocatorIterator
11:37:36.115 (115337000)|SYSTEM_METHOD_EXIT|[33]|Database.QueryLocator.iterator()
11:37:36.115 (115353000)|SYSTEM_METHOD_ENTRY|[33]|Database.QueryLocatorIterator.hasNext()
11:37:36.115 (115373000)|SYSTEM_METHOD_EXIT|[33]|Database.QueryLocatorIterator.hasNext()
11:37:36.115 (115401000)|SYSTEM_METHOD_ENTRY|[41]|LIST<Account>.iterator()
11:37:36.115 (115424000)|SYSTEM_METHOD_EXIT|[41]|LIST<Account>.iterator()
11:37:36.115 (115435000)|SYSTEM_METHOD_ENTRY|[41]|system.ListIterator.hasNext()
11:37:36.115 (115451000)|SYSTEM_METHOD_EXIT|[41]|system.ListIterator.hasNext()
11:37:36.115 (115495000)|SYSTEM_METHOD_ENTRY|[43]|MAP<Id,Account>.get(Object)
11:37:36.115 (115518000)|SYSTEM_METHOD_EXIT|[43]|MAP<Id,Account>.get(Object)
11:37:36.115 (115557000)|SYSTEM_METHOD_ENTRY|[49]|String.valueOf(Object)
11:37:36.115 (115573000)|SYSTEM_METHOD_EXIT|[49]|String.valueOf(Object)
11:37:36.115 (115583000)|SYSTEM_METHOD_ENTRY|[49]|System.debug(ANY)
11:37:36.115 (115589000)|USER_DEBUG|[49]|DEBUG|a1.ownerid:00560000001267dAAA
11:37:36.115 (115594000)|SYSTEM_METHOD_EXIT|[49]|System.debug(ANY)
11:37:36.115 (115624000)|SYSTEM_METHOD_ENTRY|[50]|MAP<String,Market_Assignments__c>.get(Object)
11:37:36.115 (115646000)|SYSTEM_METHOD_EXIT|[50]|MAP<String,Market_Assignments__c>.get(Object)
11:37:36.115 (115658000)|SYSTEM_METHOD_ENTRY|[41]|system.ListIterator.hasNext()
11:37:36.115 (115670000)|SYSTEM_METHOD_EXIT|[41]|system.ListIterator.hasNext()
11:37:36.888 (115691000)|CUMULATIVE_LIMIT_USAGE
11:37:36.888|LIMIT_USAGE_FOR_NS|(default)|

 

ForcepowerForcepower

Try printing the market assignment value and see if that is as it should be. Since you are converting to lower case, perhaps you may not be getting a match (SOQL typically ignores case but just to make sure). Use the code below:

 

Trigger marketassignmenttrigger on Account (before Insert, before Update) {
    
    Map<String, Market_Assignments__c> junctionMap = new Map<String, Market_Assignments__c>{};
    List<Id> accountIds = new List<Id>();
    for (Account a : Trigger.new)
    {   
    accountIds.add(a.id);
    }
    List<Account> accounts = [select id, ownerId, Market_assignment_copy__c from Account where id in :accountIds];
    Map<Id, Account> accountsMap = new Map<Id, Account>();
    for (Account a : accounts)
    {   
    accountsMap.put(a.id, a);
    }
    for (Account a : Trigger.new)
    {   
    Account a1 = accountsMap.get(a.id);
    System.debug('a1.owner = ' + a1.owner + ' a1.ownerid:' + a1.ownerid);
        if (a1.ownerid != null &&
        (Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(a.id).Market_assignment_copy__c == a1.Market_assignment_copy__c))) */
        {            
            //Ignore casing in the map
            junctionMap.put(a1.Market_assignment_copy__c.toLowerCase(), null);
            System.debug(' market assignment = ' + a1.Market_assignment_copy__c.toLowerCase());
            System.debug('a1.ownerid:' + a1.ownerid);           
        }
    }
    
    if (!junctionMap.isEmpty())
    {    
        
        for (Market_Assignments__c item : [ select Id, market_manager__c, name from Market_Assignments__c where market_manager__c IN :junctionMap.keySet()] )
        {
            System.debug('item name:' + item.name.toLowerCase());           
            junctionMap.put(item.name.toLowerCase(), item);
        }
                        
        for (Account a : Trigger.new)
        {
         Account a1 = accountsMap.get(a.id);

             String ownerIdStr = a1.ownerid;

                    if (ownerIdStr != null && ownerIdStr.startsWith( '00560000001267d'))
             {
            System.debug('matched default owner - a1.ownerid:' + a1.ownerid);
               Market_Assignments__c m = junctionMap.get(a1.market_assignment_copy__c.toLowerCase());
                
                if (m != null)
                {
                    a.ownerid = m.market_manager__c;                
                }
                
            }
        }
    }    
}

megan.burchmegan.burch

It's grabbing the correct market assignment. I don't know why I set it to lower case. I dont' think I needed to do that. Can I just remove everywhere it has it as lower case?

 

13:40:17.162 (162509000)|SYSTEM_METHOD_ENTRY|[24]|System.debug(ANY)
13:40:17.162 (162523000)|USER_DEBUG|[24]|DEBUG| market assignment = hanoi
13:40:17.162 (162530000)|SYSTEM_METHOD_EXIT|[24]|System.debug(ANY)
13:40:17.162 (162534000)|STATEMENT_EXECUTE|[25]
13:40:17.162 (162540000)|HEAP_ALLOCATE|[25]|Bytes:11
13:40:17.162 (162557000)|SYSTEM_METHOD_ENTRY|[25]|String.valueOf(Object)
13:40:17.162 (162571000)|HEAP_ALLOCATE|[25]|Bytes:18

 

ForcepowerForcepower

Yep - you can try removing the lower case altogether and see if it makes a difference.

megan.burchmegan.burch

I removed it and am getting the same result.

ForcepowerForcepower

Megan,

 

I suspect you're not querying with the right selection criteria:

Your junction map key is Market_assignment_copy__c - is this the same value as market_manager__c in Market_Assignments__c ?

 

Is hanoi the name of a marketing manager? I suspect you're comparing this with market_manager__c which I'm guessing is an id. If that is the case, your query below needs to change so that it is grabbing the right value to compare (perhaps market_manager__r.Name?)

 

        for (Market_Assignments__c item : [ select Id, market_manager__c, name from Market_Assignments__c where market_manager__c IN :junctionMap.keySet()] )

megan.burchmegan.burch

Market_assignment_copy__c in accounts would be the same as the name in the market assignement object

 

Market_Manager__c in market assignments is a lookup of the user and the value I want to populate in the owner field.

 

Should I be looking up the name in the query?

ForcepowerForcepower

Yep - try this:

        for (Market_Assignments__c item :
        [ select Id, market_manager__c, name from Market_Assignments__c where Name IN :junctionMap.keySet()] )

megan.burchmegan.burch

Still nothing : ( I'm sorry this is so frustrating, thank you for your help! This is what I'm working with now

Trigger marketassignmenttrigger on Account (before Insert, before Update) {
    
    Map<String, Market_Assignments__c> junctionMap = new Map<String, Market_Assignments__c>{};
    List<Id> accountIds = new List<Id>();
    for (Account a : Trigger.new)
    {   
    accountIds.add(a.id);
    }
    List<Account> accounts = [select id, ownerId, Market_assignment_copy__c from Account where id in :accountIds];
    Map<Id, Account> accountsMap = new Map<Id, Account>();
    for (Account a : accounts)
    {   
    accountsMap.put(a.id, a);
    }
    for (Account a : Trigger.new)
    {   
    Account a1 = accountsMap.get(a.id);
     System.debug(' a1.ownerid:' + a1.ownerid);
        if (a1.ownerid != null &&
        (Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(a.id).Market_assignment_copy__c == a1.Market_assignment_copy__c)))
        {            
            //Ignore casing in the map
            //junctionMap.put(a1.Market_assignment_copy__c.toLowerCase(), null);
           System.debug(' market assignment = ' + a1.Market_assignment_copy__c);
           System.debug('a1.ownerid:' + a1.ownerid);           
        }
    }
    
    if (!junctionMap.isEmpty())
    {    
        
         for (Market_Assignments__c item :
        [ select Id, market_manager__c, name from Market_Assignments__c where Name IN :junctionMap.keySet()] )
        {
            //System.debug('item name:' + item.name.toLowerCase());           
            //junctionMap.put(item.name.toLowerCase(), item);
        }
                        
        for (Account a : Trigger.new)
        {
         Account a1 = accountsMap.get(a.id);

             String ownerIdStr = a1.ownerid;

                    if (ownerIdStr != null && ownerIdStr.startsWith( '00560000001267d'))
             {
            System.debug('matched default owner - a1.ownerid:' + a1.ownerid);
               Market_Assignments__c m = junctionMap.get(a1.market_assignment_copy__c);
                
                if (m != null)
                {
                    a.ownerid = m.market_manager__c;                
                }
                
            }
        }
    }    
}

 

ForcepowerForcepower

I think you should leave the debug statements in there so that you can see what's happening. Actually, I see you commented out the line that was putting these items into junctionMap and that is probably why it's not working. I  was just saying you should remove the .toLowerCase call, not the entire lines.

 

         for (Market_Assignments__c item :
        [ select Id, market_manager__c, name from Market_Assignments__c where Name IN :junctionMap.keySet()] )
        {
            System.debug('item name:' + item.name);           
            junctionMap.put(item.name, item);        }
megan.burchmegan.burch

Thank you. It's still not working but the debug statements look right. It's pulling the correct value for market but the owner is not changing. Is the problem where it says [29]|MAP<String,Market_Assignments__c>.isEmpty()?

 

09:07:35.069 (69580000)|USER_DEBUG|[18]|DEBUG| a1.ownerid:00560000001267dAAA
09:07:35.069 (69587000)|SYSTEM_METHOD_EXIT|[18]|System.debug(ANY)
09:07:35.069 (69628000)|SYSTEM_METHOD_ENTRY|[19]|MAP<Id,Account>.get(Object)
09:07:35.069 (69659000)|SYSTEM_METHOD_EXIT|[19]|MAP<Id,Account>.get(Object)
09:07:35.081 (81679000)|SYSTEM_METHOD_ENTRY|[24]|System.debug(ANY)
09:07:35.081 (81718000)|USER_DEBUG|[24]|DEBUG| market assignment = Las Vegas
09:07:35.081 (81727000)|SYSTEM_METHOD_EXIT|[24]|System.debug(ANY)
09:07:35.081 (81743000)|SYSTEM_METHOD_ENTRY|[25]|String.valueOf(Object)
09:07:35.081 (81760000)|SYSTEM_METHOD_EXIT|[25]|String.valueOf(Object)
09:07:35.081 (81769000)|SYSTEM_METHOD_ENTRY|[25]|System.debug(ANY)
09:07:35.081 (81776000)|USER_DEBUG|[25]|DEBUG|a1.ownerid:00560000001267dAAA
09:07:35.081 (81782000)|SYSTEM_METHOD_EXIT|[25]|System.debug(ANY)
09:07:35.081 (81793000)|SYSTEM_METHOD_ENTRY|[15]|system.ListIterator.hasNext()
09:07:35.081 (81807000)|SYSTEM_METHOD_EXIT|[15]|system.ListIterator.hasNext()
09:07:35.081 (81833000)|SYSTEM_METHOD_ENTRY|[29]|MAP<String,Market_Assignments__c>.isEmpty()
09:07:35.081 (81864000)|SYSTEM_METHOD_EXIT|[29]|MAP<String,Market_Assignments__c>.isEmpty()

 

ForcepowerForcepower
Yes - looks like another commented out line because it had "toLowerCase" in it. 
//junctionMap.put(a1.Market_assignment_copy__c.toLowerCase(), null);
junctionMap.put(a1.Market_assignment_copy__c, null);
megan.burchmegan.burch

Yea, I figured that was it. It's chaning the Owner now, but capturing the wrong market and therefore changing it to the wrong owner. The market was Las Vegas but it's capturing Chicago.

 

09:45:09.100 (100197000)|SYSTEM_METHOD_ENTRY|[23]|MAP<String,Market_Assignments__c>.put(Object, Object)
09:45:09.100 (100230000)|SYSTEM_METHOD_EXIT|[23]|MAP<String,Market_Assignments__c>.put(Object, Object)
09:45:09.100 (100250000)|SYSTEM_METHOD_ENTRY|[24]|System.debug(ANY)
09:45:09.100 (100267000)|USER_DEBUG|[24]|DEBUG| market assignment = Chicago
09:45:09.100 (100273000)|SYSTEM_METHOD_EXIT|[24]|System.debug(ANY)
09:45:09.100 (100288000)|SYSTEM_METHOD_ENTRY|[25]|String.valueOf(Object)
09:45:09.100 (100301000)|SYSTEM_METHOD_EXIT|[25]|String.valueOf(Object)
09:45:09.100 (100322000)|SYSTEM_METHOD_ENTRY|[25]|System.debug(ANY)
09:45:09.100 (100334000)|USER_DEBUG|[25]|DEBUG|a1.ownerid:00560000001267dAAA
09:45:09.100 (100348000)|SYSTEM_METHOD_EXIT|[25]|System.debug(ANY)
09:45:09.100 (100364000)|SYSTEM_METHOD_ENTRY|[15]|system.ListIterator.hasNext()
09:45:09.100 (100385000)|SYSTEM_METHOD_EXIT|[15]|system.ListIterator.hasNext()
09:45:09.100 (100413000)|SYSTEM_METHOD_ENTRY|[29]|MAP<String,Market_Assignments__c>.isEmpty()
09:45:09.100 (100456000)|SYSTEM_METHOD_EXIT|[29]|MAP<String,Market_Assignments__c>.isEmpty()
09:45:09.100 (100496000)|SYSTEM_METHOD_ENTRY|[32]|MAP<String,Market_Assignments__c>.keySet()
09:45:09.100 (100607000)|SYSTEM_METHOD_EXIT|[32]|MAP<String,Market_Assignments__c>.keySet()
09:45:09.101 (101026000)|SOQL_EXECUTE_BEGIN|[32]|Aggregations:0|select Id, market_manager__c, name from Market_Assignments__c 
09:45:09.104 (104725000)|SOQL_EXECUTE_END|[32]|Rows:1
09:45:09.104 (104793000)|SYSTEM_METHOD_ENTRY|[32]|Database.QueryLocator.iterator()
09:45:09.104 (104988000)|SYSTEM_METHOD_ENTRY|[7]|QueryLocatorIterator.QueryLocatorIterator()
09:45:09.104 (104998000)|SYSTEM_METHOD_EXIT|[7]|QueryLocatorIterator
09:45:09.105 (105103000)|SYSTEM_METHOD_EXIT|[32]|Database.QueryLocator.iterator()
09:45:09.105 (105123000)|SYSTEM_METHOD_ENTRY|[32]|Database.QueryLocatorIterator.hasNext()
09:45:09.105 (105192000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Market_Assignments__c>.size()
09:45:09.105 (105220000)|SYSTEM_METHOD_EXIT|[33]|LIST<Market_Assignments__c>.size()
09:45:09.105 (105232000)|SYSTEM_METHOD_EXIT|[32]|Database.QueryLocatorIterator.hasNext()
09:45:09.105 (105242000)|SYSTEM_METHOD_ENTRY|[32]|Database.QueryLocatorIterator.next()
09:45:09.105 (105262000)|SYSTEM_METHOD_ENTRY|[48]|Database.QueryLocatorIterator.hasNext()
09:45:09.105 (105281000)|SYSTEM_METHOD_ENTRY|[33]|LIST<Market_Assignments__c>.size()
09:45:09.105 (105290000)|SYSTEM_METHOD_EXIT|[33]|LIST<Market_Assignments__c>.size()
09:45:09.105 (105299000)|SYSTEM_METHOD_EXIT|[48]|Database.QueryLocatorIterator.hasNext()
09:45:09.105 (105334000)|SYSTEM_METHOD_EXIT|[32]|Database.QueryLocatorIterator.next()
09:45:09.105 (105376000)|SYSTEM_METHOD_ENTRY|[35]|System.debug(ANY)
09:45:09.105 (105397000)|USER_DEBUG|[35]|DEBUG|item name:Chicago
09:45:09.105 (105404000)|SYSTEM_METHOD_EXIT|[35]|System.debug(ANY)
09:45:09.105 (105435000)|SYSTEM_METHOD_ENTRY|[36]|MAP<String,Market_Assignments__c>.put(Object, Object)
09:45:09.105 (105462000)|SYSTEM_METHOD_EXIT|[36]|MAP<String,Market_Assignments__c>.put(Object, Object)
09:45:09.105 (105472000)|SYSTEM_METHOD_ENTRY|[32]|Database.QueryLocatorIterator.hasNext()
09:45:09.105 (105484000)|SYSTEM_METHOD_EXIT|[32]|Database.QueryLocatorIterator.hasNext()
09:45:09.105 (105507000)|SYSTEM_METHOD_ENTRY|[39]|LIST<Account>.iterator()
09:45:09.105 (105528000)|SYSTEM_METHOD_EXIT|[39]|LIST<Account>.iterator()
09:45:09.105 (105538000)|SYSTEM_METHOD_ENTRY|[39]|system.ListIterator.hasNext()
09:45:09.105 (105549000)|SYSTEM_METHOD_EXIT|[39]|system.ListIterator.hasNext()
09:45:09.105 (105587000)|SYSTEM_METHOD_ENTRY|[41]|MAP<Id,Account>.get(Object)
09:45:09.105 (105610000)|SYSTEM_METHOD_EXIT|[41]|MAP<Id,Account>.get(Object)
09:45:09.105 (105655000)|SYSTEM_METHOD_ENTRY|[47]|String.valueOf(Object)
09:45:09.105 (105675000)|SYSTEM_METHOD_EXIT|[47]|String.valueOf(Object)
09:45:09.105 (105684000)|SYSTEM_METHOD_ENTRY|[47]|System.debug(ANY)

 

ForcepowerForcepower
It looks like your market assignment did come in as Chicago (not Las Vegas) and the trigger code seems to have worked correctly.

"market assignment = Chicago"
megan.burchmegan.burch

Sorry, I know what happened there.

 

Thank you so much for all your help I really appreciate it!

ForcepowerForcepower
Glad to hear it, Megan! You're very welcome!
Ram
megan.burchmegan.burch

Would it be possible for you to take a look at my test class? I can't get the assertion to work becuase I'm comparing id vs name. Any suggestions would be great. Thank you so much!

 

@istest 
public class marketassignmenttrigertest{
    static testMethod void marketassignmenttrigertest(){

        geo_codes__c geo = new geo_codes__c();
        geo.name = 'test';
        insert geo;
        
        Market_Assignments__c ma = new Market_Assignments__c ();
        ma.name = 'test';
        ma.market_manager__c = 'test 1';
        insert ma;

        Geojunction__c g1 = new Geojunction__c();        
        g1.name = 'test';  
        g1.geo_codes__c = geo.id;
        g1.Market_Assignments__c = ma.id;      
        insert g1;
        
        Account a1 = new Account();        
        a1.Name = 'Test Account';
        a1.market__c = g1.name;                 
        insert a1;
        a1 = [Select name, OwnerId from Account where Id = '00560000001267d'];

        a1 = [ select Id, market_assignment_copy__c from Account where Id = :a1.Id];        
        system.assertequals(a1.owner,ma.market_manager__c);
        
       
    }
}

 

ForcepowerForcepower

Megan,

 

 I thought market_manager__c was a lookup to a user. A tad confused with what's going on the test class. You may want to assign the id to a String and see if you can compare two strings.

 

Ram

megan.burchmegan.burch

It is a lookup to the user.

megan.burchmegan.burch

I'm sorry but I have yet another question!

 

The trigger is failing when I try to do an insert from the (x, x) user ID. I think it's becuase the market assignment field it looks at is a formula field pulling it from another object and therefore isn't populated yet. Do you have any suggestions on how to fix this?


Thanks

ForcepowerForcepower

Megan,

 

I guess the trigger had been very geared towards updates. I think the following changes should help with the Inserts.

 

Just add the following line in bold wherever you have the first line (should be two places like that)

 

        Account a1 = accountsMap.get(a.id);
        if (a1 == null) a1 = a;

Ram

    for (Account a : Trigger.new)
    {   
        if (a.ownerid != null && Trigger.isInsert) {
            
            junctionMap.put(a.Market_assignment_copy__c, null);
            System.debug(' market assignment = ' + a.Market_assignment_copy__c);
            System.debug('a.ownerid:' + a.ownerid);
                       
        }
        else
        if (a1.ownerid != null &&
        (Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(a.id).Market_assignment_copy__c == a1.Market_assignment_copy__c))) */
        {            
            //Ignore casing in the map
            junctionMap.put(a1.Market_assignment_copy__c, null);
            System.debug(' market assignment = ' + a1.Market_assignment_copy__c);
            System.debug('a1.ownerid:' + a1.ownerid);           
        }
        
    }

This was selected as the best answer
megan.burchmegan.burch

Thanks! That worked.

ForcepowerForcepower

Glad to hear it, Megan!

Ram

megan.burchmegan.burch

Hi Ram,

 

I'm running into problems with this tirgger and thought that you may be able to help. Some of our makret managers on market assignements are out of date so it's trying to assign the account to an inactive owner and failing.

 

Where would I add an if statement to check if the user is active?

ForcepowerForcepower

Megan,

 

If the assignment fails in the trigger for an inactive user, the easies thing would be to catch the exception and assign a "fall back" owner:

 

                if (m != null)
                {
try { a.ownerid = m.market_manager__c;
}
catch (Exception e) {
// assign a fall back owner id here
}
}

If it does not explicitly fail in the assignment of an inactive owner id, we may have to query the User table and make sure the m.market_manager__c is actually active before we use it.

Hope that helps,
Ram
megan.burchmegan.burch

Thanks Ram. I think I should try to cath it more upstream and creat a trigger on the market assignements that checks if the user in market manager field there is active and if not assign it to a default owner. Is that reasonable?

ForcepowerForcepower

I agree, Megan. I think that's a good strategy.

Ram

megan.burchmegan.burch

Thank you,

 

I tried doing that, but I keep getting a complile error on line 3 .

 

Error: Invalid type: market_manager__c at line 3 column 63

trigger Active_Market_Managers on Market_Assignments__c (before insert, before update) {

Map<string, market_manager__c> junctionmap = new Map <string, market_manager__c>{};

List<Id> marketmanagerids = new List<Id>();

for (Market_assignments__c ma : Trigger.new)
{
List<User> Users =[select id, isactive from user where id in:marketmanagerids];
  
    if(MM.isActive = true)
     market_manager__c = MA.market_manager__c;
       else
       market_manager__c = '00560000001267d';
     
  }
}

 Any idea why?

 

Thanks!

 

 

ForcepowerForcepower
Hey Megan,

Is market_manager__c of type Id? If I recollect, it is a field storing the id of a user. I don't think it's a type by itself. Try using Id instead.
Ram
megan.burchmegan.burch

market_manager __c is a lookup to the user

ForcepowerForcepower
ok - I think you'll need that to be

Map<string, Id> junctionmap = new Map <string, Id>{};
or

Map<string, User> junctionmap = new Map <string, User>{};

depending on what you want to store in the map value (an id vs a User)
megan.burchmegan.burch

Thank you! I'm getting an error I haven't gotten before when I put that in

 

Error: Compile Error: Expression cannot be assigned at line -1 column -1

 

Here's what I changed it to.

 

trigger Active_Market_Managers on Market_Assignments__c (before insert, before update) {

Map<string, Id> junctionmap = new Map <string, Id>{};

List<Id> marketmanagerids = new List<Id>();

for (Market_assignments__c ma : Trigger.new)
{
List<User> Users =[select id, isactive from user where id in:marketmanagerids];
  
    if(user.isActive = true)
     ma.market_manager__c = MA.market_manager__c;
       else
       ma.market_manager__c = '00560000001267d';
     
  }
}

 

 

ForcepowerForcepower
Megan,
Careful with your comparisons. It thinks you're assigning (=) rather than comparing (==)
if(user.isActive = true) should be
if(user.isActive == true)
megan.burchmegan.burch

Hi Ram,

 

That's what I thought but when I had

 

if(user.isActive == true) earlier I got this error

 

Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, Boolean at line 11 column 8

ForcepowerForcepower

Megan,

 

You will need to iterate through the List<Users> with a for loop and use a lop variable for user. RIght now you're just jumping right into

 

If (User.IsActive...)

 

which it interprets as the standard object User and its field type IsActive

 

    for (User user1 : users) {    
if (user1.isActive) ma.market_manager__c = MA.market_manager__c; else ma.market_manager__c = '00560000001267d'; }
}
megan.burchmegan.burch

Thank you very much!

 

The trigger is executing but not changing anything. When I look at the debug logs it looks like its not returning anything in the soql query.

 

28.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
10:48:27.045 (45777000)|EXECUTION_STARTED
10:48:27.045 (45813000)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
10:48:27.045 (45837000)|CODE_UNIT_STARTED|[EXTERNAL]|01qg0000000CfkJ|Active_Market_Managers on Market_Assignments trigger event BeforeUpdate for [a0Yg00000005Px3]
10:48:27.047 (47204000)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
10:48:27.047 (47245000)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
10:48:27.047 (47445000)|SYSTEM_METHOD_ENTRY|[7]|LIST<Market_Assignments__c>.iterator()
10:48:27.047 (47782000)|SYSTEM_METHOD_EXIT|[7]|LIST<Market_Assignments__c>.iterator()
10:48:27.047 (47817000)|SYSTEM_METHOD_ENTRY|[7]|system.ListIterator.hasNext()
10:48:27.047 (47847000)|SYSTEM_METHOD_EXIT|[7]|system.ListIterator.hasNext()
10:48:27.050 (50499000)|SOQL_EXECUTE_BEGIN|[9]|Aggregations:0|select id, isactive from user where id IN :tmpVar1
10:48:27.052 (52510000)|SOQL_EXECUTE_END|[9]|Rows:0

 

ForcepowerForcepower
You're not loading the list marketmanagerids. It's empty - so your query brings nothing back.
megan.burchmegan.burch

So add something like the following?

 

MarketmanagerIds.add(ma.id);
ForcepowerForcepower

Megan,

 

I'd suggest putting together a simple flow chart or pseudo code so there's understanding of what is being built. Here are some of the steps involved.

 

Get all record ids from the trigger invocation into a list, recIds. Also load it into a map, trigMap (Id to Marketing_Assignments__c)

Query the object that has the trigger on it (Marketing_Assignments__c) where id is in recIds. Make sure to to get IsActive as part of the query by going against Marketing_Manager__r.IsActive.

Loop through the records from the query checking the user isActive status on each. if user is inactive, fetch the corresponding trigger record from the map and make the change in it.

 

Hope that helps.

Ram

 

megan.burchmegan.burch

Thank you. I really appreciate your help, I'm trying to teach myself this and it's not easy : ). The trigger is changing the user every time, not just when the user is not active so I think I'm having a problem looping through all the records and checking the status on each. Below is my code, is that the problem?

 

trigger Active_Market_Managers on Market_Assignments__c (before insert, before update) {

List<Id> recids = new List<Id>();

Map<id, market_assignments__c> junctionMap = new Map <Id, market_assignments__c>{};
for (market_assignments__c ma :Trigger.new)
{
recids.add(ma.id);
}
List<market_assignments__c> market_assignments = [select id, market_manager__c, Market_Manager__r.IsActive, name FROM market_assignments__c where id in:recids];  


for (Market_assignments__c ma : Trigger.new)
{
     if(ma.Market_Manager__r.IsActive == true)
     {
     ma.market_manager__c = ma.market_manager__c;
     }
       else
      {
       ma.market_manager__c = '00560000001267d';
       }
     
  }
}

 

ForcepowerForcepower

Hey Megan,

 

No worries. Not sure what you mean by "it's changing the user every time". You don't need the following redundant assignment for sure. Change that and see if you still see the same behavior.

 

     if(ma.Market_Manager__r.IsActive == true)
     {
     //ma.market_manager__c = ma.market_manager__c;
     }
ForcepowerForcepower

Actually, you need to get the full record from the map so you can check the isActive value properly:

 

trigger Active_Market_Managers on Market_Assignments__c (before insert, before update) {

List<Id> recids = new List<Id>();

Map<id, market_assignments__c> junctionMap = new Map <Id, market_assignments__c>{};
for (market_assignments__c ma :Trigger.new)
{
recids.add(ma.id);
}
List<market_assignments__c> market_assignments = [select id, market_manager__c, Market_Manager__r.IsActive, name FROM market_assignments__c where id in:recids];  
//need to load this into a map
Map<Id, market_assignments__c> maMap = new Map<Id, market_assignments__c>()

for (market_assignments__c ma : market_assignments) {
maMap.put(ma.id, ma);
}
for (Market_assignments__c maTrig : Trigger.new) {
// get the "full" record from the Map (the trigger record will not have all the details)
Market_assignments__c ma = maMap.get(maTrig.id);
if (!ma.Market_Manager__r.IsActive) {
ma.market_manager__c = '00560000001267d';
}
}
}
ForcepowerForcepower

minor change:

 

    if (!ma.Market_Manager__r.IsActive) { 
maTrig.market_manager__c = '00560000001267d';
}
megan.burchmegan.burch

That didn't work. Whether the user is acitve or not its changing the manager to the default user in the else statement

 


     if(ma.Market_Manager__r.IsActive == true)
     {
   // ma.market_manager__c = ma.market_manager__c;
     }
       else
      {
       ma.market_manager__c = '00560000001267d';
ForcepowerForcepower
Hope you saw my later posts.
megan.burchmegan.burch

Yes, I did. Thank you so much. That seemed to do the trick!

ForcepowerForcepower
Glad to hear it, Megan.
Ram
Mir MarayanMir Marayan
You guess are awesome.Ilearned a lot from you. And I applied it to my new site .plz check this out.
all about Quotes (https://www.100quotes4you.online)
new attitude status (https://www.100quotes4you.online/2020/02/New-attitude-status.html)
New single WhatsApp status (https://www.100quotes4you.online/2020/02/New-single-whatsapp-status.html)
Deep love Quotes (https://www.100quotes4you.online/2020/02/deep-love-quotes-and-status.html)