Salesforce flow json to collection variable - json

I am receiving data from an external api in apex class. I am using a wrapper class to pass this data to salesforce screen flow. In debug the flow is showing that it receives data from the apex class. How can I convert this data to a collection variable to show in flow data table. In data table I tried to create new resource and tried to assign Apex defined variable but whenever I checked Multiple Values the data table just rejects it and no resource is shown in the resource. This is the data I am trying to show in data table.
{
"Product_Catagories": [{
"id": "8ad08aef8534de220185400383d82def",
"name": "Product One",
"description": "Desc One",
"region": "",
"category": "Main Products",
"ProductFamily": "Main",
"RelatedProducts": "POC-B0000001",
"productfeatures": []
}, {
"id": "8ad0887e8534de2701853fff5a9b22ee",
"name": "Product Two",
"description": "Desc Two",
"region": "",
"category": "Main Products",
"ProductFamily": "Main",
"RelatedProducts": "POC-B0000002",
"productfeatures": []
}, {
"id": "8ad08aef8534de2201853ffe48fc08f6",
"name": "Product Three",
"description": "Desc Three",
"region": "",
"category": "Main Products",
"ProductFamily": "Main",
"RelatedProducts": "POC-B0000003",
"productfeatures": []
}]
}

Show some code, not just the message you got.
You're using https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm right?
3rd example on that page shows how to return a list of helper class objects, public static List <Results> execute. So what you need is something like
public static List<CategoryWrapper> execute(List<Id> ids){
String response = new Http().send(yourRequestBuilder(ids)).getBody();
if(String.isNotBlank(response)){
Wrapper w = (Wrapper) JSON.deserialize(response, Wrapper.class); // your "main" helper class
if(w.Product_Catagories != null){
return w.ProductCatagories; // return to flow the array inside, not the whole object
}
}
return null; // something went wrong, or maybe throw exception
}
There may even be a code-free way to do it, we don't know what your API is. https://help.salesforce.com/s/articleView?id=sf.flow_http_callout.htm&type=5

Related

HATEOAS - problem with position of page links and page details

