Category Archives: MS CRM 4.0

Create Opportunity Relationship record

Sometime we need to create “Opportunity Relationship” record through code. We can use below code to create Opportunity relationship record. Basically we need to set three values while creating opportunity relationship record customerid, opportunityid and opportunityroleid.

//I have used Dynamic entity to create opportunity relationship record

DynamicEntity  _OpportunityRelationship = new DynamicEntity();

//Set entity name as “customeropportunityrole”         

 _OpportunityRelationship.Name = “customeropportunityrole”;

//Create property to set customer 

CustomerProperty _OpportunityCustomer=new CustomerProperty();

  _ OpportunityCustomer.Name=”customerid”;

 _ OpportunityCustomer.Value=new Customer();

 _ OpportunityCustomer.Value.type = “account”; //Set customer type (I am using account here)

 _ OpportunityCustomer.Value.Value=CustomerGUID;  //replace CustomerGUID with customer GUID

_OpportunityRelationship.Properties.Add(_OpportunityCustomer);

//set Opportunity ID 

LookupProperty _OpportunityID=new LookupProperty();

 _ OpportunityID.Name=”opportunityid”;

 _ OpportunityID.Value=new Lookup();

 _ OpportunityID.Value.Value=OpportunityID; //replace OpportunityID with opportunity GUID

 _OpportunityRelationship.Properties.Add(_OpportunityID);

 //Set Opportunity Role

//you can create Relationship Roles from Setting->Business Management->Relationship Roles        

 LookupProperty _OpportunityRole = new LookupProperty();

 _OpportunityRole.Name = “opportunityroleid”;

 _OpportunityRole.Value = new Lookup();

   _OpportunityRole.Value.Value =GUIDofRelationshipRole; //Replace GUIDofRelationshipRole with Relationship role Guid.

   _OpportunityRelationship.Properties.Add(_OpportunityRole);

  TargetCreateDynamic targetCreate = new TargetCreateDynamic();

  targetCreate.Entity = _OpportunityRelationship;

// Create the request object.

 CreateRequest create = new CreateRequest();

  // Set the properties of the request object.

  create.Target = targetCreate;

 // Execute the request.

 CreateResponse created = (CreateResponse)_Service.Execute(create);

 Hope it will help somebody.

Cheer :)

Leave a comment

Filed under MS CRM 4.0, Workflow & Plugin Development

Microsoft Dynamics CRM MVP

Just want to share with you all that I won Dynamics CRM MVP award. It is great honor to me and I would like to thank Microsoft and Dynamics CRM community for this recognition. Special thanks to Jim to submit my nomination for this great honor.

I would like to thank all my friends,my family members,my colleagues for their cooperation and help to achieve this award.

9 Comments

Filed under MS CRM 4.0

Run exe from MS CRM 4.0

If you are looking to run .exe from MS CRM 4.0, then you can do this easily. You can create a ISV button on MS CRM form and create on global function on formonload can call it in ISV button JS.. you can write below code to run .exe

var _Object=new ActiveXObject( “WScript.Shell” ) ;
_Object.Run(“C:\\WINDOWS\\system32\\calc.exe” );

 you can also pass parameters with Run command.

Enjoy !!!

4 Comments

Filed under JS Script, MS CRM 4.0

Easy way to avoid Infinite loop in Update plugin

I have seen many developers facing issue of infinite loop while using update method inside update plugin. I know it could be requirement specific, to use update method inside update plugin. But sometime we can void infinite issue, there is no need of calling update method. You could play with inputparmeter propertybag. For example sometime we have requirement to calculate values based on the input values scenarios like this we don’t need to use update method. So the question is how to update record without using update method in update plugin. For scenarios like these we can register our plugin on pre state and can catch our inputs from inputpropertybag and after calculating our values we can add our new property of resultant value in inputpropertybag, so no need of using update method to save value in database.

We need to follow below steps

  1. Register preupdate plugin.
  2. Read required input values from inputparameter propertybag.
  3. Calculate your resultant value.
  4. Create new property of your resultant value.
  5. Add new property to input propertybag.

Enjoy !!!

Leave a comment

Filed under MS CRM 4.0, Workflow & Plugin Development

Close Activity Event in MS CRM

sometime we need to perform some action when activity is closed in MS CRM.In that case you have to capture close activity event, you can do this using event.Mode property in onsave event. you just have to write below code.

if(event.Mode==5) //Close activity event

{

//you code to implement your logic

}

Enjoy !!!

Leave a comment

Filed under JS Script, MS CRM & Javascript, MS CRM 4.0

Attach onclick event with MS CRM CRM checkbox

As you know that we can set control formatting of bit field to use it as checkbox. But only changing formatting does not make it to behave like a checkbox because of onchange event. Let take an example for this, like we have one field “Calculate Value” and we want to calculate some value on the selection of this bit field. We can set control formatting for the bit field to display like a checkbox.

 So if we will write some code onchange event of this bit field

