Hey,
I had .Name in the mix as that was the label in the objecttype but I changed that so all I am focusing on now is the leaveStartDate attribute ID 2468.
Here is the code which sadly still isn’t working. The URL I am using for the fetch is the create one I found in the API. If I get this to work I will set it differently and capture workspace ID as the variable as I will want to have these entered when the app is configured for the first time for each customer I use this for (a whole new level of confusion right there).
Here is the code with the fixes in place that I think you were referring in your reply, still no joy though.
import ForgeUI, { Form, DatePicker, UserPicker, Toggle, render, QueuePage } from '@forge/ui';
import fetch from '@forge/api';
const App = () => {
async function createAsset(attributeValues) {
const accessToken = 'TOKEN'; // Replace with your actual access token
const assetsUrl = 'https://api.atlassian.com/jsm/assets/workspace/WORKSPACEVALUE/v1';
try {
const response = await fetch(`${assetsUrl}/object/create`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
objectTypeId: '256', // Replace with the actual object type ID
attributes: [
{
objectTypeAttributeId: 2468, // Replace with actual Object Type Attribute Id
objectAttributeValues: [
{
value: attributeValues.leaveStartDate
}
] // Closing square bracket for objectAttributeValues
}
] // Closing square bracket for attributes
})
});
if (response.ok) {
const responseData = await response.json();
console.log('New asset created in Assets:', responseData);
} else {
console.error('Error creating asset in Assets:', response.statusText);
}
} catch (error) {
console.error('Error:', error);
}
}
async function onSubmit(formProps) {
const newAssetValues = {
leaveStartDate: formProps.date
// Add more attribute values as needed
};
try {
await createAsset(newAssetValues);
console.log('Asset creation successful');
} catch (error) {
console.error('Error creating asset:', error);
}
}
return (
<Form onSubmit={onSubmit}>
<DatePicker
name="date"
label="Leave Start Date"
description="First day of your OOO period"
/>
<DatePicker
name="date2"
label="Leave End Date"
description="Last day of your OOO period"
/>
<UserPicker
name="User"
label="Select User"
/>
<Toggle
label="Out of Office already"
name="isSectionVisible"
/>
{/* Add more form fields as needed */}
</Form>
);
};
export const run = render(
<QueuePage>
<App />
</QueuePage>
);
when I run forge log I can see this
ERROR 2023-08-09T22:59:16.546Z 9e7aa110-7d6d-400c-ac8d-4c1577913228 Error creating asset in Assets: { code: 401, message: 'Unauthorized' }
INFO 2023-08-09T22:59:16.547Z 9e7aa110-7d6d-400c-ac8d-4c1577913228 Asset creation successful
I ran a little powershell script using the exact same token and it connects fine to the sandbox. Not sure why I am getting a 401 error.
I assume this is correct
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
I used the token to generate the workspaceID so I know it is valid.
Something is and isn’t working… SURELY I am close now