RE: WEB-API Request to create a quote based on an opportunity
Hello Jeo!!
In order to add more information to your new quote, you need to add the other properties into the body of your request.
If you want to add the account and contact associated with your opportunity, you need to indicate the account id and contact id. If your opportunity is already linked to the opportunity, you can simply get the values from there through the executionContext of the opportunity (not sure you can get them using Postman), here is an example:
Postman Body:
{
"quotenumber": "ZYY-Q21-00141",
"totalamount": 0,
"opportunityid@odata.bind": "/opportunities(71dfe9e9-a3ee-eb11-bacb-0022487ef2a3)"
"customerid@odata.bind": "/accounts(accountid)"
}
Where accountid is the id of the associated account.
Javascript web resource:
function createQuote (executionContext) {
const formContext = executionContext.getFormContext();
const opportunityId = formContext.data.entity.getId().slice(1,-1);
const accountId = formContext.getAttribute(accountField).getValue();
const contactId = formContext.getAttribute(contactField).getValue();
const parameters = {
quotenumber: "ZYY-Q21-00141",
totalamount: 0,
opportunityid@odata.bind: "/opportunities(" opportunityId ")",
accountid@odata.bind: "/accounts(" accountId ")",
contactid@odata.bind: "/contacts(" contactId ")"
}
Request("POST", formContext.context.getClientUrl() '/api/data/v9.2/quotes', parameters);
}
function Request (action, uri, parameters) {
const data = parameters || {};
return new Promise(function (resolve, reject) {
var request = new XMLHttpRequest();
request.open(action, uri, true);
request.setRequestHeader("OData-MaxVersion", "4.0");
request.setRequestHeader("OData-Version", "4.0");
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
request.setRequestHeader("Prefer", "odata.maxpagesize=100");
request.onreadystatechange = function () {
if (this.readyState === 4) {
request.onreadystatechange = null;
switch (this.status) {
case 200: // Success with content returned in response body.
case 204: // Success with no content returned in response body.
resolve(this);
break;
default: // All other statuses are unexpected so are treated like errors.
var error;
try {
error = JSON.parse(request.response).error;
} catch (e) {
error = new Error("Unexpected Error");
}
reject(error);
break;
}
}
};
request.send(JSON.stringify(data));
});
}
Note that the fields' name I used are an example, make sure to replace them with the name of your fields before using the code
For additional information:
MS Dataverse Web API
Request Function Source
@odata.bind documentation