CRM 2011 Cascading Drop Down

0 comments


Ever encounter problem is cascading drop down?
Cascading in different level ? or maybe just one level.
Using this script.

*Note put this script function cascadingDropdown on the field that onchange to trigger the cascade.

Javascript Eg.

//Function to put in toward field trigger onchange

function cascadingDropdown() {
    var Saving = Xrm.Page.data.entity.attributes.get("vw_saving").getValue();
    var SavingType = "vw_savingtype";
    var picklistTwo = Xrm.Page.getControl(SavingType);
    var picklistTwoAttribute = picklistTwo.getAttribute();


    if (picklistTwo.flag == true) {
        picklistTwo.clearOptions();
        var origOptions = picklistTwo.originalPicklistValues;


        for (var i = origOptions.length - 1; i >= 0; i--) {
            if (origOptions[i].text != "") {
                picklistTwo.addOption(origOptions[i]);
            }
        }
    }
    else {
        picklistTwo.originalPicklistValues = picklistTwoAttribute.getOptions();
        picklistTwo.flag = true;
    }


    var picklistTwoOptions = picklistTwoAttribute.getOptions();


    switch (Saving) {
        case true:
            for (var i = picklistTwoOptions.length - 1; i >= 0; i--) {
                if (picklistTwoOptions[i].value != null && picklistTwoOptions[i].value != "") {
                    var optionText = picklistTwoOptions[i].text;
                    var optionValue = picklistTwoOptions[i].value;
                    //Remove these values (This is the problem with hard code)
                    //U can change the code to get the value too. It depends.
                    if (optionText == "Negotiation" || optionText == "Discount" || optionText == "Contracting Strategy" || optionText == "Online Bidding (OLB)") {
                        picklistTwo.removeOption(optionValue);
                    }
                    //END: EOS Selection
                }
            }
            break;
        case false:


            for (var i = picklistTwoOptions.length - 1; i >= 0; i--) {
                if (picklistTwoOptions[i].value != null && picklistTwoOptions[i].value != "") {
                    var optionText = picklistTwoOptions[i].text;
                    var optionValue = picklistTwoOptions[i].value;
                    //Remove these values
                    if (optionText == "Joint Tender" || optionText == "Integrated Tender") {
                        picklistTwo.removeOption(optionValue);
                    }
                    //END: CS Selection
                }
            }
            break;
        default:
            break;
    }
}


End.

The bad thing about this script is if u have thousand or hundred of cascading record, u need to hardcore the value one by one and it takes a lot of time and script line.
For what i suggest if there are thousand or more than thousand of cascading record, better make use of the lookup toward entity. By using lookup it wont be consuming much time in scripting or work with the cascading part.

CRM 2011 Date Time Duration

0 comments


Date Time Duration between two field.
*Note duration field is in decimal type. That mean the calculation can calculate until decimal point. At here u can set the amount of decimal point u want to see at the field.

Javascript Eg.

function getAging() { 
     var Answer = 0; 
     var AgingStartTime = Xrm.Page.data.entity.attributes.get("FieldName").getValue();
     var AgingEndTime = Xrm.Page.data.entity.attributes.get("FieldName").getValue(); 
     if (AgingStartTime != null) { 
         //Date time duration different between today and AgingStartTime
         var today = new Date();  
         Answer = (today - AgingStartTime) / 1000 / 60 / 60 / 24; 
         //Date time duration different between AgingStartTime and AgingEndTime 
         Answer = ( AgingStartTime  -  AgingEndTime ) / 1000 / 60 / 60 / 24; 
     } 
     else { Answer = null; } 
    //Which field to put the answer record to.
     Xrm.Page.data.entity.attributes.get("FieldName").setValue(Answer); 
}

End.

CRM 2011 Focus To A Certain Field

0 comments


Javascript to focus to particular field
Xrm.Page.ui.controls.get("FieldName").setFocus();

CRM 2011 Set Field Into Mandatory Field

0 comments


Javascipt to set the field into mandatory field.

Javascript Eg.

