Many times, we will have a thought of Locking a record in salesforce to restrict editing/updating it by other Users.
There is no explicit option provided by saleforce to support this action, however we can do it by making use of the available functionalities which is the Approval Process.
This post makes use of the Approval Process and AJAX for locking the record.
Let us assume a requirement that an Opportunity Owner want to lock his/her Opportunity, to avoid further processing on it by the Sales Team.
Since Opportunity is locked, it cannot be edited by its Opportunity Sales Team members even with Read/Write access on it.
To implement this,
1. Custom button "Lock Record" was created on Opportunity and on clicking it invokes an approval process as below, which Locks the record.
2. Approval Process is created with Entry criteria condition as Current User = Opportunity Owner.
Process contains a one level step of approval with Assigned Approver same as the Opportunity Owner. Such that, the approval process is within the Opportunity Owner and can approve or reject or recall the record to Unlock the record. Final Approval or Final Rejection or Recall Action should Unlock the record.
Following actions are performed when clicking the "Lock Record" button,
1. Checks if the User who is trying to Lock the record is the Opportunity Owner or not. If yes, proceed to next step.
2. Checks if the record is already in any other approval process in Pending status or not. If not, proceed to next step.
3. Submit the record to the Approval Process.
This post is unique in terms of executing this requirement, fully based on AJAX just by a button click.
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
sforce.connection.query("SELECT Id,Name,OwnerId,StageName FROM Opportunity where Id= '{!Opportunity.Id}'",{onSuccess: handleSuccess,onFailure: handleFailure});
function handleSuccess(queryresult)
{
pop1('Processing......');
try
{
records = queryresult.getArray("records");
pro = sforce.connection.query("SELECT Id,Status,TargetObjectId FROM ProcessInstance where TargetObjectId= '{!Opportunity.Id}'").getArray("records");
if(records[0].OwnerId.substring(0,15)=='{!$User.Id}')
{
var proins = sforce.connection.query("SELECT Id,TargetObjectId,Status FROM ProcessInstance WHERE TargetObjectId='{!Opportunity.Id}'");
var result= proins.getArray("records");
var flag='true';
if(proins.size>0)
{
for(var i=0;i<result.length;i++)
{
if(result[i].Status=='Pending')
flag='false';
}
}
if(flag=='true')
{
var request = new sforce.ProcessSubmitRequest();
request.objectId = records[0].Id;
request.comments = "Record Locked by the Opportunity Owner";
var processRes = sforce.connection.process([request]);
CBox('Record Locked. Either Approve or Reject to Unlock the Record!!');
}
else if(flag=='false')
{
pop2('Record is in Process already. Cannot Lock it');
}
}
else
{
pop2('Only Opportunity Owner can Lock the record');
}
}
catch(error)
{
alert(error);
}
}
function handleFailure(error)
{
alert(error);
}
function pop1(status)
{
var box = new parent.SimpleDialog("sfdc"+Math.random(), true);
parent.box = box;
box.setTitle("<img src='/img/icon/opportunities24.png' style='margin:0 5px;'/>"+"Record Lock");
box.createDialog();
box.setWidth(500);
box.setContentInnerHTML("<img src='/img/loading32.gif'/> "+status);
box.setupDefaultButtons();
box.show();
}
function pop2(status)
{
box.setContentInnerHTML(status+"<br/><br/>"+"<input type=\"button\" value=\"Close\" onclick=\"box.hide();window.location.reload();\" />");
}
Approval History will be shown below the Opportunity with comments as "Record Locked by the Opportunity Owner".
Opportunity Owner need to Approve or Reject or Recall the record in order to release it for the Sales Team Members.
Note : Important constraint to note here, we can still see the "Lock Record" option still available even if the record is locked, which cannot be removed from the page layout. Only option i can see to remove it after the locking the record is by using sControl which is not in use now.
But when we click the "Lock Record" button now, will alert the user that the "Record is in Process already. Cannot Lock it".
Although we have various ways of locking the record via Validation rule or trigger which needs a custom field creation to restrict record updation, when that field condition is met. This post can be considered as an alternative way which involves Locking of record with no additional field creation, with a visual Locking appearance.
No comments:
Post a Comment