Could someone please explain me below code?

Venkat141
Tera Contributor

Could you please explain below code. As it's OOB script I'm not able to understandthe script properly.

 

Below script is written on Transform Map script

 

function setMakeAndModel() {
    var mm = MakeAndModelJS.fromNames(source.u_manufacturer, source.u_model, "hardware");
    target.model_id = mm.getModelNameSysID();
    target.manufacturer = mm.getManufacturerSysID();
}
 
Below code is written on script include.
 
MakeAndModelJS.fromNames = function(make, model, modelType) {
    var modelTable = SncMakeAndModel.determineModelTableName(modelType);
    var makeAndModelJava = SncMakeAndModel.fromNames(make, model, modelTable);      
    var mm = new MakeAndModelJS(makeAndModelJava);      
    return mm;
};

 

@Ankur Bawiskar 

@Peter Bodelier 

@AnveshKumar M 

@Vishal Birajdar 

@Sandeep Rajput

@Atulya - LNG 

@Saurav11 

1 ACCEPTED SOLUTION

Harish Bainsla
Tera Sage
Tera Sage
  1. function setMakeAndModel(): This is a function definition in the Transform Map script. It is intended to set the "make" and "model" fields on a target record (usually a table) based on data from a source record (which is also a table).

  2. var mm = MakeAndModelJS.fromNames(source.u_manufacturer, source.u_model, "hardware");: In this line, a JavaScript variable mm is declared. This variable is being set to the result of the MakeAndModelJS.fromNames function call. It is passing three arguments:

    • source.u_manufacturer: This is presumably a field from the source record that contains the manufacturer information.
    • source.u_model: This is presumably a field from the source record that contains the model information.
    • "hardware": This string indicates the model type, which is set as "hardware."
  3. target.model_id = mm.getModelNameSysID();: This line sets the "model_id" field on the target record to the result of the mm.getModelNameSysID() function call.

  4. target.manufacturer = mm.getManufacturerSysID();: This line sets the "manufacturer" field on the target record to the result of the mm.getManufacturerSysID() function call.

Now, let's dive into the code within the script include MakeAndModelJS.fromNames.

  1. MakeAndModelJS.fromNames = function(make, model, modelType): This code defines a function within the MakeAndModelJS object called fromNames. It expects three arguments:

    • make: The manufacturer name.
    • model: The model name.
    • modelType: The type of model (e.g., "hardware").
  2. var modelTable = SncMakeAndModel.determineModelTableName(modelType);: This line determines the appropriate table name based on the modelType. It uses a function called determineModelTableName in the SncMakeAndModel object to do this. The result is stored in the modelTable variable.

  3. var makeAndModelJava = SncMakeAndModel.fromNames(make, model, modelTable);: This line retrieves the data from the ServiceNow instance using the SncMakeAndModel.fromNames function, passing the make, model, and modelTable as arguments. The result is stored in the makeAndModelJava variable.

  4. var mm = new MakeAndModelJS(makeAndModelJava);: This line creates a new MakeAndModelJS object, which is initialized with the data retrieved from the ServiceNow instance using the makeAndModelJava object.

  5. return mm;: Finally, the mm object (an instance of MakeAndModelJS) is returned from the fromNames function.

View solution in original post

1 REPLY 1

Harish Bainsla
Tera Sage
Tera Sage
  1. function setMakeAndModel(): This is a function definition in the Transform Map script. It is intended to set the "make" and "model" fields on a target record (usually a table) based on data from a source record (which is also a table).

  2. var mm = MakeAndModelJS.fromNames(source.u_manufacturer, source.u_model, "hardware");: In this line, a JavaScript variable mm is declared. This variable is being set to the result of the MakeAndModelJS.fromNames function call. It is passing three arguments:

    • source.u_manufacturer: This is presumably a field from the source record that contains the manufacturer information.
    • source.u_model: This is presumably a field from the source record that contains the model information.
    • "hardware": This string indicates the model type, which is set as "hardware."
  3. target.model_id = mm.getModelNameSysID();: This line sets the "model_id" field on the target record to the result of the mm.getModelNameSysID() function call.

  4. target.manufacturer = mm.getManufacturerSysID();: This line sets the "manufacturer" field on the target record to the result of the mm.getManufacturerSysID() function call.

Now, let's dive into the code within the script include MakeAndModelJS.fromNames.

  1. MakeAndModelJS.fromNames = function(make, model, modelType): This code defines a function within the MakeAndModelJS object called fromNames. It expects three arguments:

    • make: The manufacturer name.
    • model: The model name.
    • modelType: The type of model (e.g., "hardware").
  2. var modelTable = SncMakeAndModel.determineModelTableName(modelType);: This line determines the appropriate table name based on the modelType. It uses a function called determineModelTableName in the SncMakeAndModel object to do this. The result is stored in the modelTable variable.

  3. var makeAndModelJava = SncMakeAndModel.fromNames(make, model, modelTable);: This line retrieves the data from the ServiceNow instance using the SncMakeAndModel.fromNames function, passing the make, model, and modelTable as arguments. The result is stored in the makeAndModelJava variable.

  4. var mm = new MakeAndModelJS(makeAndModelJava);: This line creates a new MakeAndModelJS object, which is initialized with the data retrieved from the ServiceNow instance using the makeAndModelJava object.

  5. return mm;: Finally, the mm object (an instance of MakeAndModelJS) is returned from the fromNames function.