0

I have a trigger for the case object. If a user with a specified role updates the case, they want their name to be populated in a custom text field. The trigger fires and I can see debug logs if I update the case as myself. I see all debug statements except the one intended to be logged only when the criteria are met. If I log in as anybody else it seems the trigger will not fire when the case is updated as there is no 'ui/common/inlineEditSave' log when logged in as somebody else. Or so I am assuming since the trigger does not work as intended. Edit: The problem seems to be that case.recordType.id is coming up as "null" when evaluated even though I know the Case has the proper record type. All of my debug log output is good up until it hits the if statement that evaluates recordType. Here is my code:

trigger CaseTrigger on Case(before insert, before update) {


    private static boolean firstRun = true;
    caseTriggerHandler handler = new caseTriggerHandler();
    system.debug('Trigger fired!');
    try {
        if (firstRun) {
            firstRun = false;
            handler.execute();
        }
    } catch (exception e) {
        system.debug('Error during campaignMemberTrigger: ' + e.getMessage());
        for (Case cs: (List <Case>) Trigger.new) {

            cs.addError('Unable to process transaction, please try again or contact your administrator if you need assistance ');
        }
    }
}

public with sharing class CaseTriggerHandler extends triggerHandler {

    public override void beforeInsert() {

        relatedContactInfo.caseChanges(trigger.new);

    }

    public override void beforeUpdate() {
        identifyPSM.populatePSMSourcer();
        relatedContactInfo.caseUpdate((Map <Id, Case>)trigger.oldMap, (Map <Id, Case>)trigger.newMap, (List <Case>)trigger.new);
    }
}

public class identifyPSM {


    public static void populatePSMSourcer() {
        system.debug('Looking for PSM');
        Set <id> psms = new set <Id> ();
        //get list for all current PSMs
        for (User u: [SELECT id FROM user WHERE user.UserRole.name = 'PSM Sourcer'
                AND user.isActive = true
            ]) {
            system.debug('Adding PSMs to the list');
            psms.add(u.id);
        }

        //get ID of CSM SB recordType
        Id rt = [SELECT id FROM recordType WHERE name = 'CSM SB'].id;
        //loop through all cases.  If they're a CSM SB case and are being edited
        //by a psm sourcer then the psm sourcer's name should be updated to
        //the psm sourcer field on the case
        for (case cs: (List <Case>) trigger.new) {
            if (cs.recordType.id == rt && psms.contains(UserInfo.getUserId())) {
                system.debug('Found PSM');
                cs.PSM_Sourcer__c = userInfo.getName();
            } else {
                system.debug('PSM not found!');
                system.debug('User Name: ' + userInfo.getName());
                system.debug('User Id: ' + userInfo.getUserId());
            }
        }
    }
}
6
  • 2
    I would start with: looking at the Field Level Security permissions for the fields that are being accessed by the logged in user.
    – Arnold Jr.
    Oct 10 '17 at 19:08
  • The changes I make with the logged in user are committing to the database and they have persmission to view and edit the field that's being populated. Is there another angle I should be looking at that from?
    – SFDC Noob
    Oct 10 '17 at 21:11
  • As per your words(trigger can be executed by Sys. Admin profile), the issue should be something related to Salesforce Data Security Model. Please cross check with all the permissions and access levels to the specific user.
    – Arnold Jr.
    Oct 10 '17 at 21:39
  • when logged in as second user, you need to enable debug logging for that user
    – cropredy
    Oct 11 '17 at 0:09
  • I have turned on a trace on that user. The trigger is apparently firing and for whatever reason it's the Case's record Type id causing it not to perform as expected. It comes up null in the debug logs even though I can query the record in the query editor and it shows the proper record type Id. I will edit the post for clarity.
    – SFDC Noob
    Oct 11 '17 at 0:21
1

I was able to resolve this by changing the following code:

if (cs.recordType.id == rt && psms.contains(UserInfo.getUserId())) {
            system.debug('Found PSM');
            cs.PSM_Sourcer__c = userInfo.getName();
        }

To:

if (cs.recordTypeId == rt && psms.contains(UserInfo.getUserId())) {
            system.debug('Found PSM');
            cs.PSM_Sourcer__c = userInfo.getName();
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.