Friday, January 27, 2012

Create and attach TASK to a CASE on the click of a button

My aim is to create a task and attach the task to a case, on the click of a button.

Step 1 : Add the below two files as a web resource to your solution
json2.js
sdk.rest.js
(These two files can be found in the SDK, path : sdk\samplecode\js\restendpoint\javascriptrestdataoperations\javascriptrestdataoperations\scripts)

In my scenario i have named it sny_SDK.REST and sny_Jason2.

Step 2 : Create a javascript (case.js) file with the below code.

function CreateTaskFromCase()
{
//REMEMBER WE ADDED THIS IN THE PREVIOS STEP
load_script("/SONY/WebResources/sny_SDK.REST");
load_script("/SONY/WebResources/sny_Jason2");
debugger;
CreateTask();
}

function load_script(url) {
var x = new ActiveXObject("Msxml2.XMLHTTP");
x.open('GET', url, false);
x.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
x.send('');
eval(x.responseText);
var s = x.responseText.split(/\n/);
var r = /^function\s*([a-z_]+)/i;
for (var i = 0; i < s.length; i++) {
var m = r.exec(s[i]);
if (m != null)
window[m[1]] = eval(m[1]);
}
}

function CreateTask() {
var task = {};
task.Subject = "Followup Task";
task.Description = "This task was created by the JavaScriptRESTDataOperations sample.";
debugger;
task.RegardingObjectId = { Id: Xrm.Page.data.entity.getId(), LogicalName: "incident", Name: Xrm.Page.data.entity.attributes.get("title").getValue() }
//Add Two Tasks
var today = new Date();
var startDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 3); //Set a date three days in the future.
task.ScheduledStart = startDate;
task.PriorityCode = { Value: 0 };
SDK.REST.createRecord(task, "Task", successHandler, errorHandler);
}

function successHandler(entity) {
alert("Created record id is :" +entity.Title);
}

function errorHandler(error) {
debugger;
alert(error.message);
}

Step 3 : Add the above js file to your solution. I have named it as sny_Case


Step 4 : Create a button on the case entity using the 2011 Ribbon Editor (http://crmvisualribbonedit.codeplex.com/)

Call the function "CreateTaskFromCase" refering to the library "///WebResources/sny_Case.

Step 5 : Now open a case record an you can see the button. Click the button and navigate to the activities section to see the task.

No comments:

Post a Comment