Friday, January 6, 2012

calculation of duration between two days in crm2011

to calculate duration

// get both dates from the form....
function Testing()
{
var start = Xrm.Page.getAttribute("new_staredate").getValue();
var end = Xrm.Page.getAttribute("new_enddate").getValue();

// to cancel the error when undefined
if (start==undefined || start==null || end==undefined || end==null) {
return;
}

// Get 1 day
var day=1000*60*60*24;
// Get time in days...
var time = ((end-start)/day)+1;

// Can't be less than 0.
if (time<0) time=0;
// Set the duration
Xrm.Page.getAttribute("new_noofdays").setValue(time);
}

Reference :-
Gangs036-MS-CRM-Blogspot: calculation of duration between two days in crm201...

calculation of duration between two days in crm4.0 using javascript


// to calculate duration

// get both dates from the form....
var start = crmForm.all.activeon.DataValue;
var end = crmForm.all.expireson.DataValue;

// to cancel the error when undefined
if (start==undefined || start==null || end==undefined || end==null) {
return;
}

// Get 1 day
var day=1000*60*60*24;
// Get time in days...
var time = ((end-start)/day)+1;

// Can't be less than 0.
if (time<0) time=0;
// Set the duration
crmForm.all.new_duration1.DataValue = time;

Reference :-
Gangs036-MS-CRM-Blogspot: calculation of duration between two days in crm4.0...:

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


Tuesday, October 25, 2011

Renaming the attached file in CRM

Now i am trying to rename attaching file to a certain "name" and wen i am going through the microsoft forum through my i have got two links which will be help full for achieving this and here is that links

Link 1
Link 2