AXForum  
Вернуться   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 05.09.2011, 18:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,640 / 848 (80) +++++++
Регистрация: 28.10.2006
Gareth Tucker: Quick Create Contact from the CRM Case Form
Источник: http://gtcrm.wordpress.com/2011/09/0...crm-case-form/
==============

In a previous post I demonstrated a screen customisation that allows quick creation of a new Contact directly on the Phone Call form.   Today I needed the same thing on the Case form so thought I would share that code here as well.

Here’s what my screen looks like:



The “Quick Create New Citizen” fields are enabled so long as a “Citizen” has not yet been specified on the Case (note: I have renamed Contact to Citizen).   Once you key into one of the Quick Create fields the other Quick Create fields become mandatory.  Once they are all populated a new Citizen/Contact is created and the Case is updated to link to that record.

Here’s the java script:

function SetLookupValue(fieldName, id, name, entityType) {

if (fieldName != null) {

var lookupValue = new Array();

lookupValue[0] = new Object();

lookupValue[0].id = id;

lookupValue[0].name = name;

lookupValue[0].entityType = entityType;

Xrm.Page.getAttribute(fieldName).setValue(lookupValue);

}

}

 

function DisableFields() {

Xrm.Page.ui.controls.get("new_firstname").setDisabled(true);

Xrm.Page.ui.controls.get("new_lastname").setDisabled(true);

Xrm.Page.ui.controls.get("new_phonenumber").setDisabled(true);

}

 

function MakeFieldsMandatory() {

Xrm.Page.data.entity.attributes.get("new_firstname").setRequiredLevel("required");

Xrm.Page.data.entity.attributes.get("new_lastname").setRequiredLevel("required");

Xrm.Page.data.entity.attributes.get("new_phonenumber").setRequiredLevel("required");

}

 

function MakeFieldsNonMandatory() {

Xrm.Page.data.entity.attributes.get("new_firstname").setRequiredLevel("none");

Xrm.Page.data.entity.attributes.get("new_lastname").setRequiredLevel("none");

Xrm.Page.data.entity.attributes.get("new_phonenumber").setRequiredLevel("none");

}

 

function OnLoad() {

if (Xrm.Page.ui.getFormType() == 1) {

}

else if (Xrm.Page.ui.getFormType() != 1) {

DisableFields();

}

}

 

function NewContact() {

if (

Xrm.Page.getAttribute("new_firstname").getValue() == null &&

Xrm.Page.getAttribute("new_lastname").getValue() == null &&

Xrm.Page.getAttribute("new_phonenumber").getValue() == null) {

MakeFieldsNonMandatory();

}

else if (

Xrm.Page.getAttribute("new_firstname").getValue() != null &&

Xrm.Page.getAttribute("new_lastname").getValue() != null &&

Xrm.Page.getAttribute("new_phonenumber").getValue() != null &&

Xrm.Page.data.entity.attributes.get("customerid").getValue() == null) {

CreateContact();

Xrm.Page.getAttribute("new_phonenumber2").setValue(Xrm.Page.getAttribute("new_phonenumber").getValue());

DisableFields();

}

else {

MakeFieldsMandatory();

//Xrm.Page.ui.controls.get("from").setVisible(false);

}

}

 

function CreateContact() {

// Get the CRM URL

var serverUrl = Xrm.Page.context.getServerUrl();

 

// Cater for URL differences between on premise and online

if (serverUrl.match(/\/$/)) {

serverUrl = serverUrl.substring(0, serverUrl.length - 1);

}

 

// Specify the ODATA end point (this is the same for all CRM 2011 implementations)

var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";

 

// Specify the ODATA entity collection

var ODATA_EntityCollection = "/ContactSet";

 

// Define an object for the CRM record you want created

var CRMObject = new Object();

 

// Define attribute values for the CRM object

CRMObject.FirstName = Xrm.Page.getAttribute("new_firstname").getValue();

CRMObject.LastName = Xrm.Page.getAttribute("new_lastname").getValue();

CRMObject.Telephone1 = Xrm.Page.getAttribute("new_phonenumber").getValue();

 

//Parse the entity object into JSON

var jsonEntity = window.JSON.stringify(CRMObject);

 

//Asynchronous AJAX function to Create a CRM record using OData

$.ajax({

type: "POST",

contentType: "application/json; charset=utf-8",

datatype: "json",

url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection,

data: jsonEntity,

beforeSend: function (XMLHttpRequest) {

//Specifying this header ensures that the results will be returned as JSON.

XMLHttpRequest.setRequestHeader("Accept", "application/json");

},

success: function (data, textStatus, XmlHttpRequest) {

//This function will trigger asynchronously if the Retrieve was successful

//alert("ajax call successful");

var NewCRMRecordCreated = data["d"];

var FullName = Xrm.Page.getAttribute("new_firstname").getValue() + " " + Xrm.Page.getAttribute("new_lastname").getValue();

SetLookupValue("customerid", NewCRMRecordCreated.ContactId, FullName, "contact");

},

error: function (XmlHttpRequest, textStatus, errorThrown) {

//This function will trigger asynchronously if the Retrieve returned an error

alert("ajax call failed");

}

});

}
The new fields are called new_firstname, new_lastname and new_phonenumber.

You’ll need to add the following libraries and event handler to the Case form’s OnLoad event (I provide a download link to these at the end of this post):



And you will need this event handler added to each of the Quick Create field’s OnChange events:



The 4 web resources employed are available here.

Cheers,

Gareth.




Источник: http://gtcrm.wordpress.com/2011/09/0...crm-case-form/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
Gareth Tucker: Quick Create Contact from the Phone Call Form Blog bot Dynamics CRM: Blogs 0 23.08.2011 12:11
Gareth Tucker: Building a Microsoft CRM 2011 VM – A Quick Checklist Blog bot Dynamics CRM: Blogs 0 03.08.2011 09:11
CRM DE LA CREME! Some more useful javascripts for MS CRM Blog bot Dynamics CRM: Blogs 0 04.05.2010 11:05
CRM DE LA CREME! Configuring Microsoft Dynamics CRM 4.0 for Internet-facing deployment Blog bot Dynamics CRM: Blogs 0 18.08.2009 11:05
Microsoft Dynamics CRM Team Blog: CRM Online: ‘Contact Us’ Web Form Made Easy Blog bot Dynamics CRM: Blogs 0 20.05.2009 05:05

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 15:08.