Posted in CRM, HTML, JavaScripts, OData Call

Passing WebResource Data to Update Form Field

Passing WebResource Data to Update CRM Form Field :

Here I am Passing the data  entered in a HtML webresource into a crm form field through WebApi(OData Call). Onclick of submit button in a html , It will updated in the crm form.

1

 

First Insert the webresource in a form >Customizations>Entity> Forms> Information >Insert > WebResource

2.JPG

 

Click on the Lokkup >AddNew > Select Type as HTML as follows

3.JPG

 

Click on  the Text Editor , Inside the Source paste the following code

<html><head><meta><meta><meta><meta></head><body style="word-wrap: break-word;">
<form>
 Enter name: <input type="text" name="fname" id="myText">
 <input type="button" value="Submit" onclick="myFunction()"> 
</form>

function myFunction() {
debugger;
var textdata = document.getElementById("myText").value;
//Directly update the crm field without Odata Call
//window.parent.opener.Xrm.Page.getAttribute("new_test1").setValue(textdata) ;
//window.parent.Xrm.Page.getAttribute("new_test1").setValue(textdata);

//or With OdataCall

// new_test1 Schema name of updated fields and new_customs entity SchemaName name
var entity = {};
entity.new_test1= textdata;
var entityname =window.parent.Xrm.Page.data.entity.getEntityName();
var RecordId= window.parent.Xrm.Page.data.entity.getId().replace("{","").replace("}","");

//OdataCall
var req = new XMLHttpRequest();
req.open("PATCH", window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.0/"+entityname+ "s("+RecordId + ")", 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;
 if (this.status === 204) {
 //Success - No Return Data - Do Something
 
 }
 else {
 alert(this.statusText);
 }
 }
};
req.send(JSON.stringify(entity));
window.parent.Xrm.Page.data.refresh();
}


</body></html>

while pasting the html codes to word press, it doesn’t taking the code at script. Please go through the image.

Screenshot (341)

Under RichText look like this

4.JPG

Click on OK > Save > PublishCustomizations

Thats it Will work.. Onclick of submit button data entered in HTML text , It will  Updated in crm.

Crming – Enjoy 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s