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
EMHDevEMHDev 

How to override edit conditionally?

I have a custom Invoice object which gets a related Case created if the invoice goes overdue.  That works fine.  Now, we want to stop the invoice from being edited by anyone except users from a particular profile, once the invoice has a related Case.  If I use the standard button override, I end up with a recursive loop because I'm trying to use the standard edit functionality (when they are allowed to edit), so it keeps calling my scontrol.  Current (bad) code is as follows:
Code:
function EditInvoice() {
  var IsCase ="{!m62_Invoice__c.Case_Number__c}";
  var TopDog = false;
  var _profile = "{!$Profile.Name}";
  if  ((_profile == "Financial Controller") || (_profile == "System Administrator")){
    TopDog = true;
  }
alert("Profile is " + _profile + " topdog is " + TopDog);
  
  var retURL= escape("/{!m62_Invoice__c.Id}");
  if ((IsCase == "") || TopDog) {  // No Case been created OR profile is that of financial controller or Sysadmin
    parent.location.href = retURL+"/e";   
  } else {
    alert ("You cannot edit the invoice once a Case has been added");
    parent.location.href = retURL;
  }
}

Using retURL + "/e" simply causes the function to be called again and again forever if the conditions are met, so it is obviously not the way to do it.

Basically I want to avoid rewriting all the edit functionality but stop certain users from editing the record under certain conditions (i.e. once a case has been created and if the user is not from a named profile).

Does anyone have any ideas to help me here?

Thanks,
Erica

Best Answer chosen by Admin (Salesforce Developers) 
HL-DevHL-Dev
You can use the URLFOR function to control if you want to override a standard button functionality.
 
For example:
 
if (condition A is meet) {
  window.parent.location.href = "{!URLFOR($Action.Contact.Edit, Contact.Id, [retURL=URLFOR($Action.Contact.View, Contact.Id)], true)}";
}
 
When condition A is met, you're telling the browser that it should go to the Contact.Edit page of the selected Contact.Id. The second parameter within [] is the target or the following action (after the user clicks on the "Save" button), which in my example, is to redirect the page to the selected id's detail page. The "true" parameter tells the browser that you don't want to override the standard salesforce functionality, even if you're overriding the edit button.
Hope this helps.


Message Edited by HL-Dev on 01-23-2008 08:29 AM

All Answers

HL-DevHL-Dev
You can use the URLFOR function to control if you want to override a standard button functionality.
 
For example:
 
if (condition A is meet) {
  window.parent.location.href = "{!URLFOR($Action.Contact.Edit, Contact.Id, [retURL=URLFOR($Action.Contact.View, Contact.Id)], true)}";
}
 
When condition A is met, you're telling the browser that it should go to the Contact.Edit page of the selected Contact.Id. The second parameter within [] is the target or the following action (after the user clicks on the "Save" button), which in my example, is to redirect the page to the selected id's detail page. The "true" parameter tells the browser that you don't want to override the standard salesforce functionality, even if you're overriding the edit button.
Hope this helps.


Message Edited by HL-Dev on 01-23-2008 08:29 AM
This was selected as the best answer
BtormarBtormar
At first glance I'd say you have to add the 'nooverride=1' parameter to your new URL. I think that should stop the loop.
Greg HGreg H
Btormar is correct - if you add the nooverride=1 parameter it will stop the looping.
 
Also, I would suggest that you pull the parent.location.href into a variable prior to determining whether the user sees the custom edit page or regular edit page.  I would do this in case there are other parameters already being passed to the page before you intercept it with the override.  If it turns out that they need to see the standard edit page you could simply append the original values and the nooverride parameter and redirect using parent.location.href.  This would ensure the user doesn't get something they didn't expect.
-greg
EMHDevEMHDev
Thanks for the suggestions, guys, but I must be having a slow week.  I've changed the code as follows, but still get the loop when the "if" condition is satisfied:

Code:
var IsCase ="{!m62_Invoice__c.Case_Number__c}";
var TopDog = false;
var _profile = "{!$Profile.Name}";
if  ((_profile == "Financial Controller") || (_profile == "System Administrator")){
  TopDog = true;
}
alert("Profile is " + _profile + " topdog is " + TopDog);

var retURLView = "URLFOR($Action.m62_Invoice__c.View, m62_Invoice__c.Id)";
 
if ((IsCase == "") || TopDog)  {  // No Case been created OR profile is that of financial controller or Sysadmin
//  parent.location.href = retURL+"/e";   
    parent.location.href = {!URLFOR($Action.m62_Invoice__c.Edit, m62_Invoice__c.Id, [retURLView], true)}";
} else {
  alert ("You cannot edit the invoice once a Case has been added");
  parent.location.href = retURLView;
}
}

 Doesn't the parameter "true" at the end of the URLFOR function indicate that 'nooverride=1'?  When I look at the help on URLFOR, it says to set the last parameter to true to set 'no override'.  If not, how do I add the 'nooverride=1' to the statement?

How do you know what inputs the URLFOR function needs?  I'm working in the dark here, as the docs don't explain this in any way that I understand, unless I'm looking in the wrong place (SF Help).

Also, Greg, when you suggest putting the parent.location.href into a variable, isn't that what I'm doing with retURLView?

Obviously I'm missing something!  I really appreciate the help.

Erica



Message Edited by m62Dev on 01-25-2008 03:02 AM
EMHDevEMHDev
OK, the reason it may not have worked was that I'd upgraded my Apex toolkit for Eclipse, but not started a new workspace and I didn't see the error message that said it hadn't saved my scontrol back to SF because there was a syntax error.  Meanwhile, I hunted around at some other posts on the board and saw Greg's suggestions previously, so the working solution is as follows:
Code:
function EditInvoice() {
 var IsCase ="{!m62_Invoice__c.Case_Number__c}";
 var TopDog = false;
 var _profile = "{!$Profile.Name}";
 if  ((_profile == "Financial Controller") || (_profile == "System Administrator")){
    TopDog = true;
 }
 var retURLView = "URLFOR($Action.m62_Invoice__c.View, m62_Invoice__c.Id)";
 var retURLEdit = parent.location.href; //URL accessing sControl
 retURLEdit += "&nooverride=1"; //pass nooverride parameter to prevent infinite reloading of new edit page
 if ((IsCase == "") || TopDog)  {  // No Case been created OR profile is that of financial controller or Sysadmin
    parent.location.href = retURLEdit;
 } else {
    alert ("You cannot edit the invoice once a Case has been added");
    parent.location.href = retURLView;
 }
}

 I'd still appreciate some pointers regarding where I can find info on the inputs to the URLFOR, i.e. how do you know what they need to be?

Thanks, guys.
Erica