Pages

Monday, June 5, 2017

Creating a sequence of activities

Customer requirement:

There are some approval activities in opportunity as Gate 0 Approval, Gate 1 Approval, Gate 2 Approval, Gate 3 Approval, Users will be allowed to create each approval only after the previous approval activity status is "Approved" except Gate 0 Approval.

Solution:

This I have achieved basically through scripting. First we have to create four different activity types for the above requirement. I have created below four activity types under Administration- Data -- List of Values (Lov Type - TODO_TYPE)
  1. ESGB Gate 0
  2. ESGB Gate 1
  3. ESGB Gate 2
  4. ESGB Gate 3
Then we can write validation under BusComp_PreSetFieldValue of 'Action' BC to validate the previous activity status, when the user select the activity type. 
Here as we have to validate three activities, I have written a general function and then associated the function at BusComp_PreSetFieldValue .
Please be reminded that, the Action BC should be a child of Opportunity BC while we do this validation.

Function ESGBApprovalSeq(fVal)
{
var optyId ="";
//Verify whether the Parent is Opportunity
if (this.ParentBusComp() != null)
{
if (this.ParentBusComp().Name() == "Opportunity")
{
//Get the Opportunity ID related to the activity.
var Optybc = this.ParentBusComp();
optyId = Optybc.GetFieldValue("Id");
}
}
//Search in Action BC for activities which is having the opportunity ID, 
//Activity type defined by function variable and activity status as approved

var boBusObj = TheApplication().GetBusObject("Opportunity");
var bcAct = boBusObj.GetBusComp("Action");
with (bcAct)
{
ClearToQuery();
SetViewMode(AllView);  
ActivateField("Status");
SetSearchSpec("Opportunity Id", optyId);
SetSearchSpec("Type", fVal);
SetSearchSpec("Status", "Approved");
ExecuteQuery(ForwardOnly)
//If No such record throw error;
if(FirstRecord()== false)
{
this.UndoRecord()
TheApplication().RaiseErrorText("Previous Gate Approval not yet completed!");
}
}
}


In BusComp_PreSetFieldValue , write below;

function BusComp_PreSetFieldValue (FieldName, FieldValue)
{

if(FieldName == "Type")
{
if (FieldValue == "ESGB Gate 1")
ESGBApprovalSeq("ESGB Gate 0")
else if (FieldValue == "ESGB Gate 2")
ESGBApprovalSeq("ESGB Gate 1")
else if (FieldValue == "ESGB Gate 3")
ESGBApprovalSeq("ESGB Gate 2")
}


}

This will validate whether the defined activity status is "Approved"

Hope this helps...

Sequence Number Field in parent BC

Customer requirement as below;

On updating sales stage of Opportunity to a particular stage (Eg: “Governance Approval”), There will be a custom field as ESGB reference number, which will be generated sequentially in a defined format (Eg: ESGB000300001)


Solution:

I have used the vanilla BC sequence generation to accomplish the requirement together with some scripting to generate the format

It is also possible to create a DB sequence and associate the sequence to the field.(Which is not discussed here)

So in our way, first we create the sequence field as below;

  • Add a Field in the Opportunity BC
    • Name: esgb_seq
    • Type : DTYPE_NUMBER
    • Column : X_ESGB_SEQ (Pls add a new column or associate existing column which is not used)
  • Add a Business Component User property to Opportunity  BC
    • Name: Sequence Field
    • Value : esgb_seq
  • Create a new Business Component
    • Name: Opportunity.esgb_seq (Sequence)
    • Class : CSSSequence
    • Sort Spec: Sequence (DESCENDING)
    • Table: S_OPTY
  • Add a field in BC created above step
    • Name : Sequence
    • Column : X_ESGB_SEQ
  • Add the Business Object Component under Business Object Opportunity
    • Bus Comp : Opportunity.esgb_seq (Sequence)
Now a sequence number should be generated starting from 1 for each new opportunity created.
You can expose the esgb_seq field in UI for verification. But later you should remove it from UI

Then define a method to populate this sequence(Formatted according to requirement) to a different field when a button is clicked.


function BusComp_PreInvokeMethod (MethodName)
{
if (MethodName=="ESGBRef")
{
var rowSeq=this.GetFieldValue("esgb_seq");
var strEsgbSeq="" ;
var strLength = 9 - rowSeq.length;
if (strLength == 1)
strEsgbSeq = "ESGB" +  "0" + rowSeq;
else if (strLength == 2)
strEsgbSeq = "ESGB" +  "00" + rowSeq;
else if (strLength == 3)
strEsgbSeq = "ESGB" +  "000" + rowSeq;
else
strEsgbSeq = "ESGB" + rowSeq;
this.SetFieldValue("ESGB_Number", strEsgbSeq);
this.WriteRecord();
}
}

Here I have used the starting value of the sequence as 300001 to fulfill the formatting

Hope this helps...