0

I have this Application object that runs through a trigger and trigger handler after DML update. Day-to-day this is fine since most Applications are produced by internal/system admin users but we now have a special scenario where a guest user can create/update an Application object via a public facing portal.

I am running into an issue where the guest user trying to update an application throws an error in the trigger/trigger handler because the user is a guest and guest's cannot update records. If I comment out the line in the trigger that call the trigger handler, the guest can update their record but I know this bandage solution will not last forever.

I am thinking about adding a field to this Application object so whenever a guest creates and Application object (There is only one LWC/Apex Controller that facilitates guests creating/updating records) set this field to True.

In my Trigger that executes after an Application is updated, is there a way to call this extra true/false field I created and check the fields value. If its true (i.e. this record was created and updated by guest user) then do not execute the trigger helper that is failing because the user is a guest.

GuestApplicationsController (Creates application and if its a guest who created it, set Guest Created field to true)

public without sharing class GuestApplicationsController {
    public GuestApplicationsController () {}

    @AuraEnabled
    public static String createGuestApplication(Boolean era){
   
    if(era) {
      Applications__c eraApp = new Applications__c();
      String rTypeId = Schema.SObjectType.Applications__c.getRecordTypeInfosByName().get('ERA').getRecordTypeId();
      eraApp.RecordTypeId = rTypeId;
      eraApp.Guest_Created__c = true;
      insert eraApp;
      String message = 'Success';
      return message;
    }

Update Guest Record Controller (Pass in the record Id for the Application and Map of the fields that need updating)

public without sharing class updateRecordGuestController {
    @AuraEnabled
    public static String updateRecordGuest(String recordId, Map<String, Object> fields) {
        // Initialize setup variables
        String objectName = 'Applications__c';  // modify as needed
        String query = 'SELECT';
        Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap();

        // Grab the fields from the describe method and append them to the queryString one by one.
        for(String s : objectFields.keySet()) {
        query += ' ' + s + ',';
        }


        // Strip off the last comma if it exists.
        if (query.subString(query.Length()-1,query.Length()) == ','){
            query = query.subString(0,query.Length()-1);
        }

        // Add FROM statement
        query += ' FROM ' + objectName;

        // Add on a WHERE/ORDER/LIMIT statement as needed
        query += ' WHERE Id =: recordId LIMIT 1'; // modify as needed
        
        try {

                Applications__c App = database.query(query);

                for(String key : fields.keySet()){
                    App.put(key, fields.get(key));
                }
                
                update App;

        } catch (QueryException e){
                //perform exception handling
                System.debug(e.getMessage());
                throw new AuraHandledException(e.getMessage());
                
        }

        String message = 'Success...';

        return message;
    }
}

Applications Trigger (Executes after Update DML)

trigger ApplicationsTrigger on Applications__c (before insert, before update, after update) {
  
    class applicationException extends Exception {}

    if(ApplicationsTriggerHandler.disabled)
      return;
      
    if (trigger.isInsert)
      ApplicationsTriggerHandler.handleBeforeInsert(trigger.new);

    else if (trigger.isUpdate && trigger.isBefore)
          ApplicationsTriggerHandler.handleBeforeUpdate(trigger.newMap, trigger.oldMap);
      
    // In this else if() I would like to check if the Applications__c.Is_Guest__c != true
    // If the Applications__c.Is_Guest__c is true, then skip the line below, else run the trigger helper line below
    else if (trigger.isUpdate && trigger.isAfter){
     
        // This line below is what's throwing the 500 error since the user updating is a guest
        Applications.handleAfterUpdate(trigger.new, trigger.oldMap);
    }

}

This is the specific I am getting:

status":500,"body":{"fieldErrors":{},"pageErrors":[{"statusCode":"CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY","message":"ApplicationsTrigger: execution of AfterUpdate\n\ncaused by: System.StringException: Invalid id: ERA-2101060124\n\nExternal entry point\nClass.ApplicationTriggerHandler.handleAfterUpdate: line 90, column 1\nTrigger.ApplicationsTrigger: line 22, column 1"}],"index":null,"duplicateResults":[]},"headers":{}}

1 Answer 1

0

Can you include the specific error you're receiving? You didn't include the code from Applications so it is hard to tell specifically what is going on if the error is coming from Applications.handleAfterUpdate (is it really a 500 error? Does it have a GACK?).

If you don't want to run specific code when the context user is a guest, you can refer to the context user's UserType using UserInfo.getUserType() which will tell you the kind of license they have. For example:

else if (trigger.isUpdate && trigger.isAfter) { 
    // This line below is what's throwing the 500 error since the user updating is a guest
    if(UserInfo.getUserType() != 'Guest') {
        Applications.handleAfterUpdate(trigger.new, trigger.oldMap);
    }
}

This approach should work provided you actually always want to avoid the handleAfterUpdate call for Guest users.

3
  • I added the error to the original post @nbrown. May 15, 2021 at 0:39
  • Thanks for adding the error. This looks like a reasonable exception and it's most likely caused by assigning an invalid Id to a lookup field or record Id field on line 90 of ApplicationTriggerHandler.handleAfterUpdate. It doesn't look like you've included that code so I can't help you diagnose it. Do you have a typo in your post and you don't actually have a class named Applications?
    – nbrown
    May 15, 2021 at 11:56
  • adding that if to check the user type worked, thank you May 15, 2021 at 12:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .