Useful JavaScript functions & methods for CRM 2011 – CWS.CRM.Utils.js Library

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) {
		}
	}
}

19 comments

  1. 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!

  2. 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.

  3. 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

  4. 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

  5. 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?

    1. 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).

Leave a Reply to jj Cancel reply

Your email address will not be published. Required fields are marked *