• Carrie Schoenvogel
  • NEWBIE
  • 0 Points
  • Member since 2011
  • 6 X SFDC Certified CRM Professional

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 1
    Replies

Can someone help me with a test class for the following.  Would like to put together a "public WITHOUT SHARING" test class if possible.  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) {

        if(a.Account_Manager__r.Id != NULL)

                 c.OwnerId = a.Account_Manager__r.Id;

        else

                 c.OwnerId = UserInfo.getUserId();

            break;

          }

      }

     

   }  

  

}

Hello,

 

I am hoping someone can help me with 2 issues I am having with the trigger below.  Any help or guidance would be greatly appreciated. 

 

1)       Help with code coverage – I’m a System Admin.  I know I need to have code coverage and mine is showing 0% in the sandbox.  I am not sure what to do and am hoping for some guidance on this.   

2)       A way around sharing rules due to a “Private: Grant Access Using Hierarchies” model (details below).

 

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 field 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). 

 

The trigger assigns a case according to the following logic:

  • IF THE ACCOUNT MANAGER CUSTOM FIELD ON ACCOUNT OBJECT IS NOT NULL > THEN ASSIGN CASE TO ACCOUNT MANAGER (this might be a USER or a QUEUE)
  • IF THE ACCOUNT MANAGER CUSTOM FIELD ON ACCOUNT OBJECT IS NULL > THEN ASSIGN CASE TO THE CASE CREATOR

 

Sharing rules issue – Cases are on a “Private: Grant Access Using Hierarchies” model.  The issue falls specifically around our sales users.  If a sales user tries to create a case, they receive the Insufficient Privileges system error. 

After testing, I found:

  • The sales user CANNOT create a case if they are NOT the account manager (client services custom field) but they ARE the account owner AND contact owner.  The reason it is NOT allowing them to create the case in this situation is because the trigger is assigning the case to the Account Manager (that is not the current user/sales rep) right before the final SAVE happens and then telling them that they don’t have sufficient privileges due to the sharing rules.  For accounts with account managers, the trigger logic is to assign the case to the account manager (client services) – the account manager field will NEVER be the sales rep.
  • The sales user CAN create a case if I assign them as the account manager (client services custom field) – However, in our everyday process, this would never happen because they are the sales rep (account owner)… not the Account Manager (client services).  The reason it is allowing them to create the case in this situation is because the trigger is assigning the case to the sales rep as the owner and giving them the rights to the case.
  • In addition, the sales user CAN also create a case if the Account Manager field is blank.  The reason it is allowing them to create the case in this situation is because the trigger is assigning the case to the sales rep as the owner and giving them the rights to the case (because the logic is to assign BLANK account managers to the case creator).

 

Since our model is private, we can usually get around this on cases by setting up sharing rules.  Most of our other case models assign newly created cases to a QUEUE.  Unfortunately, in this case model, it is assigning the case to multiple possibilities: either the account manager (might be user or queue) or the case creator.  I am not able to setup a sharing rule to cover all of the possibilities needed without completely opening up the sharing model and this is not the desired solution. Is there some kind of tweak I can make to the trigger that will alleviate this issue?   Also WITHOUT using CASE TEAMS?

 

 

 

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) {

        if(a.Account_Manager__r.Id != NULL)

                 c.OwnerId = a.Account_Manager__r.Id;

        else

                 c.OwnerId = UserInfo.getUserId();

            break;

          }

      }

     

   }  

  

}

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;

          }

      }

     

   }  

  

}

We have a custom object that is related to the standard object through a lookup relationship.  I need to perform a field update to the "Stage" field on the standard Opportunity object depending on 3 field values on a custom object called "Provisioning".  I am unable to use a simple workflow to do this because it is not a master-detail relationship between the two objects.  Can someone please help me put together a trigger for this scenario?

 

On the Provisioning_c custom object:

If the following criteria = TRUE -

RecordType = X  || Y && Type_c = X && Status_c = X

 

Then:

Update the 'Stage' field on the standard Opportunity object to 'X' value.

 

Thank you!

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;

          }

      }

     

   }  

  

}