I'm starting to learn HATEOAS. In response, I would like to display the page details first, then the links to the pages and then the resources. Unfortunately, everything is displayed in reverse order. How could I put the data of "_links {}" and "page {}' at the beginning of Json response before the "_embedded {}" data. They always go to the end :(
My REST controller:
#RestController
#RequestMapping(value = "/api")
public class WebController {
private static final int DEFAULT_PAGE_NUMBER = 0;
private static final int DEFAULT_PAGE_SIZE = 5;
#Autowired
private AlbumRepository albumRepository;
#Autowired
private AlbumModelAssembler albumModelAssembler;
#GetMapping("/test")
public ResponseEntity<PagedModel<AlbumModel>> getAllAlbums(
#PageableDefault(page = DEFAULT_PAGE_NUMBER, size = DEFAULT_PAGE_SIZE) Pageable pageable,
PagedResourcesAssembler<AlbumEntity> pagedResourcesAssembler) {
Page<AlbumEntity> albumEntities = albumRepository.findAll(pageable);
Link selfLink = new Link(ServletUriComponentsBuilder.fromCurrentRequest().build().toUriString());
PagedModel<AlbumModel> collModel = pagedResourcesAssembler.toModel(albumEntities, albumModelAssembler,selfLink);
return new ResponseEntity<>(collModel, HttpStatus.OK);
}
}
I get the following response:
{
"_embedded": {
"albums": [
{
"title": "Top Hits Vol 1",
"description": "Top hits vol 1. description",
"releaseDate": "10-03-1981"
},
{
"title": "Top Hits Vol 2",
"description": "Top hits vol 2. description",
"releaseDate": "10-03-1982"
},
{
"title": "Top Hits Vol 3",
"description": "Top hits vol 3. description",
"releaseDate": "10-03-1983"
},
{
"title": "Top Hits Vol 4",
"description": "Top hits vol 4. description",
"releaseDate": "10-03-1984"
},
{
"title": "Top Hits Vol 5",
"description": "Top hits vol 5. description",
"releaseDate": "10-03-1985"
}
]
},
"_links": {
"first": {
"href": "http://localhost:8080/api/test?page=0&size=5"
},
"self": {
"href": "http://localhost:8080/api/test"
},
"next": {
"href": "http://localhost:8080/api/test?page=1&size=5"
},
"last": {
"href": "http://localhost:8080/api/test?page=1&size=5"
}
},
"page": {
"size": 5,
"totalElements": 10,
"totalPages": 2,
"number": 0
}
}
In general, RFC 8259 states the following:
JSON parsing libraries have been observed to differ as to whether or not they make the ordering of object members visible to calling software. Implementations whose behavior does not depend on member ordering will be interoperable in the sense that they will not be affected by these differences.
which does not recommend to depend on a certain given order structure. Certain implementations do i.e use a HashMap internally that is not able to retain the desired order.
If you use Jackson as your de/serialization framework you can use an annotation like
#JsonPropertyOrder({ "id", "title", "artist", "description", "releaseDate" })
public class AlbumEntity {
...
}
to return the data in the desired order on entity classes you want to return. I'm currently though not aware how you can convince Spring to generate a HAL response representation that aligns the elements (i.e. _links, embedded, ...) in the desired order.
However, even if you i.e. invoke an endpoint via curl, that may yield the desired order, and pipe the output to a pretty-printing tool like json_pp on a Mac OS X system, the order is shingled again, as it doesn't respect the given order. The point here is, every part in the chain needs to preserve the order of the JSON document otherwise the ordering of the content can not be guaranteed.

Multiple inputs - trying to send several request (http method: PUT)

I want to send different JSONs to an endpoint:
{{URL_API}}/products/{sku}
I need to update several information related to different products so i need to specify the product within the endpoint, i mean, i.e:
If you access this particular endpoint: {{URL_API}}/products/ you will get all the products but i need to specify the product that i want to update:
{{URL_API}}/products/99RE345GT
Take a look at this, i want to send a JSON like this:
{
"sku": "99RE345GT",
"price": "56665.0000",
"status": 1,
"group_prices": [
{
"group": "CLASS A",
"price": 145198.794
},
{
"group": "CLASS B",
"price": 145198.794
},
{
"group": "CLASS C",
"price": 145198.794
}
]
}
AND another one like this (both JSONs share the same structure BUT with different information):
{
"sku": "98PA345GT",
"price": "17534.0000",
"status": 1,
"group_prices": [
{
"group": "CLASS A",
"price": 145198.794
},
{
"group": "CLASS B",
"price": 145198.794
},
{
"group": "CLASS C",
"price": 145198.794
}
]
}
How can i do that?.I have already generated more than 200 JSONs for every product..
So, i have to update 200 products so i generated one JSON for every product, do you get me?
Following my example i would need to edit (somehow) the endpoint for every product and send a JSON, i.e:
since the first JSON has the SKU: 99RE345GT it should perform a http method: PUT over this enpoint:
{{URL_API}}/products/99RE345GT
Then, since the second JSON has the SKU: 98PA345GT it should perform a http method: PUT over this enpoint:
{{URL_API}}/products/98PA345GT
I have never done something like this before.. i read something about CSV + POSTMAN runner but i did not understand the way.
EDIT
I was working on a file (Excel file) and i did this:
So now i have all the different JSON for every product.
EDIT#2. It fails when it validates de Request_URL
I did this:
1)I created a new collection
2)I put this Request_url: {{URL_API}}/products/{{sku}}
3)I saved the changes and then, i went to the Collector Runner:
4)After cliking on the run button. i got this error message:
Invalid URL:
Have you tried adding those data sets to a CSV?
https://learning.postman.com/docs/postman/collection-runs/working-with-data-files/
If you have 2 column headers in a CSV file, one with sku and the other with requestBody - Add that variable value to the request body of the PUT request instead of the JSON.
sku,requestBody
99RE345GT, {JSON Payload}
98PA345GT, {...}
Add a couple of values under those headings to start with, once you prove that it works in the collection Runner.
Once you're happy, add the rest into the file. You may need to do some parsing of the JSON in the Pre-request Script but it should work.
Alternatively, use this template in the PUT request body and this create a CSV withe same column heading as the values in the {{...}} syntax. The values in the datafile will resolve to the values in the request body.
{
"sku": "{{sku}}",
"price": "{{price}}",
"status": {{status}},
"group_prices": [
{
"group": "{{groupA}}",
"price": {{groupAPrice}}
},
{
"group": "{{groupB}}",
"price": {{groupBPrice}}
},
{
"group": "{{groupC}}",
"price": {{groupCPrice}}
}
]
}
The CSV might look like this:
sku,price,status,groupA,groupAPrice,...
99RE345GT,1234,1,Group A, 555
98PA345GT,1235,1,Group A, 666

How to display item of nested json array in component using Angular 7

