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
BryanTSCBryanTSC 

Trigger Causing Visualforce Authorization Page

Hi Folks,


I have a custom child object of Contact called Inquiry_History__c.  I developed a trigger so that whenever an inquiry history record is created that meets certain criteria, a task is created and assigned to the user specified in the inquiry history custom "Owner" field (a lookup to USER, automatically assigned via workflow upon creation).

 

Everything works as anticipated when adding records the good 'ole fashioned way.  However, I have a form hosted on VF sites that creates a Contact and associated inquiry history record that is now flashing the "Authorization Required' page on submit.

 

Where should I begin to look for the cause of this?  I'm not sure how I can debug this process to figure out why the webform is being halted.

 

Trigger:

 

trigger callInquiry on Inquiry_History__c (after insert) {

List<Task> taskList = new List<Task>();

    for (Integer i=0; i<trigger.new.size(); i++) {
    
        if(
        (Trigger.new[i].XStudent_Type__c == 'FF') && 
        ((Trigger.new[i].XCollege__c == 'RSC') || (Trigger.new[i].XCollege__c == 'SCA'))) {
            taskList.add(
                new Task(
                    whatid=Trigger.new[i].Id,
                    whoid=Trigger.new[i].XContact__c,
                    OwnerId=Trigger.new[i].XOwner__c,
                    Subject='Call New Inquiry',
                    ActivityDate = Date.today()+7
            ));
        }
    
    }
    
    insert taskList;

 

Thanks for any insight you can provide!

kiranmutturukiranmutturu

i think u need to check the permissions of the objects and fields in this process 

BryanTSCBryanTSC

Permissions are set for all fields on the inquiry history object.  I do not see anwhere to configure site permissions for tasks though . . .

Andy BoettcherAndy Boettcher

If you run that VF page on your org natively (http://org/apex/yourpagename) do you get any kind of errors?

 

Also - check to make sure that the "Public Access" button in your site has access to the VF classes behind the Page (if there is one)

BryanTSCBryanTSC

Running the form natively still produced an error, but at least gave me a message.  I at least know what's stopping it now, but don't know how to circumvent it.

 

Error:  System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, callInquiry: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Assigned To ID: owner cannot be blank: [OwnerId] Trigger.callInquiry: line 22, column 1: []

 

I understand that the owner field is initially blank when the system is attempting insertion.  However, this field is updated via a workflow field update and the trigger should then use that value to assign the task.  How do I configure this to handle this circumstance?

Andy BoettcherAndy Boettcher

Ahhhh....APEX "order of execution" catches us once again!  =)

 

Workflows only fire after all triggers and system validations occur - so you are going to get tripped up unless you get some OwnerId in that record.  Don't care much who it is - your workflow after the fact will correct it, right?

 

You could even do something simple like this in a BEFORE trigger:  (an AFTER trigger will throw another error trying to do this...)

 

User u = [SELECT Id FROM User WHERE IsActive = true LIMIT 1];

yourrecord.ownerId = u;

 -Andy

 

 

BryanTSCBryanTSC

Thanks, I'll play around with some of these ideas and see what I can come up with.

 

Why though does it work without problem if I create a record using standard functionality?

Andy BoettcherAndy Boettcher

The Standard UI fills in data in all of the system-required fields for you automatically.  Going Visualforce / APEX, you don't get that "luxury".  =)

 

-Andy