function setResponsible() { 
      //Status act as picklist to trigger the field to become mandatory 
      var status = Xrm.Page.getAttribute("vw_status").getValue(); 
      //setRequiredLevel to set as mandatory or other (none, required and recommended)
      if (status == 2) { Xrm.Page.getAttribute("vw_staffresponsibleid").setRequiredLevel("required"); } 
      else { Xrm.Page.getAttribute("vw_staffresponsibleid").setRequiredLevel("none"); } 
}

End.

CRM 2011 Javascript Cancelling Onsave Event

0 comments

*Note make sure you have a parameter defined to receive the Execution Object
Javascript Eg.



function EitherOne(executionObj) {
    var Field1 = Xrm.Page.data.entity.attributes.get("customerid").getValue();
    var Field2 = Xrm.Page.data.entity.attributes.get("vw_name").getValue();


    if (Field1 == null && Field2 == null) {
        alert('Either Customer or Name fields need to be entered with data.');
        // The getEventArgs() method returns an object with methods to manage the Save event.
        // The preventDefault() method cancels the save operation        executionObj.getEventArgs().preventDefault();
        Xrm.Page.ui.controls.get("customerid").setFocus();
    }
    else { onSave(); }
}


End of Script


Where to place the script?
*Note Make sure u check the pass execution context as first parameter.


CRM 2011 Filter Add Existing Button By Parent Field

1 comments


This thing work with 1:N relationship and N:N relationship.

How does this thing work?

Let say u have entity A and entity B [A : B (1:N relationship)]. When u are in form view A u can navigate to sub entity B [Subgrid view for entity B] by clicking the related entity located at the left side. Now u are in sub entity B and u can either create new or add existing record in entity B.
Here the thing work. I wan to filter add existing button in entity B. Filter by the parent record field [Field in entity A]. The method i'm using is FetchXml and Javascript.

The problem now is:
1. I cannot create view filter by field. [Another view].
2. I don't wan the view to appear in the Entity B view.
3. How to generate the view using fetchXml.

Steps:
Step 1: Create a javascript function name it as CustomAddExistingView.js [That what i name my script]. The script library locate at $webresource: new_CustomAddExistingView.js. Add the script into CustomAddExistingView.js.

JavaScript Eg:



function replaceAddExistingButtonView(params) {


    var relName = params.gridControl.getParameter("relName"),
        roleOrd = params.gridControl.getParameter("roleOrd"),
        viewId = "{00000000-0000-0000-0000-000000000001}"; // a dummy view ID


    var customView = {
        fetchXml: params.fetchXml,
        id: viewId,
        layoutXml: params.layoutXml,
        name: params.name,
        recordType: params.gridTypeCode,
        Type: 0
    };


    var lookupItems = LookupObjects(null, "multi", params.gridTypeCode, 0, null, "", null, null, null, null, null, null, viewId, [customView]);
    if (lookupItems && lookupItems.items.length > 0) {
        AssociateObjects(document.parentWindow.parent.crmFormSubmit.crmFormSubmitObjectType.value, document.parentWindow.parent.crmFormSubmit.crmFormSubmitId.value, params.gridTypeCode, lookupItems, IsNull(roleOrd) || roleOrd == 2, "", relName);
    }
}


