Sunday, 19 January 2014

Get entity form name in CRM 2013 using Javascript.

function getFormName() {
    var formLabel = Xrm.Page.ui.formSelector.getCurrentItem().getLabel();
    alert(formLabel.toString());
    if (formLabel == 'YourFormName') {
       //Write your logic here.
    }
}

Retrieve lookup field data in CRM 2013 usign javascript.

The following sample code is used to retrieve entity information by Entity Id via Javascript.

function getRecordDetails() {
    var EntityName, EntityId, LookupFieldObject;
    var Name = "";
    var resultXml;
    LookupFieldObject = Xrm.Page.data.entity.attributes.get('parentaccountid');
    if (LookupFieldObject.getValue() != null) {
        EntityId = LookupFieldObject.getValue()[0].id;
        EntityName = LookupFieldObject.getValue()[0].entityType;
        resultXml = getDetails(EntityName, EntityId);
// Retrieve optionset value and set the value to current record field.
        if (resultXml != null && resultXml.attributes['new_industry'] != null) {
            var industry = resultXml.attributes['new_industry'].value;          
            Xrm.Page.getAttribute('new_common_industry').setValue(industry);          
        }
        else
            Xrm.Page.getAttribute('new_common_industry').setValue(null);

// Retrieve text field value and set the value to current record field.
        if (resultXml != null && resultXml.attributes['new_industrydetail'] != null) {
            var industrydetail = resultXml.attributes['new_industrydetail'].value;          
            Xrm.Page.data.entity.attributes.get('new_industrydetail').setValue(industrydetail);          
        }
        else
            Xrm.Page.data.entity.attributes.get('new_industrydetail').setValue("");

// Retrieve lookup field value and set the value to current record field.
        if (resultXml != null && resultXml.attributes['primarycontactid'] != null) {          
            var lookupValue = new Array();
            lookupValue[0] = new Object();
            lookupValue[0].id = resultXml.attributes['primarycontactid'].id;
            lookupValue[0].name = resultXml.attributes['primarycontactid'].name;
            lookupValue[0].entityType = resultXml.attributes['primarycontactid'].logicalName;
            Xrm.Page.data.entity.attributes.get("parentcontactid").setValue(lookupValue);
        }
        else
            Xrm.Page.data.entity.attributes.get("parentcontactid").setValue(null);
    }
}

function getDetails(EntityName, EntityId) {
    var cols = ["new_industry", "new_industrydetail", "primarycontactid"];
    var retrievedResult = XrmServiceToolkit.Soap.Retrieve(EntityName, EntityId, cols);
    return retrievedResult;
}

After that you need to add the following files to the Form Libraries.

Jquery
Json2
XrmServiceToolkit


Monday, 6 January 2014

Prevent to create opportunity when qualifying Lead in CRM 2013

Use the following code in your plugin and register in the PostValidation on the QualifyLead message for the Lead entity.

context.InputParameters["CreateOpportunity"] = false; // set to true by default
//context.InputParameters["CreateAccount"] = false; // set to true by default
//context.InputParameters["CreateContact"] = false; // set to true by default

Tuesday, 19 November 2013

Retrieve records associate contact email address in CRM 2011 using C#

Here i retrieve email id in the "to" field of email activity.

Entity entity = context.InputParameters["Target"] as Entity;
                   
                    if (entity.LogicalName != "email")
                        return;
                    Email email = entity.ToEntity<Email>();                  
                    string to = string.Empty;

                    ColumnSet col = new ColumnSet("to");
                    entity = service.Retrieve(entity.LogicalName, entity.Id, col);
                    Guid partyId = new Guid();
                    EntityCollection Recipients = entity.GetAttributeValue<EntityCollection>("to");
                    foreach (var party in Recipients.Entities)
                    {                      
                        partyId = party.GetAttributeValue<EntityReference>("partyid").Id;
                    }

                    ColumnSet column = new ColumnSet("emailaddress1");
                    Entity toRecipent= service.Retrieve("contact", partyId, column);

                    Contact contact = toRecipent.ToEntity<Contact>();
                    to = contact.EMailAddress1;

How to use Access teams in CRM 2013.

Monday, 18 November 2013

Enable/Disable Autosave options in CRM 2013

Autosave can be enabled/disabled by navigating to Settings > Administration > System Settings > General Tab.


Friday, 15 November 2013

Get Option set text value from CRM 2011 using C#.

public string getOptionSetText(string entityName, string attributeName, int optionsetValue)
   {
       string optionsetText = string.Empty;
       RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest();
       retrieveAttributeRequest.EntityLogicalName = entityName;
       retrieveAttributeRequest.LogicalName = attributeName;
       retrieveAttributeRequest.RetrieveAsIfPublished = true;

       RetrieveAttributeResponse retrieveAttributeResponse = 
         (RetrieveAttributeResponse)OrganizationService.Execute(retrieveAttributeRequest);
       PicklistAttributeMetadata picklistAttributeMetadata = 
         (PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;

       OptionSetMetadata optionsetMetadata = picklistAttributeMetadata.OptionSet;

       foreach (OptionMetadata optionMetadata in optionsetMetadata.Options)
       {
            if (optionMetadata.Value == optionsetValue)
             {
                optionsetText = optionMetadata.Label.UserLocalizedLabel.Label;
                 return optionsetText;
             }

       }
       return optionsetText;
  }