I would like to "stringify" the object what the sendgrid template received. With other words, How can I use an unknown structured object with the sendgrid template.
Example:
My object:
{"test": {"foo": { "bar": {"foo2": "bar2"}}}
My sendgrid template:
... what is inside the test object: {{ test }}
And the result is:
... what is inside the test object: [object Object]
However I would like to something like this:
... what is inside the test object: {"foo": { "bar": {"foo2": "bar2"}}}
Is it possible somehow?
Related
I basically want to be able supply an array of objects to the dynamicTemplateData. When the template in Sendgrid receives this data, it should replicate the template within the email its creating for each object in the array. Is this currently possible, or would I need to send separate emails for each object?
Twilio SendGrid developer evangelist here.
In a SendGrid dynamic template you use handlebars to render the data into the email. If you have an array of items you can iterate through the array to render each of the object using the #each function.
For example, with the data
{
"user": {
"orderHistory": [
{
"date": "2/1/2018",
"item": "shoes"
},
{
"date": "1/4/2017",
"item": "hat"
}
]
}
}
You can write a template like this:
<ol>
{{#each user.orderHistory}}
<li>You ordered: {{this.item}} on: {{this.date}}</li>
{{/each}}
</ol>
And it will render:
<ol>
<li>You ordered: shoes on: 2/1/2018</li>
<li>You ordered: hat on: 1/42017</li>
</ol>
There are more examples in the documentation.
We are using Rest API via PowerShell (Invoke-RestMethod), in order to insert records in ServiceNow event [em_event] table with a single call, using the web service API.
We successfully inserting events to the em_event table,
but the only problem is with the additional_info field.
For some reason,
The JSON structure of my PowerShell script,
Is causing the output of additional_info, to return as an Object and Not as JSON string.
And as a result,
The values in additional_info not showing properly, but instead as [object Object]:
This is the JSON structure in my PowerShell script:
# Specify request body
$body = #"
{ "records":
[
{
"source":"MyClass",
"event_class":"$AtargetResourceType",
"resource":"$AtargetResourceType",
"node":"$AtargetResourceName",
"metric_name":"$Aname",
"type":"$AsignalType",
"severity":"$Aseverity",
"message_key":"$Aid",
"u_mc_object":"$AtargetResource",
"description":"$Adescription",
"additional_info":"{
'u_mc_object_class':'$AsourceCreatedId',
'u_mc_parameter':'$AmetricName',
'u_mc_parameter_value':'$AmetricValue'
}"
}
]
}
"#
image which you have posted is not opening. but according to your issue below line will return string value for additional_info:
($body|ConvertFrom-Json).records.additional_info
I had the same issue sending the request using Postman, I was sending the request like this:
{ "records":
[
{
"source":"BMC TrueSight",
"type":"Incident from trusight",
"severity":"1",
"description":"This is a test from WEB SERVICE API ERROR",
"additional_info":{
"status":"new",
"description":"This is a descriotion from additional information",
"category":"41",
"subcategory": "test",
"company": "test",
"business_service":"test"
}
}
]
}
and it showed [object][object] in the additional information field, what I did was sent that field as a string like this:
"additional_info": "{\"assignee_group\":\"ETSS\/UNIX99\",\"status\":\"new\",\"description\":\"This is a descriotion from additional information\",\"category\":\"41\",\"subcategory\":\"test\",\"company\":\"test\",\"business_service\":\"
}
You only need to convert the JSON into a string.
I'm a total rythm newbie and got stuck on one of my simple tests (tested here: http://fiddle.rythmengine.com/)
I guess it's easiest to explain the problem in a simple example:
My JSON input args:
{myObj:
{name: "test", values: [ {id: 1}, {id: 2} ]
}
}
so the values property is an array of object - and those objects only have one property id.
my test-template:
#args Object myObj
Hello #myObj.name#
#for (Object v: myObj.values) {
#// this fails
}
this fails:
org.rythmengine.exception.CompileException: values cannot be resolved or is not a field
what is wrong here?
i.e. outside of the loop I can access myObj.values
#args Object myObj
Hello #myObj.name#
#myObj.values#
You declared myObj as Object which doesn't have the value property at all.
Solution:
Make your JSON string look like:
{
name: "test", values: [ {id: 1}, {id: 2} ]
}
Your template code:
#args String name, Map[] values
Hello #name#
#for (Map v: values) {
#v.get("id")
}
Result:
Hello test
1
2
Tested on http://fiddle.rythmengine.com/
I have the following user object returned from a REST Api:
{
user: {
id: 3451,
name: "First Last",
favorites: {
designs: {
name: "Design 1",
url: "Url 1"
},
typo: {
name: "Typo 1",
url: "Url 2"
},
games: {
name: "Game 1",
url: "Url 3"
}
}
}
}
I got this response from the url /users/3451.
And here's my User model:
App.User = DS.Model.extend({
name: DS.attr('string'),
favorites: DS.attr(), // ??? What to put here?
});
I have no problem displaying the {{name}}. But in the favorites, I don't know.
I tried using {{#each}}
{{#each favorites}}
{{#key}} - {{name}}
{{/each}}
but no luck.It throws an error: Error: Assertion Failed: The value that #each loops over must be an Array. You passed [object Object]
What is the correct way of handling these kinds of complex objects in EmberJS? Please help.
I think the error is pretty self explanatory: you need to be looping over an array, not an object. Here's how I would convert that object to an array, while saving the key (put this in your model):
favoritesArray: function() {
var favorites = this.get('favorites');
return Em.keys(favorites).map(function(key) {
return {
key: key,
data: favorites[key]
};
});
}.property('favorites.#each.{name,url}')
Then, in your template:
{{#each favoritesArray}}
{{key}} - {{data.name}}
{{/each}}
That would be the easiest way to do it. But if you're looking for a slightly better way (in my opinion), you can user a type transform to convert the data to the format you need at the time of (de)serialization.
EDIT: Just for a bit of background info, I believe the reason that Ember.js doesn't support iterating over objects is because there is no way to bind to the object keys. Ember.js knows to update a bound helper when the dependent key observers are fired, but as far as I know, there is no way to observe the keys of an object. Something like this might be possible using an Map or similar, but I don't think that it's built in functionality.
I have a REST API consumed by an ember app.
Here is the .hbs which lists a model. I can give new elements with the "save" action, and every item has also a "delete" action.
{{ input value=name }}
{{ input value=value }}
<button {{ action "save" this }}>Save</button>
<table class="table">
{{#each item in model}}
<tr><td>{{item.name}}</td><td>{{float2 item.value}}</td><td><button {{ action "delete" item}}>delete</button></td></tr>
{{/each}}
</table>
So far works everything fine. The problem if i insert (save) a new element, i cant "delete" it, it has id:null, and the request to the api has no id at the end of the URL. (although on the client side the item will be removed from the list)
here is the controller's actions:
actions: {
save: function (record) {
var vat = this.store.createRecord('vat',{
name: this.get('name'),
value: parseFloat(this.get('value'))
});
vat.save();
},
delete: function(record){
console.log(record);
record.deleteRecord();
record.save();;
}
}
My guess is after the insert the API doesnt have the correct response, and ember-data doesnt know the new item's id. (maybe im wrong) what respons (JSON structure) expects the RESTAdapter, with which status code?
In your case the response from server should have 200 or 201 status code and the body should look like this
{
"vat": {
"id": 1,
"name": "Name",
"value": 10.5
}
}
You can also override normalize function in RESTAdapter to adjust the format of server response.