Skip to content

Instantly share code, notes, and snippets.

@britishboyindc
Created February 1, 2024 16:06
Show Gist options
  • Save britishboyindc/e471313957e8f66c26794e673cab0fb8 to your computer and use it in GitHub Desktop.
Save britishboyindc/e471313957e8f66c26794e673cab0fb8 to your computer and use it in GitHub Desktop.
<apex:page controller="DemoController" applyHtmlTag="false" showHeader="false" lightningStylesheets="true" sidebar="false">
<html>
<body>
<apex:form >
<apex:slds />
<div class="slds-grid">
<div class="slds-col">
<apex:panelGrid columns="1" width="100%">
<apex:pageBlock mode="Account">
<apex:pageBlockSection title="Section 1" columns="1" collapsible="true">
<apex:selectRadio label="Question 1?"
value="{!objNew1.Test_Parent_Question_1__c}"
required="true"
>
<apex:selectOptions value="{!yesNoOptions}" />
</apex:selectRadio>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:panelGrid>
</div>
<div class="slds-m-top_medium" align="Center">
<apex:commandButton value="SUBMIT" action="{!submit}"/>
</div>
</div>
</apex:form>
</body>
</html>
</apex:page>
//Main Controller
/**
* Created by peter on 1/29/2024.
*/
public with sharing class DemoController {
public Custom_Obj_1__c objNew1 {get;set;}
public Custom_Obj_1__c objExisting;
public Custom_Obj_2__c clist;
public String recordId;
public DemoController() {
recordId = ApexPages.currentPage().getParameters().get('id');
if (String.isNotBlank(recordId)) {
getData();
}
}
private void getData() {
List<Custom_Obj_1__c> records = [Select Id, Test_Parent_Question_1__c FROM Custom_Obj_1__c WHERE Id = :recordId];
if (records.size() > 0) {
objExisting = records[0];
objNew1 = objExisting.clone(false);
}
}
public PageReference submit() {
objNew1.Id = objExisting.Id;
DemoController_WS.updateObject(objNew1);
Custom_Obj_2__c clist = new Custom_Obj_2__c();
clist.Custom_Obj_1__c = objExisting.Id;
System.debug(objNew1.Test_Parent_Question_1__c);
clist.Test_Question_1__c = objNew1.Test_Parent_Question_1__c;
System.debug(clist.Test_Question_1__c);
DemoController_WS.insertObject(clist);
objNew1.Id = NULL;
PageReference pageref = new PageReference('/apex/Thankyoupage');
return pageref;
}
public List<SelectOption> getYesNoOptions() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('Yes', 'Yes'));
options.add(new SelectOption('No', 'No'));
return options;
}
}
public without sharing class DemoController_WS {
public static void updateObject(sObject recordToUpdate) {
update recordToUpdate;
}
public static void insertObject(sObject recordToUpdate) {
insert recordToUpdate;
}
}
@Smartronics99
Copy link

ok,Thank you

@britishboyindc
Copy link
Author

Hi there,
I would assume this line is causing the issue?
update attestationResponseDetailsList;
You'll need to move that to the withoutsharing class since the Standard Controller cannot update any record if it is running under GSU

@Smartronics99
Copy link

Smartronics99 commented Apr 2, 2024

Hi britishboyindc,
I updated my code as below :

 public PageReference submit() {
 system.debug('Test2');
 List<BPAAttestationResponseDetails__c> attestationResponseDetailsList = new List<BPAAttestationResponseDetails__c>();
 system.debug('bpaWrappers' + bpaWrappers);
 if (bpaWrappers != null && !bpaWrappers.isEmpty()) {
 for (BPAWrapper wrapper : bpaWrappers) {
    
  //  wrapper.Id = wrapper.header.Id;
  //  BPA_AttestationController_WS.updateObject(bpaHeader);

    Summary_Detail__c bpaRecord = wrapper.detail;
 // Summary_Header__c bpaSummaryHeader = wrapper.header;
 //  system.debug('bpaSummaryHeader' + bpaSummaryHeader);
    BPAAttestationResponseDetails__c attestationResponseDetail = new BPAAttestationResponseDetails__c();
    attestationResponseDetail.BPA_Detail__c = bpaRecord.Id;
    attestationResponseDetail.Name = bpaRecord.Name;
    attestationResponseDetail.BPA__c = bpaRecord.Summary_Name__c;
    attestationResponseDetail.Comment__c = bpaRecord.Comment__c;
    attestationResponseDetail.Information__c = bpaRecord.Information__c;
    attestationResponseDetail.Confirmation_Accurate__c = bpaRecord.Confirmation_Accurate__c;
    
    attestationResponseDetailsList.add(attestationResponseDetail);
   }

    try {
    BPA_AttestationController_WS.insertObject(attestationResponseDetailsList);
    BPA_AttestationController_WS.updateObject(attestationResponseDetailsList);
  
      //  insert attestationResponseDetailsList;
       // update attestationResponseDetailsList;

Still getting below error - when I submit as GSU. As an admin, able to submit record successfully.
core.apexpages.exceptions.ApexPagesGenericException: system.security.NoAccessException: Update access denied for
BPA_Summary_Detail__c, controller action methods may not execute.

How can we handle this? please advise.

Thank you :-)

@britishboyindc
Copy link
Author

I don't understand why you are performing an insert followed by an update? What is the update doing at that point?
BPA_AttestationController_WS.insertObject(attestationResponseDetailsList);
BPA_AttestationController_WS.updateObject(attestationResponseDetailsList);

But I think that is being seen as the GSU directly trying to update an object which it isn't allowed to do.

@Smartronics99
Copy link

Without insert operation, its throwing below error -
image

@britishboyindc
Copy link
Author

But why do you need the update call? What is that doing if you have just inserted the records?

@Smartronics99
Copy link

yes, if we insert the records, as an admin able to do so.
However as GSU, I'm getting this error message -
core.apexpages.exceptions.ApexPagesGenericException: system.security.NoAccessException: Update access denied for BPA_Summary_Detail__c, controller action methods may not execute

@britishboyindc
Copy link
Author

Is BPA_Summary_Detail__c the Master to BPAAttestationResponseDetails__c?
If so, if the relationship is defined as requiring update access vs just read access, that could be an issue? But otherwise, the without sharing should override all that

@Smartronics99
Copy link

Smartronics99 commented Apr 2, 2024 via email

@Smartronics99
Copy link

Hi @britishboyindc,
I still keep getting this error as GSU.

core.apexpages.exceptions.ApexPagesGenericException: system.security.NoAccessException: Update access denied for BPA_Summary_Detail__c, controller action methods may not execute.

Do I need to check any other permissions? As of now did, setup sharing rule on BPA_Summary_Detail__c owner id not equal to 0

@britishboyindc
Copy link
Author

I don't think sharing rules won't help you here - GSU can never have edit access to the records.
Did you check the sharing setting for the lookup to BPA_Summary_Detail__c from BPAAttestationResponseDetails__c .
Is it Read/Write or Read Only? If read \ write, that might be an issue

@Smartronics99
Copy link

Smartronics99 commented Apr 4, 2024

yes, its public read/write BPA_Summary_Detail__c

@britishboyindc
Copy link
Author

Can you change it to only read access and see if that solves it?

@Smartronics99
Copy link

Smartronics99 commented Apr 4, 2024

Yes,did but still got same error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment