Category: Scenario-Based Questions PART 30
Updated on: August 12, 2025  |  0

Scenario-Based Questions PART 30

🎓 Scenario-Based Question

 

📌 Use Case: Accessing an Object Across Functions in Script Include

We need to create two functions f1 and f2 in a Script Include. Function f2 will create an object, and f1 will use it. Finally, we will fetch the firstName property via a background script.


🛠 Steps to Implement:

  1. Create a Script Include (Name: Test)
    • API Name: global.Test
    • Accessible from all application scopes
  2. Write the Script Include Code:
    var Test = Class.create();
    Test.prototype = {
        initialize: function() {},
    
        f1: function() {
            return this.f2(); // Call f2 and return its result
        },
    
        f2: function() {
            var obj = {};
            obj.firstName = "John";
            obj.lastName = "Y";
            return JSON.stringify(obj); // Convert object to JSON string
        },
    
        type: 'Test'
    };
          
  3. Run Background Script to Fetch First Name:
    var gr = new global.Test();        // Create object of Script Include
    var val = gr.f1();                 // Call f1 (which calls f2 internally)
    var answer = JSON.parse(val);      // Convert JSON string back to object
    gs.print(answer.firstName);        // Output: John
          

💡 Pro Tip:

Returning JSON strings from Script Includes is useful for passing structured data between server-side scripts, but for better readability, you can return the object directly instead of converting to JSON if you don’t need serialization.

Comments

No comments yet.


Log in to post a comment