Starting with this great article, I’ve created a JavaScript Library with several useful functions & methods for CRM 2011, such as:
– CRM Service class to Create, Update, Retrieve, RetrieveMultiple, Delete records in CRM (SOAP based);
– Custom Runtime Advanced Filtered Lookup support;
– Set Lookup Value programmatically support
– Guid Generator
– CRM Form Buttons for CRM 4.0 & CRM 2011
– Date Format prototype
– Dynamic create support for Notes
– Array Contains prototype
– String EndsWith prototype
– Check if the logged-in user has a certain role
– Automatic Dialogs (launch in runtime) support
– much more to come…
You can download the full library here.
Also, here are some examples of how to use it:
// CWS.CRM.Utils.js Examples
// Gets all the offers associated with an opportunity
function GetOffersByOpportunity(opportunityId)
{
try {
if(opportunityId != null)
{
var entityName = 'quote';
var outputColumns = [new CRMField('name'), new CRMField('quoteid'), new CRMField('closedon')];
var filters = [new FilterBy('opportunityid', LogicalOperator.Equal, opportunityId)];
var items = RetrieveRecords(entityName, outputColumns, filters);
if(items != null)
return items;
}
}
catch(err) {
}
return null;
}
// Gets all the offer products associated with an offer
function GetOfferProductsByOffer(offerId)
{
try {
if(offerId != null)
{
var entityName = 'quotedetail';
var outputColumns = [new CRMField('quotedetailid'), new CRMField('new_invoicingdate'), new CRMField('productid')];
var filters = [new FilterBy('quoteid', LogicalOperator.Equal, offerId)];
var items = RetrieveRecords(entityName, outputColumns, filters);
if(items != null)
return items;
}
}
catch(err) {
}
return null;
}
// Updates certain fields of the offer product
function UpdateOfferProductInfo(offerProductId, newInvoicingDate)
{
try {
if(offerProductId != null)
{
var entityName = 'quotedetail';
var fields = [new CRMField('new_invoicingdate', newInvoicingDate.toCRMFormat())];
return UpdateRecord(entityName, offerProductId, fields);
}
}
catch(err) {
}
}
// Copies the opportunity close date to all associated offers
function ComputeOffersCloseDate()
{
var closeDate = new Date(Xrm.Page.getAttribute("actualclosedate").getValue());
if(closeDate != null)
{
try {
var opportunityId = Xrm.Page.data.entity.getId();
if(opportunityId != null)
{
var offers = GetOffersByOpportunity(opportunityId);
if(offers != null)
{
var entityName = 'quote';
var fields = [new CRMField('closedon', closeDate.toCRMFormat())];
for(var i=0; i < offers.Rows.length; i++)
{
var offerId = offers.Rows[i].GetValue("quoteid");
if(offerId != null)
{
var entityName = 'quote';
var fields = [new CRMField('closedon', closeDate.toCRMFormat())];
var updated = UpdateRecord(entityName, offerId, fields);
}
}
}
}
}
catch(err) {
}
}
}
// Generates supplier order products, for a certain order - supplier combination
function GenerateSupplierOrderProducts(orderId, productId, vendorId)
{
if(orderId != null && productId != null && vendorId != null)
{
try {
var fields = [new CRMField('new_supplierorderid', orderId), new CRMField('new_productid', productId), new CRMField('new_supplierid', vendorId)];
CreateRecord('new_supplierorderproduct', fields);
}
catch(err) {
}
}
}
Thanks for your library, it’s very useful
The download link isn’t working… Would love to try this out.
Hello Sarah,
The link has been fixed. Please try again.
Thanks for the tip.
Thanks for your library…. Its greate!
Thank you!
How do I parse the value of items.
It’s fantastic to develop in CRM 2011 🙂
But… How can I delete records? I don’t find the function in your library 🙁
Thanks a lot!
We’ll add that too at some point. In the meantime, you can download the CRM 2011 SDK and take a look at their samples (SOAP & JavaScript)
Thanks 🙂
Just stumbled across your library here. Very nice!
Great library. Would be even better if accompanied with more examples and documentation.
I have a question – I can retrieve a date value from a record but can’t populate a date field on a form with this date. Can you help?
Date returned is in the format of “yyyy-mm-ddThh:MM:ss+” – I need to convert this to a Date object so I can populate the field with Xrm.Page.getAttribute(“datefieldname”).setValue(<>);
Thanks.
Hi have you an sample show to reference and setup your libary, I not an developer and it seems that I am missing some knowlege basics as I didn´t understand how to use your gread libary
by teh way is the libary also working with crm 2011 ifd/adfs and CRM Online?
You must add the LOGICAL_OPERATOR_AND constant in your js file.
LinkedEntity method throw exception with this undefined constant. You can replace it with LogicalOperator.And
How do you read the field values in items??
How do we read the fields and rows?
It’s actually a nice and useful piece of info. I am glad that you shared this useful info with us. Please stay us informed like this. Thank you for sharing.
Hi there,
The download link is not working. But I had another question. Has this code been updated for the latest version of CRM. So CRM2015 or better still CRM2016?
Hello,
No, the library hasn’t been updated for later versions. Most of its functionalities are covered by the SDK.REST js library (found within the samples of the SDK).