---
title: "Use Script"
canonical: "https://docs.perspectium.com/space/gold/1298842/Use+Script"
format: markdown
---
> Macro (html-bobswift)


Table maps can be configured to use scripts to serialize the record yourself. Within the script, you can set the variable **answer** to be the value you want the record to be serialized and returned as.

The following variables are available to use:

| **Variable** | **Description** |
| --- | --- |
| current | This represents the record that is being shared |
| gr_tablemap | This represents the outbound table map itself. For example if you want to access the table map's name, you can use gr_tablemap.u_name |

:info: **<u>NOTE</u>**: Adding a script to your table map is an advance configuration. Contact [Perspectium Support](mailto:support@perspectium.com) for more information. 


### Example of a table map script

```javascript
/*
 * Custom Table Map
 */
 
//Serialize 'current' record into an XMLDocument
var recordSerializer = (typeof GlideRecordXMLSerializer != 'undefined') ? new GlideRecordXMLSerializer() : new Packages.com.glide.script.GlideRecordXMLSerializer(); 
var xmlstr = recordSerializer.serialize(current);
var xmlDoc = new XMLDocument(xmlstr);
 
//Process Display Values As Necessary
var pspUtil = new PerspectiumUtil();
var addDisplayValues = pspUtil.getPspPropertyValue("com.perspectium.replicator.add_display_values", "true");
var currentFieldsOnly = pspUtil.getPspPropertyValue("com.perspectium.replicator.share_current_fields", "false");
if (currentFieldsOnly == "true" || addDisplayValues == "true") {
    addDVFields();
}
 
/*
 * Any extra mapping, to add
 */
 
// Send the XMLDoc string to our answer
answer = xmlDoc.toString();
 
// Helper Functions
 
//Standard DV Field Processing
function addDVFields(){
    var fl = (typeof GlideFieldList != 'undefined') ? new GlideFieldList() : new Packages.com.glide.processors.FieldList();
    var tableName = current.getTableName();
    var fieldNames = fl.get(current.getTableName(),"");
    var arrFields = current.getFields();
 
    for (var i = 0; i < arrFields.size(); i++) {
        var glideElement = arrFields.get(i);
        var ed = glideElement.getED();
        var elName = glideElement.getName();
 
        if (!fieldNames.contains(elName) || (currentFieldsOnly == "true" && tableName != ed.getTableName())) {
            removeElement(elName);
        }
 
        // Create dv fields for reference, choice, or lists
        if (ed.isReference() || ed.isChoiceTable() || ed.getInternalType() == "glide_list") {
            addElement("dv_" + elName, glideElement.getDisplayValue());
        }
 
        if (!glideElement.hasValue()) {
            continue;
        }
    }
}
 
//Remove an element from xmlDoc
function removeElement(elName){
    var nn = xmlDoc.getElementByTagName(elName);
    if(nn && nn.parentNode) {
        nn.parentNode.removeChild(nn);
    }
}
//Add an element from xmlDoc
function addElement(elName, elValue){
    xmlDoc.createElement(elName, elValue);
}
```

The example above mimics the replication process without the use of a table map. The script will contain the following:

1. Creating and serializing the base record
2. Adding Display Values as necessary
3. A section for your custom changes
4. Converting the XMLDocument to a string and assigning it to the **answer **variable
5. Helper functions which you can call

You can then utilize the function** removeElement(elName)** and **addElement(elName, elValue)** to modify the outgoing columns and data as you see fit.


:info: **<u>NOTE</u>**: To use **current.operation()** or **psp_action**, there is an on-goin research on retaining value of current.operation() in the Table Map and Field Map. As a workaround, use **current.psp_action = action**.

Thus, the following can be added to the dynamic share's **Before Share Script** to address both concerns:

```javascript
current.psp_action = psp_action;
var prev = new GlideRecord(current.getTableName());
prev.get('sys_id', current.sys_id);
```


### Procedure

To add a script to your table map, follow these steps: 

---

> Macro (legacy-content)

> Macro (legacy-content)



## **<span style="color: #000000">Similar topics</span>**

---

> Macro (contentbylabel)

## **<span style="color: #000000">Contact Perspectium Support</span>**

---

#### > Macro (inline-media-image)

<span style="color: #333333">**US: **</span>[<span style="color: #333333">**1 888 620 8880**</span>](tel:18886208880)

#### <span style="color: #333333">**UK: **</span>[<span style="color: #333333">**44 208 068 5953**</span>](tel:442080685953)

#### [<span style="color: #333333">**support@perspectium.com**</span>](mailto:support@perspectium.com)<span style="color: #333333">** **</span>** **