Как создавать кнопку на CRM-форме рассказано в предыдущем посте, теперь рассмотрим как создать новую запись кликом по кнопке и используя javascript.
Создавать будем новый объект сущности Account.

Обработаем функцию TestTheButton(), которая отвечает за событие на клик кнопки:
///////создание и обработка кнопки
//Надпись на кнопке
crmForm.all.new_button.DataValue = «Button»;
crmForm.all.new_button.style.textAlign = «center»;
crmForm.all.new_button.vAlign = «middle»;
//стили
crmForm.all.new_button.style.cursor = «hand»;
crmForm.all.new_button.style.backgroundColor = «#CADFFC»;
crmForm.all.new_button.style.color = «#000000″;
crmForm.all.new_button.style.borderColor = «#330066″;
crmForm.all.new_button.style.fontWeight = «bold»;
crmForm.all.new_button.contentEditable = false;
//меняем цвет при наведении курсора
crmForm.all.new_button.attachEvent(«onmousedown»,changeC1);
crmForm.all.new_button.attachEvent(«onmouseup»,changeC2);
crmForm.all.new_button.attachEvent(«onmouseover»,changeC3);
crmForm.all.new_button.attachEvent(«onmouseleave»,changeC4);
function changeC1() {
crmForm.all.new_button.style.color = «000099″;
}
function changeC2() {
crmForm.all.new_button.style.color = «000000″;
}
function changeC3() {
crmForm.all.new_button.style.backgroundColor = «#6699FF»;
}
function changeC4() {
crmForm.all.new_button.style.backgroundColor = «CADFFC»;
}//при клике на кнопке вызываем функцию TestTheButton
crmForm.all.new_button.attachEvent(«onclick»,TestTheButton);function TestTheButton()
{
// Значения для новой Организацииvar name = «Account_Name»;
var accountnumber=»Acc1234567″;
var telephone1=»8-050-1234567″;var authenticationHeader = GenerateAuthenticationHeader();
// Подготовка SOAP сообщения
var xml = «<?xml version=’1.0′ encoding=’utf-8′?>» +
«<soap:Envelope xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/’»+
» xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance’»+
» xmlns:xsd=’http://www.w3.org/2001/XMLSchema’>»+
authenticationHeader+
«<soap:Body>»+
«<Create xmlns=’http://schemas.microsoft.com/crm/2007/WebServices’>»+
«<entity xsi:type=’account’>»+
«<name>»+name+»</name>»+
«<accountnumber>»+accountnumber+»</accountnumber>»+
«<telephone1>»+telephone1+»</telephone1>»+
«</entity>»+
«</Create>»+
«</soap:Body>»+
«</soap:Envelope>»;// Подготовка xmlHttpObject и отправка запроса
var xHReq = new ActiveXObject(«Msxml2.XMLHTTP»);
xHReq.Open(«POST», «/mscrmservices/2007/CrmService.asmx», false);
xHReq.setRequestHeader(«SOAPAction»,»http://schemas.microsoft.com/crm/2007/WebServices/Create»);
xHReq.setRequestHeader(«Content-Type», «text/xml; charset=utf-8″);
xHReq.setRequestHeader(«Content-Length», xml.length);
xHReq.send(xml);
// Результат
var resultXml = xHReq.responseXML;// Проверим на ошибки
var errorCount = resultXml.selectNodes(‘//error’).length;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode(‘//description’).nodeTypedValue;
alert(msg);
}
// Если нет ошибок, то открываем созданную запись
else
{
var accountid = resultXml.selectSingleNode(«//CreateResult»);
window.open(«/sfa/accts/edit.aspx?id={«+accountid.nodeTypedValue+»}»);
}
}
