Sep 29, 2008

Scenario's on scripting

Scenario II

User needs to save a newly created opportunity along with proper validations in the various fields.

Following are the Events applicable to these scenarios

Event 2.1

BC: Opportunity

Event : BusComp_NewRecord

The NewRecord event is called after a new row has been created in the business component and that row has been made active.

Functional Use: This event is used set up default values for Field ‘Sales Method’ in the Opportunity BC.

Code:

 function BusComp_NewRecord ()

{

            this.ActivateField('Sales Method');     

            this.SetFieldValue('Sales Method', 'XYZ Sales Methodology');

}

Event 2.2

 BC: Opportunity

Event : BusComp_PreWriteRecord

The PreWriteRecord event is called before a row is written out to the database. The event may perform any final validation necessary before the actual save occurs.

 Functional Use: This event is used check the Sales stage in the Opportunity BC. Depending on the Sales stage of the Opportunity, the Account Status is changed. Eg. If the Opportunity Sales Stage is ‘Closed/Won’ the Account status changes to ‘Initiate Request’.

 Code :

function BusComp_PreWriteRecord ()

{

    var SalesStage,AccountStatus,bcAccount,boAccount,AccountId,Name,Stat;

             this.ActivateField("Sales Stage");

            SalesStage=this.GetFieldValue("Sales Stage");

            if (SalesStage == "" || SalesStage == null )

            {

                        this.SetFieldValue("Sales Stage", "01 - Prospecting")

            }

     else if (SalesStage == "10 - Closed / Won")

            {

                        ActivateField("Account Id");

                        AccountId = GetFieldValue("Account Id");

                         with(bcAccount)

                        {

                                    SetViewMode(AllView);

                                    SetSearchSpec("Id", AccountId);

                                    ExecuteQuery(ForwardOnly);

                                    Name= GetFieldValue("Name");

                        ActivateField("Account Status");

                                    Stat=GetFieldValue("Account Status");

                        }

                         if (Stat == "Prospect")

                        {

                                    bcAccount.SetFieldValue ("Account Status","Initiate Request");

                                    bcAccount.WriteRecord();                 

                        }

                 bcAccount=null;

              boAccount=null;                                

            }

           return (ContinueOperation);

}

Event 2.3

BC: Opportunity

 Event: BusComp_WriteRecord

This event is fired after the record is written to the database. This event can be used to perform any associated actions immediately after the record is written to the database.

 Functional Use: This event is used to write date value in the Field ‘Expected Delivery date’ in the Opportunity BC, when a record is saved.

Code:

function BusComp_WriteRecord ()

{

   var SalesStage,clsdate,boOpty,bcRealizable,month,day,year,Fyearret;//declared for realizable revenue

           if (ibProdModify || ibProdModifyMore)

            {

                        var Id = GetFieldValue("Id");

                        var CloseDate = GetFieldValue("Close Date");                   

                        var BOCurrent = TheApplication().ActiveBusObject();

                        var BOOpty, BCOpty, BCOptyProd;

                        if ( BOCurrent == "Opportunity" )

                        {

                                    BOOpty = BOCurrent;

                                    BCOptyProd = BOOpty.GetBusComp('Opportunity Product');

                            }else

                        {

                                    BOOpty = TheApplication().GetBusObject("Opportunity");

                                    BCOpty = BOOpty.GetBusComp('Opportunity');                            

                                    BCOptyProd = BOOpty.GetBusComp('Opportunity Product');

                        }                                               

                        BCOptyProd.ClearToQuery();

                        BCOptyProd.ExecuteQuery();

                        var Ret = BCOptyProd.FirstRecord();

                  if (SalesStage != "14 - Closed / Scrapped" && SalesStage != "10 - Closed / Won" && SalesStage != "12 - Closed / Lost" && SalesStage != "13 - Closed / Shelved")

               {

            while(Ret)

           {

             BCOptyProd.SetFieldValue("Expected Delivery Date",CloseDate);

     BCOptyProd.WriteRecord();                          

              Ret = BCOptyProd.NextRecord();

            }

            }

                        

No comments: