Andrew Reid

My feedback

  1. 0 votes
    Vote
    Sign in
    (thinking…)
    Sign in with: Facebook Google
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    2 comments  ·  General Discussion  ·  Flag idea as inappropriate…  ·  Admin →
    Andrew Reid commented  · 

    Hi Matt,

    The native copy function will only duplicate the record on which you're currently on. To also duplicate children (and grandchildren and on down the chain) you'd want to make use of app scripts.

    I've generally found the best route is to create a field on the table which you can use to trigger the duplication. Your script then needs to loop through each level and duplicate the records starting at the parent. The basic structure would look something like this:

    // Check to see if the field used to trigger duplication is populated.
    if(currentValues["Duplicate"] == "Yes") {

    // Create a copy of the existing parent (or copy over only those fields which are relevant)
    Map newParent = [
    "Parent Field": currentValues["Parent Field"],
    "Parent Field 2": currentValues["Parent Field 2"]
    ]

    // Save the newly created parent.
    newParent = save("Parent Table", newParent, "Application")

    // Get the children of the current record.
    List<Map> children = find("Child Table", "Relationship to Parent", currentValues, "Application") as List<Map>

    // Loop through all children found
    for (child in children) {

    // Create a copy of the existing child (or copy over only those fields which are relevant)
    Map newKid = [
    "Child Field": child["Child Field"],
    "Parent Relationship": newParent
    ]

    // Save the newly created child.
    newKid = save ("Child Table", newKid, "Application")

    // Get the grandchildren of the original child.
    List<Map> grandchildren = find("Grandchild Table", "Relationship to Child", child, "Application") as List<Map>

    // Loop through the grandchildren.
    for (grandchild in grandchildren) {

    // Same process above for creating & saving a new record.

    }
    }

    // Reset the duplication field so it doesn't keep triggering on other updates.
    currentValues["Duplicate"] == null
    }

  2. 1 vote
    Vote
    Sign in
    (thinking…)
    Sign in with: Facebook Google
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    3 comments  ·  TrackVia API & App Scripts » App Script  ·  Flag idea as inappropriate…  ·  Admin →
    Andrew Reid commented  · 

    Hi George -

    Scripts require a database action in order to run. So the user must be either adding or updating a record in order to trigger a script. There's not a situation in which a script could be triggered where they're neither the creator nor the updater.

    Andrew Reid commented  · 

    Hi George-

    You can access either the Created By User (for record inserts) or the Last User (for record updates) via script.

    To pull that Last User value into a variable on record update, you'd use the following syntax:
    Map userVariable = currentValues["Last User"] as Map

    Once you've accessed the user record, you can either store that value in an Application User field or parse it via script to get the user's name or email address.

  3. 1 vote
    Vote
    Sign in
    (thinking…)
    Sign in with: Facebook Google
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    1 comment  ·  TrackVia API & App Scripts » App Script  ·  Flag idea as inappropriate…  ·  Admin →
    Andrew Reid commented  · 

    Hi Steve,

    When you get a list of records via an App Script, they'll be typically be sorted by the default sort. which is record created date. If you wish to sort by some other field in the record then it’s probably best to use the Groovy Spaceship Operator (http://groovy-lang.org/operators.html#_spaceship_operator).

    In the example below, I’m generating a list of child records from their common parent and then sorting them by a Date field in the child records. The second list (called “sortedKids”) returns all those children with the earliest date first.

    List<Map> kids = getChildren("Child Table", "Parent") as List<Map>
    List<Map> sortedKids = kids.sort({ a,b -> a['Date'] as Date <=> b['Date'] as Date }) as List<Map>

    As shown above, this does require casting objects as Dates in order for the sort to compile and execute properly.

Feedback and Knowledge Base