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
Carrie SchoenvogelCarrie Schoenvogel 

Help with Trigger to assign Null Values

We have a custom field on accounts called "Account Manager" ( Account_Manager__r.Id ).  This is the Client Services Account Manager who handles an account in addition to the Account Owner (account owner is usually the Sales Rep).

The custom fields called "Account Manager" is a Lookup to User object field and could be EITHER a USER, a QUEUE or it could also be BLANK.

 

I am creating a client services cases model (which is the record type '012R00000004tyC' you see below). 

 

I need to create a trigger that does the following:

IF THE ACCOUNT MANAGER CUSTOM FIELD ON ACCOUNT OBJECT IS NOT NULL > THEN ASSIGN CASE TO ACCOUNT MANAGER

IF THE ACCOUNT MANAGER CUSTOM FIELD ON ACCOUNT OBJECT IS NULL > THEN ASSIGN CASE TO THE CASE CREATOR

 

The trigger I have created below works ONLY if the Account Manager custom field IS NOT NULL.

It does NOT assign it to case creator if Account Manager custom field is blank... and it does not know what to do if Account Manager custom field field is blank.

(It throws the following error message if the Account Manager custom field is blank)

 

"Error: Invalid Data.

Review all error messages below to correct your data.

Apex trigger caseToAM caused an unexpected exception, contact your administrator: caseToAM: data changed by trigger for field Owner ID: owner cannot be blank"

 

Can anyone help me revamp this trigger?  Thank you!!

 

 

trigger caseToAM on Case (before insert) {

   Set<ID> accountIDs = new Set<ID>();

  

   for (Case c : trigger.new) {

      //build a set of unique account ids associated to the case(s) being created

      if (c.AccountId != null && c.RecordTypeId == '012R00000004tyC') {

         accountIDs.add(c.AccountId);

      }

     

   }

   //get the account manager id for all accounts affected

   List<Account> lAccounts = [SELECT Id, Name, Account_Manager__r.Id FROM Account WHERE Id in :accountIDs];

  

   //loop through the cases again and assign the account manager to the case

   for (Case c : trigger.new) {

      //change the owner to the account manager

          for (Account a : lAccounts) {

            if (c.AccountId == a.Id) {

               c.OwnerId = a.Account_Manager__r.Id;

               break;

          }

      }

     

   }  

  

}

Best Answer chosen by Admin (Salesforce Developers) 
k_bentsenk_bentsen

Modify your if statement in your loop to:

 

if (c.AccountId == a.Id) {
   if(a.Account_Manager__r.Id != NULL)
       c.OwnerId = a.Account_Manager__r.Id;
   else
       c.OwnerId = UserInfo.getUserId();
   break;
}

 

All Answers

k_bentsenk_bentsen

Modify your if statement in your loop to:

 

if (c.AccountId == a.Id) {
   if(a.Account_Manager__r.Id != NULL)
       c.OwnerId = a.Account_Manager__r.Id;
   else
       c.OwnerId = UserInfo.getUserId();
   break;
}

 

This was selected as the best answer
Carrie SchoenvogelCarrie Schoenvogel

This worked perfectly, thank you so much!! :)