الأربعاء، 29 يناير 2014

CRM 2011 : unable to cast object of type 'microsoft.xrm.sdk.entity' to type 'xrm.account'

To Solve it

Use enableProxyTypes method on your Organization Service proxy to fix it, here is the sample for generating organization service proxy properly:

OrganizationServiceProxy orgService = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
 orgService.EnableProxyTypes();

 orgService.Authenticate(); 
 IOrganizationService _service = (IOrganizationService)orgService;

How to use linq with crm 2011 plugin


create XrmServiceContext class
  a- run command prompt 
  b-write the following command "cd C:\Program Files\Microsoft Dynamics CRM\tools" then press enter
  c- write the following command then press enter
        CrmSvcUtil.exe /out:Xrm.cs /url:http://serverName/OrgName/XRMServices/2011/Organization.svc /domain:domainName /username:YourUserName /password:yourPassword /namespace:Xrm /serviceContextName:XrmServiceContext

       (Note:replace the red words with your CRM login configuration)
  d- go to the following path "C:\Program Files\Microsoft Dynamics CRM\tools" and copy "Xrm.cs" file then past to your project.

using (var crm = new XrmServiceContext(service))
 {
       var QuoteProduct = crm.QuoteDetailSet.Where(c => c.QuoteDetailId == QPID).First();
       foreach((var item in QuoteProduct)
      {

      }
}



الأحد، 26 يناير 2014

CRM 2011: Set the focus to control

function ControlSetFocus(fieldName) {
    var attribute = Xrm.Page.data.entity.attributes.get(fieldName);
    var control = attribute.controls.get(0);
    control.setFocus(true);
}

CRM 2011 Validate required form using javascript


This code checks if form is valid for saving, by going over all required attributes and checking if it contains value.

function IsFormValidForSaving(){
var valid = true; Xrm.Page.data.entity.attributes.forEach(function (attribute, index) { if (attribute.getRequiredLevel() == "required") { if (attribute.getValue() == null) { if (valid) { var control = attribute.controls.get(0); alert(control.getLabel() + " Field is empty"); control.setFocus(); } valid = false; } } }); return valid;
}
http://dynamicslollipops.blogspot.com/2012/07/microsoft-dynamics-crm-2011-validate.html

الخميس، 23 يناير 2014

CRM2011:Syntax error... /userdefined/edit.aspx?etc=

When I was using Visual Ribbon Editor, I added a group and a button to trigger a JavaScript function in a JavaScript Library, I was using CRM 2011 and rollup 12, I found this:
Issue:
If you upgraded to CRM 2011 rollup 12 already and use Visual Ribbon Editor to create button, and the action of  button is calling a JavaScript function in a JavaScript Library, you might get a JavaScript Syntax Error.
Solution:
Add $webresource: in front of you JavaScript library.
For examples:
in if you JavaScript library name is: myJavaScriptLibrary.js, then should specify like like this:
$webresource:myJavaScriptLibrary.js
OR
/WebResources/myJavascriptLibrary

الثلاثاء، 14 يناير 2014

How to retrieve the value of an Optionset using SQL

OptionSet labels are stored in stored in the StringMapBase table.

SELECT 
      [ObjectTypeCode]

      ,[AttributeName]

      ,[AttributeValue]

      ,[LangId]

      ,[OrganizationId]

      ,[Value]

  FROM [StringMap]

  WHERE AttributeName = 'OptionsetName'

الأحد، 5 يناير 2014

Execute workflow using javascript in CRM 2011

function RunWorkflow() {
    var _return = window.confirm('Are you want to execute workflow.');
    if (_return) {
        var url = Xrm.Page.context.getServerUrl();
        var entityId = Xrm.Page.data.entity.getId();
        var workflowId = '33dce53c-9107-4148-9712-52f83742e6b3';
        var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
        url = url + OrgServicePath;
        var request;
        request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                      "<s:Body>" +
                        "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                          "<request i:type=\"b:ExecuteWorkflowRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">" +
                            "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">" +
                              "<a:KeyValuePairOfstringanyType>" +
                                "<c:key>EntityId</c:key>" +
                                "<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + entityId + "</c:value>" +
                              "</a:KeyValuePairOfstringanyType>" +
                              "<a:KeyValuePairOfstringanyType>" +
                                "<c:key>WorkflowId</c:key>" +
                                "<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + workflowId + "</c:value>" +
                              "</a:KeyValuePairOfstringanyType>" +
                            "</a:Parameters>" +
                            "<a:RequestId i:nil=\"true\" />" +
                            "<a:RequestName>ExecuteWorkflow</a:RequestName>" +
                          "</request>" +
                        "</Execute>" +
                      "</s:Body>" +
                    "</s:Envelope>";

        var req = new XMLHttpRequest();
        req.open("POST", url, true)
        // Responses will return XML. It isn't possible to return JSON.
        req.setRequestHeader("Accept", "application/xml, text/xml, */*");
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        req.onreadystatechange = function () { AssignResponse(req); };
        req.send(request);
    }
}

function AssignResponse(req) {
    if (req.readyState == 4) {
        if (req.status == 200) {
            alert('successfully executed the workflow');
        }
    }
}