Requirement:
We have Opportunities as Parent and Activities (Action BC) as child. Allow users to delete records from an Activities List Applet, only if more than 1 record is present.
Analysis:
Requirement seems pretty simple when we read the requirement statement. One thing is very clear that we have to use scripting. When we have to do scripting there are two important questions that we should ask before we get down to write script
Which event to write the script on?
Why to write script on that event?
I always believe that scripting is easy but to script on right event and in right way is difficult.
Solution:
I came across a script shared by somebody for the solution of the above problem. I will discuss that script before telling you my own version of that script.
function BusComp_PreDeleteRecord ()
{
var currentView = TheApplication().ActiveViewName();
if(currentView =="Opportunity Detail - Activities View")
{
var optyid = TheApplication().ActiveBusObject().GetBusComp("Opportunity").GetFieldValue("Id");
var oBO = TheApplication().GetBusObject("Opportunity");
var oBC = oBO.GetBusComp("Action");
with(oBC)
{
SetViewMode(AllView);
ActivateField("Opportunity Id");
ClearToQuery();
SetSearchSpec("Opportunity Id", optyid);
ExecuteQuery(ForwardBackward);
if(FirstRecord())
{
var isRecord = NextRecord();
if(isRecord < 1)
{
TheApplication().RaiseErrorText("Msg");
return (CancelOperation);
}
}
}
}
oBO = null;
oBC = null;
return (ContinueOperation);
}
At first look the script seems to be fine and will serve the purpose but if you closely analyze the script you will find out there is lot of scope for improvement in the above script. Let’s discuss what is wrong
- function BusComp_PreDeleteRecord ()
I think it is a wrong choice of event because if user creates a new record and clicks on UNDO then also PreDeleteRecord and DeleteRecord events are called and in that case also this code is going to execute, which actually is not required.
Also the statement
var currentView = TheApplication().ActiveViewName();
if(currentView =="Opportunity Detail - Activities View")
is only required because we are writing the script in BC and not on applet. So it will be better to write the script at applet rather than BC
- ExecuteQuery(ForwardBackward);
Lot of times we don’t pay attention to cursor mode we are defining while doing ExecuteQuery. ForwardBackward (which is default cursor mode) is only required if we have to do a Previous Record in our code and in that case it is not required so the cursor mode here should be ForwardOnly.
- if(FirstRecord()) { var isRecord = NextRecord();
This code is not required there is function called CountRecords that can give number of records present so the condition should be if(oBC.CountRecords == 1) then error
- return (CancelOperation);
There is no need for return(CancelOperation) as RaiseErrorText means that stop execution and next statements are never executed and here that would mean that oBO and oBC are never made null when error is displayed. Always make variables null in finally block.
My Solution:
In Applet PreInvokeMethod write the following script
if(MethodName == "DeleteRecord")
{
if(this.BusComp().CountRecords() == 1)
{
TheApplication().RaiseErrorText("Error!!!");
}
}
I hope this post puts helps you to write better scripts in future.


(8 votes, average: 4.63 out of 5)