alert(‘Clicked’);

You will not get this alert until you will move focus from this field. so make it to behave like real checkbox you have to attach onclick event with this field. You can do this using below code on form onload

crmForm.all.new_calculatevalue.onclick=function()

{

alert(‘Clicked’);

 }

after that you can simple check DataValue for bit ,if it is true or false to work accordingly

 Enjoy!!

1 Comment

Filed under JS Script, MS CRM 4.0

Remove Picklist option based on Security Roles

Most of the client asks for this requirement. They want to hide picklist options based on security roles. Let’s take an example for this, for example if we have picklist “Customer Status” having four option like “Request for Approve”,”Approved”,”Rejected”,”Black Listed”. Let’s say we have requirement to show only first option to all users having security roles other then “System Admin”. Only “System Admin” should be able to see all of these options, you can implement this using below code

//First check user have “sys admin” role or not

function CheckSecurityRole() {

    var xml = “” +

    “<?xml version=\”1.0\” encoding=\”utf-8\”?>” +

    “<soap:Envelope xmlns:soap=\”http://schemas.xmlsoap.org/soap/envelope/\” xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\” xmlns:xsd=\”http://www.w3.org/2001/XMLSchema\”>” +

    GenerateAuthenticationHeader() +

    ” <soap:Body>” +

    ” <RetrieveMultiple xmlns=\”http://schemas.microsoft.com/crm/2007/WebServices\”>” +

    ” <query xmlns:q1=\”http://schemas.microsoft.com/crm/2006/Query\” xsi:type=\”q1:QueryExpression\”>” +

    ” <q1:EntityName>role</q1:EntityName>” +

    ” <q1:ColumnSet xsi:type=\”q1:ColumnSet\”>” +

    ” <q1:Attributes>” +

    ” <q1:Attribute>name</q1:Attribute>” +

    ” </q1:Attributes>” +

    ” </q1:ColumnSet>” +

    ” <q1:Distinct>false</q1:Distinct>” +

    ” <q1:LinkEntities>” +

    ” <q1:LinkEntity>” +

    ” <q1:LinkFromAttributeName>roleid</q1:LinkFromAttributeName>” +

    ” <q1:LinkFromEntityName>role</q1:LinkFromEntityName>” +

    ” <q1:LinkToEntityName>systemuserroles</q1:LinkToEntityName>” +

    ” <q1:LinkToAttributeName>roleid</q1:LinkToAttributeName>” +

    ” <q1:JoinOperator>Inner</q1:JoinOperator>” +

    ” <q1:LinkEntities>” +

    ” <q1:LinkEntity>” +

    ” <q1:LinkFromAttributeName>systemuserid</q1:LinkFromAttributeName>” +

    ” <q1:LinkFromEntityName>systemuserroles</q1:LinkFromEntityName>” +

    ” <q1:LinkToEntityName>systemuser</q1:LinkToEntityName>” +

    ” <q1:LinkToAttributeName>systemuserid</q1:LinkToAttributeName>” +

    ” <q1:JoinOperator>Inner</q1:JoinOperator>” +

    ” <q1:LinkCriteria>” +

    ” <q1:FilterOperator>And</q1:FilterOperator>” +

    ” <q1:Conditions>” +

    ” <q1:Condition>” +

    ” <q1:AttributeName>systemuserid</q1:AttributeName>” +

    ” <q1:Operator>EqualUserId</q1:Operator>” +

    ” </q1:Condition>” +

    ” </q1:Conditions>” +

    ” </q1:LinkCriteria>” +

    ” </q1:LinkEntity>” +

    ” </q1:LinkEntities>” +

    ” </q1:LinkEntity>” +

    ” </q1:LinkEntities>” +

    ” </query>” +

    ” </RetrieveMultiple>” +

    ” </soap:Body>” +

    “</soap:Envelope>” +

    “”;

    var xmlHttpRequest = new ActiveXObject(“Msxml2.XMLHTTP”);

    xmlHttpRequest.Open(“POST”, “/mscrmservices/2007/CrmService.asmx”, false);

    xmlHttpRequest.setRequestHeader(“SOAPAction”, ” http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple&#8221;);

    xmlHttpRequest.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8″);

    xmlHttpRequest.setRequestHeader(“Content-Length”, xml.length);

    xmlHttpRequest.send(xml);

    var resultXml = xmlHttpRequest.responseXML;

    if (resultXml != null) {

        //select the node text

        var RoleResponse = resultXml.selectNodes(“//BusinessEntity/q1:name”);

        if (RoleResponse != null) {

            for (i = 0; i < RoleResponse.length; i++) {

                if (RoleResponse[i].text == ‘System Administrator’) {

                    return true;

                }

            }

        }

    }

    return false;

}

//if your does not have sys admin role remove picklist options

//Let consider I have used index 1,2,3,4 for our picklist options

function HidePicklistOptions()

{

    If(!CheckSecurityRole())

    {

    var Picklist=crmForm.all.new_customerstatus; // get picklist

    Picklist.DeleteOption(2); //use approval index to remove Approved from piclist

    Picklist.DeleteOption(3); //to remove Rejected

    Picklist.DeleteOption(4); //to remove Black Listed

    }

}

 DeleteOption basically removes picklist option temporary, it does not remove them from database permanently.

Enjoy !!!

Leave a comment

Filed under JS Script, MS CRM 4.0

“This control only accepts strings or null as input” error message

when you will try to assign integer value to nvarchar attribute in MS CRM like below

var IntVal=10;

Let’s say we have one field named “ABC” having nvarchar datatype

crmForm.all.ABC.DataValue=IntVal;

you will get error “This control only accepts strings or null as input”. to solve this issue you can use toString() method of JS like below

crmForm.all.ABC.DataValue=IntVal.toString();

Enjoy !!!

Leave a comment

Filed under MS CRM & Javascript, MS CRM 4.0

New MS CRM 4.0 VPC

I know most the developers use CRM VPC for their development work, so I thought to provide them link to download latest MS CRM 4.0 VPC, But remember you can download it only if you are microsoft partner

Here are the link for download

CRM 4 July 2010 VPC.part01.exe 

CRM 4 July 2010 VPC.part02.rar

CRM 4 July 2010 VPC.part03.rar

CRM 4 July 2010 VPC.part04.rar

Enjoy !!!

Leave a comment

Filed under MS CRM 4.0

Dynamics CRM Development Part – 2

After creating CRM environment, now it’s time to start our development.Sometime we need to implement some new functionality in CRM that CRM does not provide OOB. Then crm customization and development comes in picture, so basically we can say that CRM development is a result of our CRM Extension plan. Dynamics CRM is a highly extendable application, we can extend it according to your business needs,for more information you can check Dynamics CRM Extensibility Architecture. We can extend Dynamics CRM using Client Side and Server side code. Java script is used for Client side programming and for server side programming we can use C# or VB.net. we can use crm webservices and API to extend it. Dynamics CRM provided basically three webservice

1      CrmDiscoveryService – used to get organization information

2      CrmService- used to interact with CRM Data

3      MetadataService- used to modify data structure

Now the question is where we can use CRM webservices or API.we have different option, where we can do our development for Dynamics CRM for example

  1. CRM Form and Field events. (for JS code)
  2. Plugin. (for server side code)
  3. Custom workflow. (for server side code)
  4. Custom Integration. (for server side code)

I will discuss on these points one by one in my development series. So let’s start with using CRM form and field events. Dynamics CRM form comes with two basic events Onload and OnSave. you can use Onload event to write JS code for different purpose,for that we use the most important property of CRM form “FormType”, which helps us to detect form’s mode, for example if the form is opened in create mode,update mode,readonly mode etc. you can check FormType property as below

if(crmForm.FormType==1)

{           // Create mode                  }

else if(crmForm.FormType==2)

{  //Update mode        }

 for all FormType value you can check http://msdn.microsoft.com/en-us/library/cc150873.aspx . Another thing that I want to point out here is that as we can use FormType property to detect form’s mode on onload in the same way we can use Event.mode on onsave event. We can use Event.mode to check for which purpose Onsave event is called. we can use below JS to check event.Mode value on onsave event

alert(event.Mode);

For all possible value of Event.mode you can check http://msdn.microsoft.com/en-us/library/cc150868.aspx, another good resource for the same is http://mscrmtools.blogspot.com/2009/01/jscript-know-which-action-raised-onsave.html (thanks to Tanguy,for this wonderful post).

All CRM field comes with onchange event.But you can add another JS event for crm field,  for example if you want to call some code on onclick of crm field you can use as below  code on form load event

 crmForm.all.<FieldName>.onclick=function()

{alert(“ Mouse click”);}

I will request you to check http://msdn.microsoft.com/en-us/library/cc150866.aspx for different data types that Dynamics CRM supports you can check.  So Javascript can be used for multipurpose, you can use JS code to retrieve crm data with the help of crmservice refer http://msdn.microsoft.com/en-us/library/cc150864.aspx  to use all crmwebservice method through JS. We can write JS code on ISV.config  file while creating custom button. but while using JS code in ISV.config you should keep in mind to replace some character with their encoded value like below

Character                                          Encode Value

<                                                               &lt;

”                                                                 &quot;

&                                                               &amp;

you can write JS code to load iFrame content dynamically. You can check my various post that I have written to use JS code in Dynamic CRM.

I hope this post will help you to understand basic use of JS in Dynamics CRM development, so in next part I will discuss about plugins.

2 Comments

Filed under CRM Dvelopment Series