function replaceAddExistingButtonViewForAccount(gridTypeCode, gridControl, primaryEntityName) {
    //get the entity name on which entity to work at
    var locateField = document.parentWindow.parent.Xrm.Page.data.entity.getEntityName();


    if (locateField == "vw_workorder") {
        //get parent field
        var CaseId = document.parentWindow.parent.Xrm.Page.data.entity.attributes.get("vw_caseid").getValue();
        replaceAddExistingButtonView({
            gridTypeCode: gridTypeCode,
            gridControl: gridControl,
            //make sure all is in name not scheme name for fetchXml and layoutXml
            fetchXml: "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> " +
                      "<entity name='vw_items'>" +
                        "<attribute name='vw_itemtype' />" +
                        "<attribute name='vw_caseid' />" +
                        "<attribute name='vw_workorder' />" +
                        "<attribute name='vw_name' />" +
                        "<attribute name='vw_projectid' />" +
                        "<attribute name='vw_propertyid' />" +
                        "<attribute name='vw_category' />" +
                        "<attribute name='vw_area' />" +
                        "<attribute name='vw_element' />" +
                        "<attribute name='vw_defect' />" +
                        "<attribute name='vw_defectrectified' />" +
                        "<attribute name='createdon' />" +
                        "<order attribute='vw_itemtype' descending='false' />" +
                        "<filter type='and'>" +
                            "<condition attribute='vw_caseid' operator='eq' uiname='213' uitype='incident' value='" + CaseId[0].id + "' />" +
                            "<condition attribute='vw_workorder' operator='null'/>" +
                        "</filter>" +
                      "</entity>" +
                      "</fetch>",
            layoutXml: "<grid name='resultset' object='" + gridTypeCode + "' jump='vw_name' select='1' icon='1' preview='1'>" +
  "<row name='result' id='vw_itemsid'>" +
                        "<cell name='vw_itemtype' width='150' />" +
                        "<cell name='vw_caseid' width='100' />" +
                        "<cell name='vw_workorder' width='100' />" +
                        "<cell name='vw_name' width='200' />" +
                        "<cell name='vw_projectid' width='100' />" +
                        "<cell name='vw_propertyid' width='100' />" +
"<cell name='vw_category' width='100' />" +
"<cell name='vw_area' width='100' />" +
                        "<cell name='vw_element' width='100' />" +
"<cell name='vw_defect' width='100' />" +
                        "<cell name='vw_defectrectified' width='100' />" +
"<cell name='createdon' width='150' />" +
 "</row>" +
"</grid>",
            name: "Item Related To Case"
        });
    }
    else {
        replaceAddExistingButtonView({
            gridTypeCode: gridTypeCode,
            gridControl: gridControl,
            fetchXml: "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                       "<entity name='vw_items'>" +
                        "<attribute name='vw_itemtype' />" +
                        "<attribute name='vw_caseid' />" +
                        "<attribute name='vw_workorder' />" +
                        "<attribute name='vw_name' />" +
                        "<attribute name='vw_projectid' />" +
                        "<attribute name='vw_propertyid' />" +
                        "<attribute name='vw_category' />" +
                        "<attribute name='vw_area' />" +
                        "<attribute name='vw_element' />" +
                        "<attribute name='vw_defect' />" +
                        "<attribute name='vw_defectrectified' />" +
                        "<attribute name='createdon' />" +
                        "<order attribute='vw_itemtype' descending='false' />" +
                        "<filter type='and'>" +
                          "<condition attribute='vw_caseid' operator='null' />" +
                        "</filter>" +
                      "</entity>" +
                      "</fetch>",
            layoutXml: "<grid name='resultset' object='" + gridTypeCode + "' jump='vw_name' select='1' icon='1' preview='1'>" +
  "<row name='result' id='vw_itemsid'>" +
                        "<cell name='vw_itemtype' width='150' />" +
                        "<cell name='vw_caseid' width='100' />" +
                        "<cell name='vw_workorder' width='100' />" +
                        "<cell name='vw_name' width='200' />" +
                        "<cell name='vw_projectid' width='100' />" +
                        "<cell name='vw_propertyid' width='100' />" +
"<cell name='vw_category' width='100' />" +
"<cell name='vw_area' width='100' />" +
                        "<cell name='vw_element' width='100' />" +
"<cell name='vw_defect' width='100' />" +
                        "<cell name='vw_defectrectified' width='100' />" +
"<cell name='createdon' width='150' />" +
 "</row>" +
"</grid>",
            name: "Item Not Related To Case"
        });
    }
}


End of Javascript


Step 2: Now it time to edit based on the xml. Create one new solution name Ribbon.
Step 3: At the ribbon solution, add existing entity u wan to edit the ribbon and save. Let say i wan to edit entity A ribbon then i add existing entity A into the ribbon solution.
Step 4: Export the solution out and extract the solution.
Step 5: From the ribbon extracted file open customizations.xml and edit it.
Step 6: Find <RibbonDiffXml> and edit the xml according to below:

XML eg:


