Monday, June 13, 2011

PreWriteRecord to fire only for New Records being saved [Method : isNewRecordPending]

I got this information from my friend Pranoj . Thanks Pranoj ..

Scenario: User wants a few validation written on the PreWriteRecord to fire only for New Records being saved
Solution :
A Vanilla Method “isNewRecordPending” can be used to achieve the required functionality

This method can be invoked by using a script in the PreWriteRecord event to determine if the current record is newly created. If the record is a new record, this method returns the value TRUE

Sample Code :

function BusComp_PreWriteRecord ()

{

var isNewRecord = this.InvokeMethod("IsNewRecordPending");

if(isNewRecord == "TRUE")

{

************Code Goes Here***********
}

return (ContinueOperation);

}

Syntax :
BC.InvokeMethod(“isNewRecordPending”)

Advantage : This method reduces the overhead of Setting and Resetting the Global Variable captured at the NewRecord Event

Note : This method can be used across Events and is not specific only to PreWriteRecord

4 comments:

  1. Good insight.

    Just to keep it simple, I am wondering, can we just write a new variable in NewRecord() and catch it in PreWriteRecord() to achieve the required functionality ?

    I am not taking away the credit in any way, and I guess this is a more elegant solution. But, just to keep it simple, that's all.

    Regards,
    Nitin Jain

    ReplyDelete
  2. Thanks Nithin .. but

    When a user creates a new record and subsequently selects Undo Record, the new record flag remains set. At this point, if the user modifies another record, the code intended only for new records will be triggered, potentially causing a serious data integrity issue. Similar consequences are true for Update Record and other flags that are not properly re-set.

    If code is still required, reset the flag in the BusComp_PreDeleteRecord event, which will fire in case of an Undo Record command after a New or Copy Record action by the user. This may also require adding the code to the BusComp_CopyRecord event if the business requirement allows the user to create new record by way of copying the existing records.

    I guess using above code i can aviod all these over head. Hope i answered the question

    using the “WriteRecordNew” Run time event will help to avoid the script and using the new record flags as it only fires when a new record is written. To be able to use the run time events, the code must also be converted to a workflow or business service that can be triggered by a runtime event.

    Event When it is trigged
    WriteRecord:When a new record is created or an existing record is updated

    WriteRecordNew:When a new record is created

    WriteRecordUpdated:When an existing record is updated

    If i am wrong pls correct me in this ..

    thanks and regards
    joseph thomas

    ReplyDelete
  3. awesome. thanks a lot Joju!!!!!

    ReplyDelete
  4. From hdedeus:
    Thank you Thomas for the info above, it was very helpful.
    But based on my tests using Siebel 8.0.0.13 [20448] ENU, your statement "When a user creates a new record and subsequently selects Undo Record, the new record flag remains set." is not correct. After Undo or ESC the method "isNewRecordPending" is reset back to FALSE. Therefore I was able to solve my problem (update of field Type only allowed during insert of new records on a VBC) using this unique piece of code:
    function BusComp_PreSetFieldValue (FieldName, FieldValue)
    {
    try
    {
    switch(FieldName)
    {
    case "Type":
    if(this.InvokeMethod("IsNewRecordPending")!="TRUE")
    {
    TheApplication().RaiseErrorText("Update on Type is not allowed!!!");
    }
    break;
    }
    }
    catch(e) {throw(e);}
    finally {}
    }

    ReplyDelete