sábado, 7 de abril de 2012

xrmservicetoolkit: una gran librería de Javascript para CRM 2011

Periódicamente hago unas búsquedas en CodePlex para encontrar cosas nuevas, herramientas o ideas que puedan ser de utilidad.
Hoy he encontrado un nuevo proyecto publicado por Qiming Jaimie Ji (http://jaimiescode.blogspot.com.es/).
Se trata de una librearía de Javascript (básicamente, un fichero .js) que puede ser descargado de aquí:
http://xrmservicetoolkit.codeplex.com/

Lo bueno de esta librería es que me ha gustado mucho en "enfoque" que le dan, además de agradecer mucho que esta todo súper comentado y explicado.

La clase "XrmServiceToolkit", contiene a su vez 3 clases:
  • .Common: contiene métodos comunes en el trabajo con javascript en CRM 2011, por ejemplo habilitar, ocultar,  cambios de requeridos/opciones de campos, etc.
  • .Rest: métodos CRUD utilizando el servicio REST (OrganizationData.svc)
  • .Soap: métodos CRUS utilizando el servicio SOAP de CRM 2011
Ejemplo de utilización (y comparativa entre REST y SOAP):
test("Test XrmServiceToolkit.Soap.Create() method to create a CRM record (contact)", function () {

            var createContact = new XrmServiceToolkit.Soap.BusinessEntity("contact");
            createContact.attributes["firstname"] = "Diane";
            createContact.attributes["lastname"] = "Morgan";
            createContact.attributes["middlename"] = "<&>";   // Deliberate special characters to ensure that the toolkit can handle special characters correctly.
            createContact.attributes["gendercode"] = { value: 2, type: "OptionSetValue" };
            createContact.attributes["familystatuscode"] = { value: 1, type: "OptionSetValue" }; // Picklist : Single - 1
            createContact.attributes["creditlimit"] = { value: 2, type: "Money" };
            createContact.attributes["birthdate"] = birthDate;
            createContact.attributes["donotemail"] = true;
            createContact.attributes["donotphone"] = false;
            createContact.attributes["parentcustomerid"] = { id: accountId, logicalName: "account", type: "EntityReference" };

            contactId = XrmServiceToolkit.Soap.Create(createContact);

            ok(guidExpr.test(contactId), "Creating a contact should returned the new record's ID in GUID format. ");

});
test("Test XrmServiceToolkit.Rest.Create() method to create a new record", function () {

            var account = {};
            account.Name = "Test Account Name";
            account.Description = "This account was created by the XrmServiceToolkit.Rest.Create() sample.";
            if (contactId != null) {
                //Set a lookup value
                account.PrimaryContactId = { Id: contactId, LogicalName: "contact" };
            }
            //Set a picklist value
            account.PreferredContactMethodCode = { Value: 2 }; //E-mail

            //Set a money value
            account.Revenue = { Value: "2000000.00" }; //Set Annual Revenue

            //Set a Boolean value
            account.DoNotPhone = true; //Do Not Allow

            //Add Two Tasks
            var today = new Date();
            var startDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 3); //Set a date three days in the future.

            var LowPriTask = { Subject: "Low Priority Task", ScheduledStart: startDate, PriorityCode: { Value: 0} }; //Low Priority Task
            var HighPriTask = { Subject: "High Priority Task", ScheduledStart: startDate, PriorityCode: { Value: 2} }; //High Priority Task
            account.Account_Tasks = [LowPriTask, HighPriTask];
            XrmServiceToolkit.Rest.Create(
                account,
                "AccountSet",
                function (result) {
                    accountId = result.AccountId;
                    ok(guidExpr.test(result.AccountId), "Creating a account should returned the new record's ID in GUID format. ");
                },
                function (error) {
                    equal(true, false, error.message);
                },
                false
            );
});

La publico porque me parece que tiene una forma súper sencilla de utilizar, y que es de muy fácil evolución.

un saludo,

1 comentario: