Saturday, November 5, 2011

JavaScript for Disabling a field once its contains a value in crm 2011

 Write this javascript in onload event of a form

     if( Xrm.Page.data.entity.attributes.get('new_consultant').getValue() !=null)
     {
         var AddressType = Xrm.Page.ui.controls.get('new_consultant');
         AddressType.setDisabled(true);
     }

Thursday, November 3, 2011

Javascript to validate to Phone No in CRM 2011

function checkisinteger()
{
  var s=Xrm.Page.getAttribute('telephone1').getValue();
  if(s!=null)
  {  
    var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
       if (((c < "0") || (c > "9"))) break;
    }
    if(i    {
     alert("Enter a valid phone number");
     Xrm.Page.getAttribute('telephone1').setValue("");
    }
 }
}

Retrieve related entity data (CRM 2011)



Team & users are N:N related. I might need to right 
long LinkEntity query to retrieve users for particular
team. Guess what, I can do it using RelatedEntitiesQuery
property of RetrieveRequest .

QueryExpression query = new QueryExpression(); 
query.EntityName = "systemuser";
 query.ColumnSet = new ColumnSet("systemuserid"); 
Relationship relationship = new Relationship();
 query.Criteria = new FilterExpression();
 query.Criteria.AddCondition(new ConditionExpression("isdisabled",
ConditionOperator.Equal, false)); // name of relationship between team & systemuser relationship.
SchemaName = "teammembership_association"; RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection(); relatedEntity.Add(relationship, query);
RetrieveRequest request = new RetrieveRequest(); request.RelatedEntitiesQuery = relatedEntity; 
request.ColumnSet = new ColumnSet("teamid"); 
request.Target = new EntityReference { Id = teamId, LogicalName = "team" }; RetrieveResponse response = (RetrieveResponse)service.Execute(request);


if (((DataCollection)
(((RelatedEntityCollection)(response.Entity.RelatedEntities)))
).Contains(new Relationship("teammembership_association")) &&
((DataCollection)(((RelatedEntityCollection)
(response.Entity.RelatedEntities))
))[new Relationship("teammembership_association")
].Entities.Count > 0) 
return true; 
else return false;

Renaming the attached file CRM 2011


    I had a requirement in crm 2011 to rename the file what ever we are attaching should be renamed to be a specific name i have achieved this through a plugin  here the code for that plugin

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.IO;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;


namespace FileRenaming
{
    public class filerename : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parmameters.
                Entity entity = (Entity)context.InputParameters["Target"];
                try
                {
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                    entity["filename"] = "Crm" + "test" + ".doc";                   
                    service.Update(entity);
                }
                catch (InvalidPluginExecutionException)
                {
                    throw new InvalidPluginExecutionException("Error Occured");
                }
            }
        }
    }
}

register this plugin as post synchronous event and it sould be in create message for the primary entity as
annotation