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
StaciStaci 

update field trigger help

I have a trigger that I want to change the substatus based on the profile of the Case Owner field.  This is what I have, but its not updating the Substatus and I'm not sure if this (objCase.Owner.Profile.Name) is right.  Any help would be grately appreciated.

trigger CW_Update_Substatus_based_on_Case_Owner on Case (before update)
{
    for (Case objCase : Trigger.new)
    {
        if (objCase.Owner.Profile.Name == 'CW Dealer Portal User')               
        {
            objCase.Substatus__c = 'Open with Dealer';
        }else{
            if (objCase.Owner.Profile.Name == 'CW MineStar Support - Tier 2')               
            {
            objCase.Substatus__c = 'Open with Support Advocates';
            }
            else
                {
                objCase.Substatus__c = 'Open with Product Specialists';
                }
    }        }
}
Best Answer chosen by Staci
Abhishek_PareekAbhishek_Pareek
Thanks for pointing out the problem. As the query from case is going to fetch the data from the database and the new values are not commited to database yet, this will fetch profile name for owner's in trigger.old. I 've tweaked the code a bit. See if that works.

trigger CW_Update_Substatus_based_on_Case_Owner on Case (before update)
{
	    Set<Id> ownerIds = new Set<Id>();
	    for(Case objCase : Trigger.new){
	    		ownerIds.add(objCase.OwnerId);
	    }
	    Map<Id,User> userIdToProfileMap = new Map<Id,User>([Select Id,Profile.Name from User where Id IN : ownerIds]); 
	    for (Case objCase : Trigger.new)
	    {
	        if (userIdToProfileMap != null && userIdToProfileMap.containsKey(objCase.OwnerId) 
	        	&& userIdToProfileMap.get(objCase.OwnerId).Profile.Name == 'CW Dealer Portal User')              
	        {
	            objCase.Status = 'Open with Dealer';
	        }else{
	            if (userIdToProfileMap != null && userIdToProfileMap.containsKey(objCase.OwnerId) 
	            	&& userIdToProfileMap.get(objCase.OwnerId).Profile.Name == 'CW MineStar Support - Tier 2')               
	            {
	            objCase.Status = 'Open with Support Advocates';
	            }
	            else
	                {
	                objCase.Status = 'Open with Product Specialists';
	                }
	    }        
     }
}
if this was helpful, hit the Like link.

All Answers

Abhishek_PareekAbhishek_Pareek
In trigger, all the relationship fields should be quried to get their values.You need to query the case object to get value of any field on it's parent object.Something like this:

Map<Id,Case> caseMap = new Map<Id,Case>([Select Id,Owner.Profile.Name from Case where Id IN : Trigger.new]);



and then modify your IF conditions something like this:

if (caseMap != null && caseMap.containsKey(objCase.Id) && caseMap.get(objCase.Id).Owner.Profile.Name == 'CW Dealer Portal User')

if this was helpful, hit the Like link.
StaciStaci
I'm bad at writting triggers, is this what you mean?  Its not changing the value when I change the owner so I'm guessing I missed something

trigger CW_Update_Substatus_based_on_Case_Owner on Case (before update)
{
    Map<Id,Case> caseMap = new Map<Id,Case>([Select Id,Owner.Profile.Name from Case where Id IN : Trigger.new]);
    for (Case objCase : Trigger.new)
    {
        if (caseMap != null && caseMap.containsKey(objCase.Id) && caseMap.get(objCase.Id).Owner.Profile.Name == 'CW Dealer Portal User')              
        {
            objCase.Substatus__c = 'Open with Dealer';
        }else{
            if (caseMap != null && caseMap.containsKey(objCase.Id) && caseMap.get(objCase.Id).Owner.Profile.Name == 'CW MineStar Support - Tier 2')               
            {
            objCase.Substatus__c = 'Open with Support Advocates';
            }
            else
                {
                objCase.Substatus__c = 'Open with Product Specialists';
                }
    }        }
Abhishek_PareekAbhishek_Pareek
Thanks for pointing out the problem. As the query from case is going to fetch the data from the database and the new values are not commited to database yet, this will fetch profile name for owner's in trigger.old. I 've tweaked the code a bit. See if that works.

trigger CW_Update_Substatus_based_on_Case_Owner on Case (before update)
{
	    Set<Id> ownerIds = new Set<Id>();
	    for(Case objCase : Trigger.new){
	    		ownerIds.add(objCase.OwnerId);
	    }
	    Map<Id,User> userIdToProfileMap = new Map<Id,User>([Select Id,Profile.Name from User where Id IN : ownerIds]); 
	    for (Case objCase : Trigger.new)
	    {
	        if (userIdToProfileMap != null && userIdToProfileMap.containsKey(objCase.OwnerId) 
	        	&& userIdToProfileMap.get(objCase.OwnerId).Profile.Name == 'CW Dealer Portal User')              
	        {
	            objCase.Status = 'Open with Dealer';
	        }else{
	            if (userIdToProfileMap != null && userIdToProfileMap.containsKey(objCase.OwnerId) 
	            	&& userIdToProfileMap.get(objCase.OwnerId).Profile.Name == 'CW MineStar Support - Tier 2')               
	            {
	            objCase.Status = 'Open with Support Advocates';
	            }
	            else
	                {
	                objCase.Status = 'Open with Product Specialists';
	                }
	    }        
     }
}
if this was helpful, hit the Like link.
This was selected as the best answer
Abhishek_PareekAbhishek_Pareek
Sorry for the typo : replace Status with Substatus__c in above piece of code.
StaciStaci
Thank you Abhishek!  That works perfect!