web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM Action Call failing

(1) ShareShare
ReportReport
Posted on by 605

Hi All,

My requirement is:

1. Contact Entity we have a button, on click of button action is being called.

2. On click of button i am getting the list of closed cases associated with that contact via web api.

3. Now i need to pass this result (from step 2) to action input parameter.

Action Input parameter created is of type "Entity Collection"

Below is my javascript code on click of button:

function actionFunction()
{
	debugger;
	var contactId = Xrm.Page.data.entity.getId().slice(1, -1);
	var caseResults = getClosedCases(contactId);
	if(caseResults != null)
	{
		 var req = new XMLHttpRequest();
                req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts(" +contactId+ ")/Microsoft.Dynamics.CRM.new_Action", false);
                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;
                        debugger;
                        if (this.status === 200) {
				
                            var results = JSON.parse(this.response);
                        } else {
                            Xrm.Utility.alertDialog(this.statusText);
                        }
                    }
                };
                req.send(JSON.stringify(caseResults)); error is present here
	}
}


function getClosedCases(contactId)
{
	var results = null;
    var req = new XMLHttpRequest();
	req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents?$select=statecode,statuscode&$filter=_contactid_value eq "+contactId+" and  statecode eq 1", false);
	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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
	req.onreadystatechange = function() {
		if (this.readyState === 4) {
			req.onreadystatechange = null;
			if (this.status === 200) {
				results = JSON.parse(this.response);
				for (var i = 0; i < results.value.length; i++) {
					var statecode = results.value[i]["statecode"];
					var statecode_formatted = results.value[i]["statecode@OData.Community.Display.V1.FormattedValue"];
					var statuscode = results.value[i]["statuscode"];
				}
				
			} else {
				Xrm.Utility.alertDialog(this.statusText);
			}
		}
	};
	req.send();
	return results; // how will i pass this in action paramater 
}

Query: How will i pass "results" (closed cases list) to action parameter. 

Any help would be appreciated.

Regards,

Rahul

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
  • Rahul G J Profile Picture
    605 on at

    I Already saw this post, i wanted to know exactly in my scenario how it behaves. because i am not hard coding any value. i wanted to generalize this across all environments.

  • Verified answer
    Shaminderpal Singh Profile Picture
    1,565 on at

    Hi,

    Try below , replace the highlighted part with your action parameter key

    function actionFunction() {
    debugger;
    var contactId = Xrm.Page.data.entity.getId().slice(1, -1);
    var caseResults = getClosedCases(contactId);
    if (caseResults != null) {
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts(" + contactId + ")/Microsoft.Dynamics.CRM.new_Action", false);
    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;
    debugger;
    if (this.status === 200) {

    var results = JSON.parse(this.response);
    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send(JSON.stringify(caseResults));
    }
    }


    function getClosedCases(contactId) {
    var results = {};
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents?$select=statecode,statuscode&$filter=_contactid_value eq " + contactId + " and statecode eq 1", false);
    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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var data = JSON.parse(this.response).value;
    //for (var i = 0; i < results.value.length; i++) {
    // var statecode = results.value[i]["statecode"];
    // var statecode_formatted = results.value[i]["statecode@OData.Community.Display.V1.FormattedValue"];
    // var statuscode = results.value[i]["statuscode"];
    //}
    var records = [];
    data.forEach(function (elem, ind) {
    var recordObj = {};
    recordObj["@odata.type"] = "Microsoft.Dynamics.CRM.incident"
    Object.keys(elem).forEach(function (objElmKey, ObjElmInd) {
    if (objElmKey !== "@odata.etag") {
    recordObj[objElmKey] = elem[objElmKey];
    }
    });
    records.push(recordObj);
    });
    results = {
    // Here Replace Cases with Action Name Parameter
    Cases: records

    };


    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();
    return results; // how will i pass this in action paramater
    }

    -Shaminder

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    What exactly is not clear for you? How to convert your results array to parameter passed?

  • Rahul G J Profile Picture
    605 on at

    Yes andrew, i am getting an error

  • Rahul G J Profile Picture
    605 on at

    Hi Shaminder,

    thanks for your code, in the request i can see data but once the below line of code gets executed then, the response seems to be empty.

    req.send(JSON.stringify(caseResults));

    Query:

    1. Status is 204

    2. this.Response is empty

    regards,

    Rahul  

  • Suggested answer
    Shaminderpal Singh Profile Picture
    1,565 on at

    You need to provide your parameter name below which is of type entitycollection in action, so if my param name is "A' in action I will write like below

    results = {

    A:records

    };

  • Rahul G J Profile Picture
    605 on at

    Hi Shaminder,

    thanks for your code, there is no error now but in the request i can see data but once the below line of code gets executed then, the response seems to be empty.

    req.send(JSON.stringify(caseResults));

    Query:

    1. Status is 204

    2. this.Response is empty

    regards,

    Rahul  

  • Suggested answer
    Santosh bhagat Profile Picture
    464 on at

    Hi, 

    1. Create action based on contact entity and call this from Java script with your contact id.

    2. In action call custom workflow to get case list as output parameter and call your second action eith custom workflow output parameter. 

  • Suggested answer
    Shaminderpal Singh Profile Picture
    1,565 on at

    Hi Rahul,

    204 is perfectly fine and is a success response which you got from your action after successful operation. I assume your action is not returning any output parameter  that's the reason we got 204 no content, if you want a specific response return an output parameter and it will get you 200 success code.

    -Shaminder

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans