Friday, January 6, 2012

Canceling the Save operation in CRM 2011



Method 1:

Add an OnSave event to the form:


Note: The pass execution context as first parameter checkbox is very important to this process. Without it, it will not work.

Inside the Contact_main_library.js web resource, we’ll add the following function:
function Form_onsave(executionObj)
{
var shouldSave = true;

if (shouldSave)
{
alert("Unable to save because of some reason or the other.");

executionObj.getEventArgs().preventDefault();
}
}
Inside the OnSave function, we need to have some type of test to see if we should allow the record to be saved.
For this demonstration, we simply check the value of a variable called shouldSave. If true, then we display an error message to the user then execute the preventDefault() method which will instruct CRM to cancel the save operation.

Method 2:
Add an OnSave event to the form:
//if event.Mode is one means Save, event .Mode is two means Save&Close.
if(event.Mode == 1 || event.Mode ==2)
{
event.returnValue = false;
return false;
}

Reference :-
Gangs036-MS-CRM-Blogspot: Canceling the Save operation in CRM 2011

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