<RibbonDiffXml>
        <CustomActions />
        <Templates>
          <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
        </Templates>
    <CommandDefinitions>
      //For Many to Many Relationship add existing
      <CommandDefinition Id="Mscrm.AddExistingRecordFromSubGridAssociated">
        <EnableRules>
          <EnableRule Id="Mscrm.AppendToPrimary" />
          <EnableRule Id="Mscrm.EntityFormIsEnabled" />
        </EnableRules>
        <DisplayRules>
          <DisplayRule Id="Mscrm.AddExisting" />
          <DisplayRule Id="Mscrm.ShowForManyToManyGrids" />
          <DisplayRule Id="Mscrm.AppendToPrimary" />
          <DisplayRule Id="Mscrm.AppendSelected" />
          <DisplayRule Id="Mscrm.CanWriteSelected" />
        </DisplayRules>
        <Actions>
          <JavaScriptFunction FunctionName="replaceAddExistingButtonViewForAccount" Library="$webresource:new_CustomAddExistingView.js">
            <CrmParameter Value="SelectedEntityTypeCode" />
            <CrmParameter Value="SelectedControl" />
<CrmParameter Value="PrimaryEntityTypeName" />
          </JavaScriptFunction>
        </Actions>
      </CommandDefinition>
      //For Many to Many Relationship add existing
      <CommandDefinition Id="Mscrm.AddExistingRecordFromSubGridStandard">
        <EnableRules>
          <EnableRule Id="Mscrm.AppendToPrimary" />
          <EnableRule Id="Mscrm.EntityFormIsEnabled" />
        </EnableRules>
        <DisplayRules>
          <DisplayRule Id="Mscrm.AddExisting" />
          <DisplayRule Id="Mscrm.ShowForOneToManyGrids" />
          <DisplayRule Id="Mscrm.AppendToPrimary" />
          <DisplayRule Id="Mscrm.AppendSelected" />
          <DisplayRule Id="Mscrm.CanWriteSelected" />
        </DisplayRules>
        <Actions>
          <JavaScriptFunction FunctionName="replaceAddExistingButtonViewForAccount" Library="$webresource:new_CustomAddExistingView.js">
            <CrmParameter Value="SelectedEntityTypeCode" />
            <CrmParameter Value="SelectedControl" />
<CrmParameter Value="PrimaryEntityTypeName" />
          </JavaScriptFunction>
        </Actions>
      </CommandDefinition>
    </CommandDefinitions>
        <RuleDefinitions>
          <TabDisplayRules />
          <DisplayRules />
          <EnableRules />
        </RuleDefinitions>
        <LocLabels />
      </RibbonDiffXml>

End of XML
*Note - Make sure that the library and the function name is correct.

Step 7: Zip the 3 xml file (Customization, Solution and [Content_Types]) into Ribbon.zip.
Step 8: Import back everything into the solution and publish all.

Finish ! Try it... anything just leave me a comment or message. Glad to help.^^

CRM 2011 – Enabling and Disabling Ribbon Buttons

0 comments


Here would be the place to refer.
http://www.powerobjects.com/blog/2011/06/17/crm-2011-enabling-and-disabling-ribbon-buttons-2/

CRM 2011 Javascript Date Add With Duration Based On Month, Year And Days

0 comments


function DateAdd() {
    //Get today date
    var Today = new Date();

    //Get the date
    var DateSentOut = Xrm.Page.data.entity.attributes.get("DateFieldName").getValue();

    //Set based on days,months and years
    //Date.setMonth(month,day) / if year add with 12 month like that
    DateSentOut.setMonth(DateSentOut.getMonth() + 3); //Add 3 Months Duration

    //Set the field answer.
    Xrm.Page.data.entity.attributes.get("AnswerFieldName").setValue(DateSentOut);
}

CRM 2011 Change Help Server URL

0 comments


Access the database for the CRM
Find the table ConfigSettings in the MSCRM_Config database.
Set the HelpServerURL value to blank (or your CRM domainname).
Using Inetmgr to refresh or recycle it.
Then recycle the CRM application pool in IIS(iisreset) to update the changes. Done!