среда, 29 августа 2012 г.

Получение наименования подразделения возможной сделки

Задача: Необходимо получить наименование подразделения возможной сделки.
Решение: Реализовано через 2 SOAP-запроса.

var oAttribute = crmForm.ObjectId;
if (oAttribute!= null)
{

sEntityName = 'opportunity';
GUID = oAttribute;
sAttributeName = 'owningbusinessunit';

var authenticationHeader = GenerateAuthenticationHeader();

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;

if (errorCount == 0)
{
if (resultXml.selectSingleNode("//q1:" + sAttributeName) != null)
{
var owningbusinessunit = resultXml.selectSingleNode("//q1:" + sAttributeName).nodeTypedValue;

sEntityName = 'businessunit';
GUID = owningbusinessunit;
sAttributeName = 'name';

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;

if (errorCount == 0)
{
if (resultXml.selectSingleNode("//q1:" + sAttributeName) != null)
{
var owningbusinessunitname = resultXml.selectSingleNode("//q1:" + sAttributeName).nodeTypedValue;
alert(owningbusinessunitname);

}
}
}
}
}

понедельник, 6 августа 2012 г.

Пример SOAP-запроса

Задача: Заполнить атрибут формы значением атрибута связанной сущности.

Решение: написан код на JScript

var oAttribute = crmForm.all.campaignid;
if (oAttribute.DataValue != null && oAttribute.DataValue[0] != null)
{

sEntityName = 'campaign';
GUID = oAttribute.DataValue[0].id;
sAttributeName = 'new_sourceid';

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;

if (errorCount == 0)
{
if (resultXml.selectSingleNode("//q1:" + sAttributeName) != null)
{
var lookupData = new Array();
var lookupItem = new Object();
var id;
lookupItem.id = resultXml.selectSingleNode("//q1:" + sAttributeName).nodeTypedValue;
lookupItem.typename = 'new_source';
var customername = resultXml.selectSingleNode("//q1:" + sAttributeName).getAttribute("name");

lookupItem.name = customername;
lookupData[0] = lookupItem;
crmForm.all.new_sourceid.DataValue = lookupData;
}
}
}

Скрытие кнопок стандартной командной панели формы

Задача: Реализовать код на JScript, который позволяет скрыть кнопки стандартной командной панели формы.

Решение: http://mscrm4ever.blogspot.com/2008/08/show-hide-crm-form-toolbar-buttons.html

пятница, 3 августа 2012 г.

Плагин для проверки возможности удаления сущности

Задача: Необходимо реализовать плагин, который запрещает удаление сущности при выполнении некоторого условия на связанную сущность.

Решение: Реализован плагин на событие Delete сущности new_grmember со снимком PreImage.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;

namespace PreDeleteGrmember
{
    public class GrmemberDeleteHandler : IPlugin
    {
        #region Члены IPlugin

        public void Execute(IPluginExecutionContext context)
        {
            if (context.MessageName != MessageName.Delete || !(context.PreEntityImages.Contains("PreImage")) || !(context.PreEntityImages["PreImage"] is DynamicEntity))
                return;

            DynamicEntity _grmember = (DynamicEntity)context.PreEntityImages["PreImage"];

            if (!_grmember.Properties.Contains("new_contractid"))
                return;

            Lookup customer = (Lookup)_grmember["new_contractid"];

            if (customer.type != EntityName.opportunity.ToString())
                return;

            Guid opportunityid = customer.Value;

            string[] fields = new string[] { "new_status" };

            ICrmService crmservice = context.CreateCrmService(true);

            RetrieveRequest request = new RetrieveRequest();
            request.ColumnSet = new ColumnSet(fields);
            request.ReturnDynamicEntities = true;

            TargetRetrieveDynamic target = new TargetRetrieveDynamic();
            target.EntityId = opportunityid;
            target.EntityName = EntityName.opportunity.ToString();

            request.Target = target;

            DynamicEntity _opportunity = (DynamicEntity)((RetrieveResponse)crmservice.Execute(request)).BusinessEntity;

            Picklist status = (Picklist)_opportunity["new_status"];

            if (status.Value.ToString() == "2")
                throw new InvalidPluginExecutionException("Вы не можете удалить этот объект!");

        }

        #endregion
    }
}