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
Eddy ZhuoEddy Zhuo 

Trigger are fire randomly under a different profile.

Hello all the SalesForce Guru,
        I want to say thank you ahead. We run in to issue with one of our Apex trigger which trigger when one of our coustom object field is modify some contact informations change. I didn't wrote the Apex code. The trigger seem to work fine under system administrator profile, but acting very strange under other more restristed profile. I added all the apex classes and visualforce page permission to this profile, also profile have edit pirviliege to contact information. 
        Did I miss anything? Thank you very much for your help!
sandeep sankhlasandeep sankhla
Can you share your trigger code so I can check if is there anything wrong.
 
Eddy ZhuoEddy Zhuo
Thank you Sandeep,
          The apex code should not affect under different profiles. Since the system administrator profile runs fine. Let me know if you have other questions.
trigger updateRegStatusOnClassCompletion on Registration__c (before insert, before update) {
//NOTE: need to consider both insert where class is already completed (because of walk-in) and 

    Map<ID, Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Registration__c.getRecordTypeInfosById();
    
    for(Registration__c classReg : Trigger.new)
    {
        // when inserted, check if already completed; when updated, check if class completion changed and is yes or exemption
        if (Trigger.isInsert) {  // inserting new records, check if already completed
            //Check if the class has been attended or exempted
            if (classReg.Class_Completed__c != 'Yes' && classReg.Class_Completed__c != 'Exemption') {
                return;
            }
        } else if (Trigger.isUpdate) {  // if the class completion status has not changed, do not continue 
            Registration__c oldClassReg = Trigger.oldMap.get(classReg.Id);
            if (classReg.Class_Completed__c == oldClassReg.Class_Completed__c ) {
                return;
            } else if (classReg.Class_Completed__c == 'No' && oldClassReg.Class_Completed__c != 'Yes' && oldClassReg.Class_Completed__c != 'Exemption') {  //also exit trigger if status changed to no but was blank (effectively not changing)
                return;
            }
        }
        
        // get contact related to registration
        Contact oContact = New Contact();
        
        List<Contact> lContacts = [Select c.Id, c.Email, c.LevelOne_Training_Date__c, c.LevelOne_Training_Completed__c,
                                   c.LevelTwo_Training_Completed__c, c.LevelOne_Status__c,
                                   c.LevelTwo_Status__c, c.LevelThree_Status__c, c.Is_a_member__c, c.Member_Status__c,
                                   c.Has_Registered_for_LevelOne_Training__c , c.Has_Registered_for_LevelTwo_Training__c , c.Has_Registered_for_LevelThree_Training__c 
                                   From Contact c Where c.Id = :classReg.Contact__c];
        
        if (lContacts.size() > 0) {
            //Get the matching contact
            oContact = lContacts.get(0);
            
            //Determine class registration tier
            String recordType = rtMap.get(classReg.RecordTypeId).getName();
            
            // continue and make sure class has been completed
            if (classReg.Class_Completed__c == 'Yes' || classReg.Class_Completed__c == 'Exemption') {
                    
                String classStatus = (classReg.Class_Completed__c == 'Exemption') ? 'Exempt' : 'Attended';
                
                if (recordType == 'Level One Registration') {
                    // Set Date, Boolean and Status for Level One Class Attendance and Approve Level Two
                    oContact.LevelOne_Training_Completed__c = true;
                    ocontact.LevelOne_Status__c = classStatus;
                    if (oContact.LevelTwo_Status__c <> 'Attended') {
                        oContact.LevelTwo_Status__c = 'Approved';
                    }    
                } else if (recordType == 'Level Two Registration') {
                    // Set Boolean and Status for Level Two Class Attendance
                    oContact.LevelTwo_Training_Completed__c = true;
                    ocontact.LevelTwo_Status__c = classStatus;
                    
                    // Approve Level Three ONLY if contact is a member 
                    if ((ocontact.Member_Status__c == 'Yes' || oContact.Member_Status__c == 'Exempt' ) && oContact.LevelThree_Status__c <> 'Attended') {
                        oContact.LevelThree_Status__c = 'Approved';
                    }
                    
                } else if (recordType == 'Level Three Registration') {
                    // Set Status for Level Three Class Attendance
                    ocontact.LevelThree_Status__c = classStatus;
                }
                
            // ROLLBACK attendance and approval statuses if class status changes and is now 'No' 
            } else if (classReg.Class_Completed__c == 'No') {
                
                if (recordType == 'Level One Registration') {
                    // Roll Back to Registered (Level One) or Approved instead of Attended if class completed changes to No 
                    oContact.LevelOne_Training_Completed__c = false;
                    ocontact.LevelOne_Status__c = 'Registered';
                    
                    if (oContact.LevelTwo_Status__c <> 'Attended') {
                        //Check if they have registered for Level Two and if not set blank instead of 'Registered'
                        if (oContact.Has_Registered_for_LevelTwo_Training__c == true) {
                            oContact.LevelTwo_Status__c = 'Registered';
                        } else {
                            oContact.LevelTwo_Status__c = '';
                        }
                    }    
                } else if (recordType == 'Level Two Registration') {
                    // Set Boolean and Status for Level Two Class Attendance
                    oContact.LevelTwo_Training_Completed__c = false;
                    
                    // Check that Level Two should be Approved, otherwise roll back to Registered
                    if (oContact.LevelOne_Training_Completed__c == true) {
                        ocontact.LevelTwo_Status__c = 'Approved';
                    } else {
                        ocontact.LevelTwo_Status__c = 'Registered';
                    }
                    
                    
                    if (oContact.LevelThree_Status__c <> 'Attended') {
                        //Check if they have registered for Level Three and if not set blank instead of 'Registered'
                        if (oContact.Has_Registered_for_LevelThree_Training__c == true) {
                            oContact.LevelThree_Status__c = 'Registered';
                        } else {
                            oContact.LevelThree_Status__c = '';
                        }
                    }   
                    
                } else if (recordType == 'Level Three Registration') {
                    // Check that Level Three should be Approved, otherwise roll back to Registered
                    if (oContact.LevelTwo_Training_Completed__c == true) {
                        ocontact.LevelThree_Status__c = 'Approved';
                    } else {
                        ocontact.LevelThree_Status__c = 'Registered';
                    }
                }

            }
            
            //Update the Contact
            try {
                update oContact;
            } catch(DmlException e) {
                System.debug('Contact could not be updated.  The following exception occurred: ' + e.getMessage());
            }
            
            
        }
        
    }
    

}

 
Sai KethuSai Kethu
Hi Eddy,

Im sorry i didn't understand your question, can you please elaborate What is this trigger doing on the custom object?? And yes trigger code should do.

Regards
Sai