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
SundayAdminSundayAdmin 

VF Page - Prevent Record Deletion Based on Field Value

Hi,

 

I'm struggling to create a VF page that can prevent a delete based on a conditional, in this case prevent a Custom Object Record Delete  of a the Custom Child Object: "Solution__c", only if the formula field "Delete" equals value B and not when it equals value A.

 

Here is the code I have up until now which works to prevent deletions for everyone except Sys Admins. However I would like to know how I could extend/change this code to allow the VF page to prevent deletions based on a field value instead of a global variable like the one for Profile as this is just a temporary measure:

 

<apex:page standardController="Solution__c" action="{!if($Profile.Name != 'System Administrator',
    null,
    urlFor($Action.Solution__c.Delete, $CurrentPage.Parameters.id, [retURL='/006'], true)
    )
    }">
    
     <apex:pageBlock >
        <apex:PageMessage summary="You are not allowed to delete Solutions if the related Opportunity has been set to the status Handover, Closed Lost or Closed Won"
            severity="Warning"
            strength="3"/>
        <apex:pageMessages />
    </apex:pageBlock>
    
</apex:page>

 

All help is appreciatted

 

Thanks and Kind Regards,

SundayAdmin

Puja_mfsiPuja_mfsi

Hi,

You can use the window.onload java script to prevent the deletion with record field condition:

 

<apex:page standardController="Account" >
           <script src="/soap/ajax/20.0/connection.js" type="text/javascript"></script>
            <script>
                 function doThings() {
                       sforce.connection.sessionId = "{!$Api.Session_ID}";
                       var typeVal = '{!Account.Type}';
                       if( typeVal != 'Prospect'){
                                   return;
                        }else{
                                 var records = '{!Account.Id}';
                                 var delResult = sforce.connection.deleteIds([records]);
                                 if(delResult[0].getBoolean("success")) {
                                          alert("account with id " + records + " deleted");
                                 } else {
                                          alert("failed to delete account " + records);
                                 }
                                 window.parent.location.href = '/006';
                          }
                 }
                window.onload = doThings;
           </script>
         <apex:pageBlock >
                <apex:PageMessage summary="You are not allowed to delete Solutions if the related Opportunity has been                                                 set to the status Handover, Closed Lost or Closed Won" severity="Warning" strength="3"/>
                 <apex:pageMessages />
         </apex:pageBlock>

</apex:page>

 

Please let me know if u have any problem on same and if this post helps u please give KUDOS by click on star at left.