Friday, January 27, 2012

Mapping Value from Parent Form to child View in CRM

Sometimes, you have to get the value from parent form and has to show in the child form. Clients may use 2 methods to open child view.
1. Using Associate Views
2. Using Sub Grid
3. Using button in ribbon

Base on the clients' selected method, we have to implement the code, because if the child form is created using Associate Views, window.parent.opener.Xrm.Page.data will be null and when calling from sub grids window.top.opener.parent.Xrm.Page.ui will be null.

So here, I have implemented a method to check both conditions. And in the Parent form, you may have a Lookup, int, decimal or picklist, that has to be mapped with different control in child page

Then, copy below function to your common Library JS (If you dont have a common JS web resource, it's time to create a one)

function MapFromParentForm(target, source)
{
    var targetControl = Xrm.Page.data.entity.attributes.get(target);
    var sourceControl;

    if (window.top.opener.parent != null && window.top.opener.parent.Xrm != null 
                    && window.top.opener.parent.Xrm.Page != null 
                    && window.top.opener.parent.Xrm.Page.ui != null)
    {        
        sourceControl = window.top.opener.parent.Xrm.Page.data.entity.attributes.get(source);
    }
    else if(window.parent != null && window.parent.opener != null 
                    && window.parent.opener.Xrm != null 
                    && window.parent.opener.Xrm.Page != null 
                    && window.parent.opener.Xrm.Page.data != null 
                    && window.parent.opener.Xrm.Page.data.entity != null 
                    && window.parent.opener.Xrm.Page.data.entity.attributes != null)
    {
        sourceControl = window.parent.opener.Xrm.Page.data.entity.attributes.get(source);
    }

    if (targetControl != null && sourceControl != null)
    {
        var sourceValue;
        if (sourceControl.getAttributeType() == "lookup")
        {
            sourceValue = sourceControl.getValue()[0].name;
        }
        else
        {
            sourceValue = sourceControl.getValue();
        }


        if (targetControl.getAttributeType() == "lookup")
        {
            targetControl.setValue(sourceValue)
        }
        else if (targetControl.getAttributeType() == "optionset")
        {
            targetControl.setValue(parseInt(sourceValue))
        }
        else
        {
            targetControl.setValue(sourceValue)
        }
    }
}


Then from the child Page form,
1. Add the Common Javascript web Resouce
2. And in form onLoad event, called the function as below

MapFromParentForm with the dynamic parameters as 'new_name', 'new_contactId' where 'new_name' is the control in child form and 'new_contactId' is in the parent form

Thursday, January 19, 2012

Tuesday, January 17, 2012

Object address not found on party or party is marked as non-emailable

When you want to send an email using CRM, you may use direct method to send email or you may use SendEmailFromTemplateRequest. And sometimes you may get the error "Object address not found on party or party is marked as non-emailable". Once you debug and found the error, you will be differently know that either the email address of 'To' is blank or marked as 'No Email' or 'No Bulk Email'. But when if fields are set correctly, and if you get the same error, once shall we do?

Other than the To Address, you have to check the 'From' users email validity as well(which we usually forget to set), because if it is missing, you will get the same error 'Object address not found on party or party is marked as non-emailable'

And what will happen if the From field is blank? Then CRM conside From user as execution context user. (May be plugin executer in Plugin Context or executer in CRM Async Service for workflows or the credentials you used to communicate with CRM Service.) And if the email address of this user is missing, you will get the same error. "Object address not found on party or party is marked as non-emailable"

Thursday, January 5, 2012

Show CRM Form based on User Role

Sometimes we have to show CRM Form by logged user role. The best way to do this is assign form to correct Security role using CRM Custermizations. But sometimes we have to show the correct Form using Javascripts. For that you can use the below code. Copy the below function to your global/common javascript file
function LoadUserForm(formName)
{

 var allForms = Xrm.Page.ui.formSelector.items.get();
 
 if(allForms != null && allForms.length > 1)
 {
   for(var i = 0; i < allForms.length; i++)
   {
    var currentName = allForms[i].getLabel().toLowerCase();
    
   if(currentName == formName)
    { 
     Xrm.Page.ui.formSelector.items.get(i).navigate();
     break;
    }
   }
 }
}
Then copy below function to all forms in the same entity

function window.onload()
{
 var currentUserRole = GetCurrentUserRole();
 if(currentUserRole == 'System Administrator')
 {
  LoadUserForm('SA Form')
 }
 else if(currentUserRole == 'Sales Manager')
 {
  LoadUserForm('SM Form')
 }
 else if(currentUserRole == 'System Operator')
 {
  LoadUserForm('SO Form')
 }
}