Цитата:
Сообщение от
datfi
Добрый день, коллеги! У меня вопрос вот какого плана:
В сущности продукт есть поле артикул. Оно при заполнении проверяется на уникальность. Есть ли возможность сделать так же для поля, например Код организации?
Вешаете следующий код на OnSave()
X++:
function GetAttributeValueFromID(sEntityName, GUID, sAttributeName)
{
var authenticationHeader = GenerateAuthenticationHeader();
// Prepare the SOAP message.
var xml = "";
xml = 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>"+
"<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<entityName>"+sEntityName+"</entityName>"+
"<id>"+GUID+"</id>"+
"<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
"<q1:Attributes>"+
"<q1:Attribute>"+sAttributeName+"</q1:Attribute>"+
"</q1:Attributes>"+
"</columnSet>"+
"</Retrieve>"+
"</soap:Body>"+
"</soap:Envelope>";
// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
// Capture the result.
var resultXml = xHReq.responseXML;
// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
var result = resultXml.selectSingleNode("//q1:" + sAttributeName).nodeTypedValue;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
result = " ";
alert(msg);
}
// Display the retrieved value.
else
{
return result;
}
}
var resultXml;
var result;
var xml;
var okpo = "";
ObjectID = crmForm.ObjectId;
if(crmForm.all.NEW_OKPO.DataValue != null)
{okpo = crmForm.all.NEW_OKPO.DataValue;}
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\">" +
" <soap:Body>" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\" xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\">" +
" <q1:EntityName>account</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>NEW_OKPO</q1:Attribute>" +
" <q1:Attribute>accountid</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Distinct>false</q1:Distinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>NEW_OKPO</q1:AttributeName>" +
" <q1:Operator>Equal</q1:Operator>" +
" <q1:Values>" +
" <q1:Value xsi:type=\"xsd:string\">"+okpo+"</q1:Value>" +
" </q1:Values>" +
" </q1:Condition>";
if (ObjectID != null)
{
xml += " <q1:Condition>" +
" <q1:AttributeName>accountid</q1:AttributeName>" +
" <q1:Operator>NotEqual</q1:Operator>" +
" <q1:Values>" +
" <q1:Value xsi:type=\"xsd:string\">"+ObjectID+"</q1:Value>" +
" </q1:Values>" +
" </q1:Condition>";
};
xml += " <q1:Condition>" +
" <q1:AttributeName>statecode</q1:AttributeName>" +
" <q1:Operator>Equal</q1:Operator>" +
" <q1:Values>" +
" <q1:Value xsi:type=\"xsd:int\">0</q1:Value>" +
" </q1:Values>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2006/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2006/WebServices/RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
result = ReadXml(xmlHttpRequest.responseXML);
try{
if(isdefined(result.Envelope.Body.RetrieveMultipleResult.BusinessEntities.BusinessEntity)&& crmForm.all.parentaccountid.DataValue==null)
{
var accountid = resultXml.selectSingleNode("//q1:accountid").nodeTypedValue;
//alert(result1);
var accountname = GetAttributeValueFromID("account",accountid,"name");
var userid = GetAttributeValueFromID("account",accountid,"ownerid");
var userlastname = GetAttributeValueFromID("systemuser",userid,"lastname");
//var userfirstname = GetAttributeValueFromID("systemuser",userid,"firstname");
alert("Такой код ОКПО уже есть у предприятия: " + accountname +'\n'+ "Ответственный менеджер: " + userlastname +'\n'+ "Обратитесь к Вашему руководителю с запросом на открытие соответствующей бизнес-линии на этом предприятии для Вас");
event.returnValue = false;
return false;
}
}
catch(e){};
return true;
где NEW_OKPO -это поле для уникального ОКПО организации в моем случае
плюс, здесь проверка на то, что если есть родительская организация, то проверка не срабатывает, сможете модифицировать под себя