Rules scripting

I have items I need to submit to a webhook using the Rules engine. Each item has a few referenced fields. How would I go about resolving multiple referenced fields? This does not work at all.

var jsonObject = {
        id:event.id,
        tags: [],
        categories:[]
    };
    
getReferences(event.data.tags.iv, function(tagNames){
    
    for(var tag of tagNames){
        jsonObject.tags.push({
            id:tag.id,
            tagName: tag.data.tagName.iv
        });
    }

}
);


getReferences(event.data.categories.iv, function(categories){
    
    for(var cat of categories){
        jsonObject.categories.push({
            id:cat.id,
            tagName: cat.data.displayName.iv
        });
    }

}
);


JSON.stringify(jsonObject)

What do you mean with "It does not work at all?

I was expecting it to populate the two arrays (categories and tags) on the original JSON object. However, here is the simulated results:

{
"method": "POST",
"requestUrl": "MYURL-REDACTED",
"requestSignature": "QASYBo4UDwZWA6jDyl3khAsak587VH7BAC5mUqtYmjk=",
"requestBody": "{\"id\":\"6cf6bafe-3ea7-4083-8bf1-1bba672e23f1\",\"tags\":[],\"categories\":[]}",
"requestBodyType": "application/json"
}}

They both have referenced data:

 "tags": {
        "iv": [
            "2447c5c0-0fc8-4ba4-b641-c56677ade828",
            "e3a8f61c-475d-41ce-92d5-09b647539470",
            "7c7b2e30-5de0-4fc1-81d1-989ab394be64"
        ]
    },
    "categories": {
        "iv": [
            "1a1a971e-df85-4989-8a8e-4940eec25dd1",
            "69236cf8-0c3b-4fae-a761-1f5dd3683511"
        ]
    }

The order is wrong, it should be like this

var jsonObject = {
    id:event.id,
    tags: [],
    categories:[]
};

getReferences(event.data.tags.iv, function(tagNames) {
    for (var tag of tagNames){
        jsonObject.tags.push({
            id:tag.id,
            tagName: tag.data.tagName.iv
        });
    }

    getReferences(event.data.categories.iv, function(categories){
        for (var cat of categories){
            jsonObject.categories.push({
                id:cat.id,
                tagName: cat.data.displayName.iv
            });
        }
    
        JSON.stringify(jsonObject)
    });
});

Thank you. I just tried, but…

“requestBody”: “null”

Can you try

complete(JSON.stringify(jsonObject));

At the same place?

Made the change but sorry, still null.

Looks like I got it finally working…the jsonObject var had to be defined inside the getReferences function. I’m going to do some more testing with other fields.

getReferences(event.data.tags.iv, function(tagNames) {
	var jsonObject = {
    id:event.id,
    tags: [],
    categories:[]
	};


   for (var tag of tagNames){
        jsonObject.tags.push({
            id:tag.id,
            tagName: tag.data.tagName.iv
        });
    }

    getReferences(event.data.categories.iv, function(categories){
        for (var cat of categories){
            jsonObject.categories.push({
                id:cat.id,
                tagName: cat.data.displayName.iv
            });
        }
    
    });
	
	
	 complete(JSON.stringify(jsonObject));});

Almost there…I can’t get a 3rd reference lookup to work. When I tried to add Authors getRef function, categories ends up being null. If I take out Authors function, categories populates.

getReferences(event.data.tags.iv, function(tagNames) {
	var jsonObject = {
    id:event.id,
    tags: [],
    categories:[],
    authors:[]
	};

   for (var tag of tagNames){
        jsonObject.tags.push({
            tagName: tag.data.tagName.iv
        });
    }

    getReferences(event.data.categories.iv, function(categories){
        for (var cat of categories){
            jsonObject.categories.push({
               category: cat.data.displayName.iv
            });
        }
       
    });
    

  getReferences(event.data.authorsNew.iv, function(authors){
        for (var author of authors){
            jsonObject.authors.push({
               author: author.data.displayName.iv
            });
        }
    
});

complete(JSON.stringify(jsonObject));});

Finally! (hopefully)

getReferences(event.data.tags.iv, function(tagNames) {
	var jsonObject = {
    id:event.id,
    tags: [],
    categories:[],
    authors:[]
	};


   for (var tag of tagNames){
        jsonObject.tags.push({
            tagName: tag.data.tagName.iv
        });
    }

    getReferences(event.data.categories.iv, function(categories){
        for (var cat of categories){
            jsonObject.categories.push({
               category: cat.data.displayName.iv
            });
        }
        
        
       
    });
    

  getReferences(event.data.authorsNew.iv, function(authors){
        for (var author of authors){
            jsonObject.authors.push({
               author: author.data.displayName.iv
            });
        }
    
    	 complete(JSON.stringify(jsonObject));
    
});
});

The getXXX(callback) methods are async methods, that do not complete immediately. This means that the execution order is like this

// 1
getReferences(event.data.tags.iv, function(tagNames) {
 // 3
});
// 2

With complete() you are telling the scripting engine that you are done.

So for one async call it is

getXXX(function () {
  complete(result);
});

2 calls

getXXX(function () {
  getXXX(function () {
    complete(result);
  });
});

3 calls

getXXX(function () {
  getXXX(function () {
    getXXX(function () {
      complete(result);
    });
  });
});
1 Like