You need to sign in to do that
Don't have an account?
Sreeram Venkatesh
Can someone help to write the below python code in apex
rows = [
{'Name': 'Name 1', 'Date': '06-Aug', 'Jan': 150, 'Feb': None, 'March': None, 'Apr': None, 'May': None},
{'Name': 'Name 2', 'Date': '07-Aug', 'Jan': None, 'Feb': 100, 'March': 110, 'Apr': None, 'May': None},
{'Name': 'Name 3', 'Date': '07-Aug', 'Jan': None, 'Feb': None, 'March': 400, 'Apr': 500, 'May': None},
]
def get_formatted_data(rows):
data = []
# For each row of rows
for row in rows:
# Loop through each key, value pair of row
for key, val in row.items():
data_dict = {
'Name': row['Name'],
'Date': row['Date']
}
if key not in ('Name', 'Date') and val:
data_dict['Month'] = key
data_dict['Rate'] = val
data.append(data_dict)
return data
{'Name': 'Name 1', 'Date': '06-Aug', 'Jan': 150, 'Feb': None, 'March': None, 'Apr': None, 'May': None},
{'Name': 'Name 2', 'Date': '07-Aug', 'Jan': None, 'Feb': 100, 'March': 110, 'Apr': None, 'May': None},
{'Name': 'Name 3', 'Date': '07-Aug', 'Jan': None, 'Feb': None, 'March': 400, 'Apr': 500, 'May': None},
]
def get_formatted_data(rows):
data = []
# For each row of rows
for row in rows:
# Loop through each key, value pair of row
for key, val in row.items():
data_dict = {
'Name': row['Name'],
'Date': row['Date']
}
if key not in ('Name', 'Date') and val:
data_dict['Month'] = key
data_dict['Rate'] = val
data.append(data_dict)
return data
You need to use custom classes to handle the data.
As you go in each record throught all months, I'm adding a map to handle the month's by their name and their value.
If you want to convert your json data to the init class, you can go to this link: https://developer.salesforce.com/forums/?id=9060G0000005nPLQAY
Not sure what the code does, but I hope this helps.
All Answers
You need to use custom classes to handle the data.
As you go in each record throught all months, I'm adding a map to handle the month's by their name and their value.
If you want to convert your json data to the init class, you can go to this link: https://developer.salesforce.com/forums/?id=9060G0000005nPLQAY
Not sure what the code does, but I hope this helps.
Thanks a lot for your detailed explanation. This was very helpful and it pretty much solved my doubts. I also have an other scenario for the above mentioned code. I would be quering the above rows data from database. If that's the case, how can I proceed as the number of rows would be dynamic. Also the formatted data needs to be stored in another table. Would be helpful if you could assit on the same. Thanks in advance.
Regards,
Sreeram
In the case you will query the rows data with SOQL you'd need to create one Object Rows (Salesforce doesn't use tables but Standard/Custom Objects) with these fields: Name, Date and Months. Months field should be a lookup field (relationship) to another Object call Months. That way you could access to the data as the next SOQL:
I think you need to figure how to sort your data into custom objects of Salesforce. And also I recommend you to get more fundamentals of these platform because has a multi-tenant architecture and the way we develop should have many things to have in mind because of the shared resources between customers.
I'm leaving you some links to get more knowleadge about it:
- https://trailhead.salesforce.com/en/content/learn/modules/starting_force_com/starting_understanding_arch
- https://trailhead.salesforce.com/content/learn/modules/apex_database?trail_id=force_com_dev_beginner
Hi, thanks for your response. I managed to query from DB. But according to your earlier code, the final formatted data stored in result seems to be incorrect. As seen in the below log, only the last month with value is taken from each map and the records are repeated. Please find the below debug log on "result" for your reference.
result|[{"Month":"Jan","MyDate":"06-Aug","Name":"Name 1","Rate":150},{"Month":"March","MyDate":"07-Aug","Name":"Name 2","Rate":110},{"Month":"March","MyDate":"07-Aug","Name":"Name 2","Rate":110},{"Month":"Apr","MyDate":"07-Aug","Name":"Name 3","Rate":500},{"Month":"Apr","MyDate":"07-Aug","Name":"Name 3","Rate":500}]
One's highlighted in bold are repeated and also Feb from second map & march from third map are missing.