How can I display "empName" and other details of this JSON in my table component in loop?
I'm using a third party API which provides a nested JSON object in return when I send employerID to the API URL.
After subscribing I'm storing the response in a var "AllPostedJobs"
{
"status": "All Jobs",
"result": [
{
"_id": "5c90fd3cfc7f3b0017803319",
"job_title": "Web Designer",
"job_desc": "test description ...",
"location": "Mangalore",
"experiance": "Freshers",
"job_type": "Full-Time",
"salary_package": "3L",
"job_keywords": "Photoshop, Illustrator",
"email_id": "hr#shreemithra.com",
"employerID": "5c7e99c2a7a9eb00174de2b2",
"company_name": "Shreemithra Designs",
"AppliedStudentDetails": [
{
"_id": "5c9393c1a918d60017de7e55",
"empName": "Anup",
"empID": "5c939375a918d60017de7e53",
"job_title": "Web Designer"
}
],
"__v": 1
},
{
"_id": "5c913570cb78a100177ab23a",
"job_title": "Full Stack Developer",
"job_desc": "htjhsv dhsd jh jds fjshgdfkhsdmhfd;lw eiwiwemsd. This is a sample job description.",
"location": "Pune",
"experiance": "2 Years",
"job_type": "Part-Time",
"salary_package": "8L - 10L PA",
"job_keywords": "Angular, Node JS, React, HTML5. CSS3",
"email_id": "info#shreemithra.com",
"employerID": "5c7e99c2a7a9eb00174de2b2",
"company_name": "Shreemithra Designs",
"AppliedStudentDetails": [
{
"_id": "5c9393c9a918d60017de7e56",
"empName": "Anup",
"empID": "5c939375a918d60017de7e53",
"job_title": "Full Stack Developer"
},
{
"_id": "5ca60fa5ba17730017182ca8",
"empName": "Amit Pateriya",
"empID": "5c7795acfd39640017ca4c37",
"job_title": "Full Stack Developer"
}
],
"__v": 2
}
]
}
The simplest way is when you receive data from api then send it to a
function then apply multiples loop alter your data by add keys in
front of data and that values for example
var data = [{"id":1, "name":"smith","applicant":{"roll": 32,"class":10}}];
data[0].applicantRoll = data[0].applicant.roll;
data[0].applicantClass = data[0].applicant.class;
now you can apply *ngfor easily, try this.
You can bind display data by binding controls in an HTML template to properties of your component.
The easiest way to display a component property is to bind the property name through interpolation. With interpolation, you put the property name in the view template, enclosed in double curly braces:{{AllPostedJobs.status}}.
<div id="result-container" *ngFor="let record of AllPostedJobs.result">
<another-component [record]= "record"></another-component>
</div>
Or depending on your need you can hand over entire result data to another-component.
Now your another-component, should have #Input defined to handle the incoming data:
export class AnotherComponent {
#Input() record: Array<any>;
In your another-component template:
<div *ngFor="let student of record?.AppliedStudentDetails">
<span>{{student.empName}}</span>
<span [innerText]="student.job_title"></span>
</div>

How to read data from nested JSON structure in protractor

Below is my code and I want to extract data under "specs" part like description, status etc. however I'm getting undefined when I capture the data and print it in the console. I have tried
let web = JSON.parse(jsondata);
let TestSuite = web["suite1"]["description"]
and this is providing data in console however, when I use this,
let id = web["suite1"]["specs"]["id"]
its gives undefined. Please help!
{
"suite1": {
"id": "suite1",
"description": "Login",
"fullName": "Login",
"failedExpectations": [],
"status": "finished",
"specs": [
{
"id": "spec0",
"description": "Should able to login into the Distribution management Webpage",
"fullName": "Login Should able to login into the Distribution management Webpage",
"failedExpectations": [
{
"matcherName": "",
"message": "",
"stack": "",
"passed": false,
"expected": "",
"actual": ""
}
],
"passedExpectations": [],
"pendingReason": "",
"started": "2018-09-06T06:57:42.740Z",
"status": "failed",
"duration": "7 secs",
"stopped": "2018-09-06T06:57:49.255Z",
"browserLogs": []
}
]
} }
JSON.parse(JSON.stringify(json)).suite1.specs[0].id
specs contains an array of objects. First, you need to get the object from the array and then get a value of the object.
Try this:
let id = web["suite1"]["specs"][0]["id"]
OR
let id = web.suite1.specs[0].id
Hope this will work.
When you do ["suite1"]["specs"], you select array. You need an index, which is the 0-th one in this case.
You can check that by typing Object.prototype.toString.call( data["suite1"]["specs"]).
You can try with:
data["suite1"]["specs"][0]["id"]
Or use the object property notation
data["suite1"]["specs"][0].id

How to store a Json File Using Lift Mapper in MySql

I am new to Liftweb. I want to Store a Json File in Mysql database using Lift Mapper
My Json File Like Below:-
[
{
"name": "Root Category",
"Id": "1",
"dispName": "",
"childs": [
{
"name": "Sub Category",
"Id": "",
"dispName": "",
"childs": [
{
"name": "Spec1",
"Id": "",
"dispName": "",
"childs": []
}
]
}
]
},
{
"name": "Root Category",
"Id": "",
"dispName": "",
"childs": [
{
"name": "Sub Category",
"Id": "",
"dispName": "",
"childs": [
{
"name": "Spec1",
"Id": "",
"dispName": "",
"childs": []
}
]
}
]
}
]
Is it Possible to store a Json File in Lift Mapper .Please give me Suggestions. It will be great if some one provide any sample
Best Regards
GSY
At the moment there is no good support for storing JSON in MySQL. I mean it's not going to provide capabilities MongoDB provides for example. However there are some JSON processing functions provided by community if you want. Given all that you can store it in VARCHAR. TEXT or BLOB field type as simple text. Here is a Mapper example:
import net.liftweb.mapper._
import net.liftweb.common._
class SomeDbClass extends LongKeyedMapper[SomeDbClass] with IdPK {
def getSingleton = SomeDbClass
// set limit of chars - can be used in `validate()`
object quota_type extends MappedString(this, 1024)
}
object SomeDbClass extends SomeDbClass with LongKeyedMetaMapper[SomeDbClass]
For one of my projects I store JSON as a string in Postgres similarly because I just need to read and write it without having to parse it in DB and query by fields. Whenever I need efficient JSON storage with query and update support I use MongoDB with Record + ( Casbah or Rogue ).