This topic contains 6 replies, has 0 voices, and was last updated by JohnCCole 6 years, 6 months ago.

  • Author
    Posts
  • #17930

    j.j

    In SS1 I remember declaring global variable outside of entry points (e.g pageinit, fieldchanged, saverecord) – with these variables and functions I used to process common business logic and then use the values across all entry points. E.G. Calculate and declare summarized value and keep it stored in a variable, then can use this variable in pageinit or saverecord or fieldchanged – this was useful to have common variables and calculations done and populated.

    With SS2 however am unable to do this as we do not have objects like scriptContext etc. available outside of entrypoints, thus unable to create global variables.

    This seems a very basic need and was readily available in SS1. Am I missing something ?

    E.g. var own = scriptContext.newRecord.getValue({fieldId : “owner”});

    var emp = scriptContext.newRecord.getValue({fieldId : “custrecord_hc_trn_part_emp”});

    var same = false;

    if (own == emp){same = true;}

    This code can run in beforesubmit or aftersubmit as the scriptContext object is avialble to these entry points. However If I call these outside of any entry points the scriptContext object does not exist.

    Basically I would like to get these values and use it across all entry points. In my example I need to identify if the employee and the record creator are same and use this in all functions so that I dont have comare everytime.

    Another example is dosomething then in beforesubmit dosomethingelse and then use the same in aftersubmit. is there a way we can pass values across entrypoints and also without any entrypoints.

    Does anyone have any workaround ? Any suggestions to accomplish this ?
    This is a cached copy. Click here to see the original post.

  • #17931

    j.j

    This is a sample client script in SS1.

    maybe one example will help here :

    // Wait for all sourcing to complete from item field, get the rate field. If rate < 10, set it to 20.

    // Execute this post sourcing function

    //declaring the global variable

    var rate = 0;

    function psitemfieldchange(type, name) //setting rate post sourcing

    {

    if(type =='item' && name =='item')

    {

    // Once all the fields from item are sourced

    var line = nlapiGetCurrentLineItemIndex(type);

    if (rate < 10)

    {nlapiSetCurrentLineItemValue('item', 'rate', 20);}

    else {nlapiSetCurrentLineItemValue('item', 'rate', rate);}

    }

    }

    function fcitemfieldchange(type, name) //getting rate for the item on field change

    {

    if(type =='item' && name =='item')

    {

    // Once all the fields from item are sourced

    rate = nlapiGetCurrentLineItemValue('item', 'rate');

    var line = nlapiGetCurrentLineItemIndex(type);

    //alert('field changed ' + rate + ' : ' + line);

    }

    }

    Note that rate is declared outside of entry points and can be used across the whole script.

    I hope the query is clear and we can receive some good suggestions on this.

  • #17932

    JohnCCole

    A 2.0 module isn’t global but it doesn’t sound like you need global a 2.0 Client Side Module can have module level variables that are shared across the different entry points in the same module. Just a simple example of what I mean

    Code:
    define([]],
    function() {
    var entryPointCount = 0;

    function pageInit(scriptContext) {

    entryPointCount ++;

    console.log(entryPointCount);

    }

    function fieldChanged(scriptContext) {

    entryPointCount ++;
    console.log(entryPoint)

    }

    return {

    pageInit: pageInit,

    fieldChanged: fieldChanged

    };

    });

  • #17933

    pcutler

    JohnCCole has the correct answer above, and that’s what I’d recommend. However, if for some reason you did need to have a global client side variable, window.myGlobalVariable = ‘hello world’ would work.

  • #17934

    j.j

    Originally posted by JohnCCole

    View Post

    A 2.0 module isn’t global but it doesn’t sound like you need global a 2.0 Client Side Module can have module level variables that are shared across the different entry points in the same module. Just a simple example of what I mean

    Code:
    define([]],
    function() {
    var entryPointCount = 0;

    function pageInit(scriptContext) {

    entryPointCount ++;

    console.log(entryPointCount);

    }

    function fieldChanged(scriptContext) {

    entryPointCount ++;
    console.log(entryPoint)

    }

    return {

    pageInit: pageInit,

    fieldChanged: fieldChanged

    };

    });

    Thanks for the response. This is fine if var entryPointCount = 0; but what if I needed this to be var own = scriptContext.newRecord.getValue({fieldId : “owner”});

    You see here the scriptContext object is not available. I can declare generic variables but cannot use scriptContext here in this example.

    Also, the approach goes something like this across all types of scripts:

    a. Calculate something beforeLoad

    b. Pass it to Client

    c. Get Value

    d. Do something on beforeSubmit/afterSubmit

    The point here is we need the scriptContext or other objects to be available outside of entrypoints which is not the case as these are only available in selected entrypoints.

  • #17935

    j.j

    Originally posted by pcutler

    View Post

    JohnCCole has the correct answer above, and that’s what I’d recommend. However, if for some reason you did need to have a global client side variable, window.myGlobalVariable = ‘hello world’ would work.

    Yes correct – we can have a global variable – but we needed scriptContext object outside of entrypoints to perform calculations, which is not available and throws error.

    In your case instead of window.myGlobalVariable = ‘hello world’, i would say try var own = scriptContext.newRecord.getValue({fieldId : “owner”}); this will not work as scriptContext is not available outside the entrypoint.

  • #17936

    JohnCCole

    j.j Focusing on the client side if you created a module level variable like entryPointCount in my example and instead called it currentRecord and and on pageInit did the following you would have a module level reference to current record that could be used anywhere within that module.

    Code:
    // Get a module level reference to current record
    if(currentRecord == null)currentRecord = scriptContext.currentRecord
    // Now anywhere in the module you can access the object
    currentRecord.getValue({fieldId:’somefield’})
    Or as pcutler pointed out in the page init you could put the currentRecord reference on the window object

    When you mention newRecord you’re talking server side correct? If you need to pass something from a server side script on beforeLoad for the clientSide to use you would need a non stored custom field which beforeLoad would set and that the clientSide could then use. In the server side you would use scriptContext.newRecord to set the value and on the client side you would use the module level reference you created in pageInit or scriptContext.currentRecord to use that value.

    If I’m still miss-understanding a quick code sample might help.

You must be logged in to reply to this topic.