Pages

Monday, June 5, 2017

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...

Sunday, May 7, 2017

Adding/Copying Accounts Team to Opportunity Sales team

I have used eScript for this function. There can be several other methods like dynamic candidate assignment to achieve the same functionality. But here I'm giving an  example code for copying the positions of Accounts Team of an Account to the Sales Team of an Opportunity, while associating the account to an Opportunity.

This is written under BusComp server scripts in Opportunity BusComp.

Below is the code and comments will explain the functionality within the script.


function BusComp_SetFieldValue (FieldName)
{
//Added to copy Accounts team to Opportunity Sales Team while associating an account to opportunity

if (FieldName == "Account")
{
var OptyAccountId = GetFieldValue("Account Id");

//First check whether the Account is input, otherwise
//Prevent code execution while removing the account from the opportunity
if (OptyAccountId)
{
var boBusObj = TheApplication().GetBusObject("Account");
var bcAcct = boBusObj.GetBusComp("Account");
var sSalesTeam ='';

with(bcAcct)
{
ClearToQuery();
SetViewMode(AllView);
ActivateField("Sales Rep");
SetSearchSpec("Id", OptyAccountId);
ExecuteQuery(ForwardOnly)
if(FirstRecord())
{
//the account was found. Now cycle through the positions

var bcMVG = bcAcct.GetMVGBusComp("Sales Rep");
bcMVG.ActivateField ("Position Id");
var isRec = bcMVG.FirstRecord();

while(isRec)
{

var PosId = bcMVG.GetFieldValue("Position Id");

var optyMVG = this.GetMVGBusComp("Sales Rep");

//search in optyMVG whether the position which is there in account already exist in oppotunity.

optyMVG.ClearToQuery();
optyMVG.SetViewMode(AllView);
optyMVG.ActivateField("Position Id");
optyMVG.SetSearchSpec("Position Id",PosId);
optyMVG.ExecuteQuery(ForwardOnly)
var isRecopty = optyMVG.FirstRecord();
//If the position record is not there in oppotunity;
if (isRecopty == false)
{
//Add position to oppotunity
var bcAssoc = optyMVG.GetAssocBusComp();
bcAssoc.ClearToQuery();
bcAssoc.SetViewMode(AllView);
bcAssoc.ActivateField("Position Id");
bcAssoc.SetSearchSpec("Position Id",PosId);
bcAssoc.ExecuteQuery(ForwardOnly)
var isRecpos = bcAssoc.FirstRecord();
if (isRecpos)
{
bcAssoc.Associate(NewAfter);
}
}
isRec = bcMVG.NextRecord();
}
}
}
}
}
}

Hope this helps...