Code Sample for a Custom Button That Creates a Child Record

This following code sample creates a custom button labeled Create Task on the Opportunity Detail page. When the button is clicked, the REST API is used to create a child Activity record. In the createTask() function, you must replace <PodURL> with the URL for your pod. The Restful Services Integration privilege is required to send REST requests.

function createHttpRequest (httpmethod)

{

	var xmlhttpRequest = null;
	xmlhttpRequest = new XMLHttpRequest();
	if (typeof xmlhttpRequest.overrideMimeType != 'undefined')

	{

		xmlhttpRequest.overrideMimeType('application/json');
	}

	return xmlhttpRequest;

}


	oraclecrmod.onReady(function()
	{
		// when on the Opportunity Detail page
		if(oraclecrmod.ctx.object == "Opportunity" && oraclecrmod.ctx.isDetailPage())
		{

			function createTask()
			{

				var id = oraclecrmod.getField("Id").getValue();
				var insertPayload = "{\"Activities\":[ \
				{\
					\"Activity\": \"Appointment\", \
					\"Subject\": \"Opp Auto Appointment\", \
					\"Type\": \"Other\", \
					\"Location\": \"Markham\", \
					\"OpportunityId\":\"" + id + "\" \
				}\
				]}"


				//Calls REST API to create Task
				var url = "<PodURL>/OnDemand/user/Rest/026/Activities";
				var req = createHttpRequest ("POST");
				req.open('POST', url, false);
				req.setRequestHeader("Content-Type","application/
				vnd.oracle.adf.resource+json");
				req.send(insertPayload);
				alert(req.responseText);
				//success
				if (req.readyState == 4 && req.status == 201)
				{

					alert("success");

				}
		}
		// get the title bar
		titleBar = oraclecrmod.getTitleBar("OpportunityFormTB");
		// create the new button
		button = oraclecrmod.createButton({id:"CreateTaskButton", text:"Create Task",parent:titleBar});
		// associate the createTask function with the button click event button.on("click", createTask);

		}

	}

);