Following code can be used to add records to Marketing List using JavaScript and WebApi:
var parameters = { List: { listid: "9DEE7D76-611D-E811-A95B-000D3A1087A0", "@odata.type": "Microsoft.Dynamics.CRM.list" }, Members: [{ accountid: "3653A521-A2C0-E711-A950-000D3A109280", "@odata.type": "Microsoft.Dynamics.CRM.account" }] }; var context; if (typeof GetGlobalContext === "function") { context = GetGlobalContext(); } else { context = Xrm.Page.context; } var req = new XMLHttpRequest(); req.open("POST", context.getClientUrl() + "/api/data/v8.2/AddListMembersList", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 204) { //Success - No Return Data - Do Something } else { var errorText = this.responseText; //Error and errorText variable contains an error - do something with it } } }; req.send(JSON.stringify(parameters));
Hi Andrew,
Samething we are doing and code also same but we are facing an issue while 3000 members adding . getting error like ‘ Failed to insert audit record” . Please answer if you have any idea about this. It will help us.
Kirna,
To be honest I have no idea about that error, never seen that error before.
Thanks,
Andrew
Hi Andrew, and others,
If you are trying to add marketing list members through the webAPI and you’re faced with the error message “Specified cast is not valid.”, then make sure that you’re adding contacts to a list that is targeted at contacts, and not inadvertedly mix contacts, accounts and leads to lists that aren’t targeted at that entity type.
Cheers!
Thanks, Ronald!
How create a new Marketinglist?
I try’d a POST to list ({{webapiurl}}lists) with following body
{
“listname”:”TEST MB 120122 List”,
“purpose”: “Test with leads”,
“source”: “leads”,
“cost”: 10000
}
but I got an error from the endpoint:
“error”: {
“code”: “0x80040265”,
“message”: “Object reference not set to an instance of an object.”
}
Susan, you should add createdfromcode field to your request that should have one of values – 1 if the target list is for accounts, 2 – for contacts and 4 for leads. Andrew