martes, 9 de marzo de 2010

Botón en un formulario que ejecuta un Workflow


Lo primero de todo es crear un atributo personalizado en la entidad del formulario donde queremos poner el botón. El atributo tiene que ser de tipo nvarchar (texto) y establecer la propiedad Búsqueda en No.
Publicamos el cambio y creamos el workflow (tiene que ser a petición) que vamos a lanzar cuando se pulse el botón.

Editamos el evento onload del formulario de dicha entidad y este código es el que crea (transforma el atributo) en un botón:
function CrearBoton(fieldname, buttontext, buttonwidth,clickevent)
{
functiontocall=clickevent;
crmForm.all.new_button.DataValue = buttontext;
crmForm.all.new_button.style.borderRight="#3366cc 1px solid";
crmForm.all.new_button.style.paddingRight="5px";
crmForm.all.new_button.style.borderTop="#3366cc 1px solid";
crmForm.all.new_button.style.paddingLeft="5px";
crmForm.all.new_button.style.fontSize="11px";
crmForm.all.new_button.style.backgroundImage="url(/_imgs/btn_rest.gif)";
crmForm.all.new_button.style.borderLeft="#3366cc 1px solid";
crmForm.all.new_button.style.width=buttonwidth;
crmForm.all.new_button.style.cursor="pointer";
crmForm.all.new_button.style.lineHeight="18px";
crmForm.all.new_button.style.borderBottom="#3366cc 1px solid";
crmForm.all.new_button.style.backgroundRepeat="repeat-x";
crmForm.all.new_button.style.fontFamily="Tahoma";
crmForm.all.new_button.style.height="20px";
crmForm.all.new_button.style.backgroundColor="#cee7ff";
crmForm.all.new_button.style.textAlign="center";
crmForm.all.new_button.style.overflow="hidden";
crmForm.all.new_button.attachEvent("onmousedown",push_button);
crmForm.all.new_button.attachEvent("onmouseup",release_button);
crmForm.all.new_button.attachEvent("onclick",testfunction);
}

function push_button(){
window.event.srcElement.style.marginLeft="1px";
window.event.srcElement.style.marginTop="1px";
}

function release_button(){
window.event.srcElement.style.marginLeft="0px";
window.event.srcElement.style.marginTop="0px";
}

Función que ejecutará el workflow:
ExecuteWorkflow = function(entityId, workflowId)
{
    var xml = "" + 
    "" + 
    "<envelope xmlns:soap="\"http://schemas.xmlsoap.org/soap/envelope/\"" xmlns:xsd="\"http://www.w3.org/2001/XMLSchema\"" xmlns:xsi="\"http://www.w3.org/2001/XMLSchema-instance\"">" + 
    GenerateAuthenticationHeader() +
    "  " + 
    "    <execute xmlns="\"http://schemas.microsoft.com/crm/2007/WebServices\"">" + 
    "      <request xsi:type="\"ExecuteWorkflowRequest\"">" + 
    "        <entityid>" + entityId + "</entityid>" + 
    "        <workflowid>" + workflowId + "</workflowid>" + 
    "      </request>" + 
    "    </execute>" + 
    "  " + 
    "</envelope>" + 
    "";

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Execute");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);
    var resultXml = xmlHttpRequest.responseXML;
    return(resultXml.xml);
}

Función del evento OnClick del botón que llamará al la que ejecuta el workflow pasándole el id del de la entidad y el del wf.
function testfunction()
{
   var theWorkflowId = "5F63397D-2868-4B74-9E6E-A5C6012981F3"; //change to your workflow Id
   ExecuteWorkflow(crmForm.ObjectId, theWorkflowId);
}


create_button_from_textattribute('new_button', 'email','184px',testfunction);

No hay comentarios: