Pages

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

No comments:

Post a Comment