Friday, October 28, 2016

Set PartyList or Regarding Lookup when using Xrm.Utility object

When using Xrm.Utility object to open entity form or quick Create form, we cannot set PartyList or Regarding Lookup with default field parameters. But there is workaround for this, we can use custom query string parameters and using custom query string parameters we can set PartyList or regarding lookup.
Suppose we want to open email entity form from contact entity and want to set To and 
Regarding lookup when email entity form opened using either
 Xrm.Utility.openEntityForm or Xrm.Utility.openQuickCreate
  
·         Open Email Form or quick create form
·         In Form properties, select Parameter tab.
·         Add 3 parameters, to set To partylist.
Name: new_ToType
Type: SafeString
Name: new_ToName
Type: SafeString
Name: new_ToId
Type: UniqueId
·         Add 3 parameters, to set Regarding lookup
Name: new_RegardingIdName
Type: SafeString
Name: new_RegardingIdType
Type: SafeString
Name: new_RegardingId
Type: UniqueId



·         In Email form load event add JavaScript, which will get query string parameters and will set To party and Regarding lookup.
var emailScript =
{
    setPartyListnRegarding: function () {
        if (Xrm.Page.ui.getFormType() == 1) {

            /*To Party list*/
            var ToPartyList = [];
            ToPartyList[0] = {};

            if (Xrm.Page.context.getQueryStringParameters().new_ToType != null && Xrm.Page.context.getQueryStringParameters().new_ToType != 'undefined') {
                ToPartyList[0].entityType = Xrm.Page.context.getQueryStringParameters().new_ToType;
            }

            if (Xrm.Page.context.getQueryStringParameters().new_ToId != null && Xrm.Page.context.getQueryStringParameters().new_ToId != 'undefined') {
                ToPartyList[0].id = Xrm.Page.context.getQueryStringParameters().new_ToId;
            }
            if (Xrm.Page.context.getQueryStringParameters().new_ToName != null && Xrm.Page.context.getQueryStringParameters().new_ToName != 'undefined') {
                ToPartyList[0].name = Xrm.Page.context.getQueryStringParameters().new_ToName;
            }

            /*Set To Partylist*/
            if (ToPartyList[0] != null && ToPartyList[0].id != null && ToPartyList[0].id != 'undefined')
                Xrm.Page.getAttribute("to").setValue(ToPartyList);

            /*Regarding Object Id*/
            var regarding = [];
            regarding[0] = {};

            if (Xrm.Page.context.getQueryStringParameters().new_RegardingIdName != null && Xrm.Page.context.getQueryStringParameters().new_RegardingIdName != 'undefined') {
                regarding[0].name = Xrm.Page.context.getQueryStringParameters().new_RegardingIdName;
            }
            if (Xrm.Page.context.getQueryStringParameters().new_RegardingId != null && Xrm.Page.context.getQueryStringParameters().new_RegardingId != 'undefined') {
                regarding[0].id = Xrm.Page.context.getQueryStringParameters().new_RegardingId;
            }
            if (Xrm.Page.context.getQueryStringParameters().new_RegardingIdType != null && Xrm.Page.context.getQueryStringParameters().new_RegardingIdType != 'undefined') {
                regarding[0].entityType = Xrm.Page.context.getQueryStringParameters().new_RegardingIdType;
            }
            if (regarding[0] != null && regarding[0].id != null && regarding[0].id != 'undefined')
                Xrm.Page.getAttribute("regardingobjectid").setValue(regarding);
        }
    },

};
·         In Contact form event when want to open email form, we need to pass these query string parameters which are defined above
var contactScript = {
    openEmail: function () {
       
        var parameters = {};
        /*Custom querystring parameter*/

        parameters["new_ToType"] = Xrm.Page.data.entity.getEntityName();
        parameters["new_ToName"] = Xrm.Page.getAttribute("fullname").getValue();
        parameters["new_ToId"] = Xrm.Page.data.entity.getId(); // current contact Id

        parameters["new_RegardingIdType"] = Xrm.Page.context.getQueryStringParameters().etc;
        parameters["new_RegardingIdName"] = Xrm.Page.getAttribute("fullname").getValue();
        parameters["new_RegardingId"] = Xrm.Page.data.entity.getId(); // current contact Id

        var windowsOptions = {};
        windowsOptions["openInNewWindow"] = true;

        Xrm.Utility.openEntityForm("email",null, parameters,  windowsOptions);
    },
   
};


·         Save all changes and publish.

·         Now when Email Form opened from Contact entity, using Xrm.Utiltiy, we will see To, and Regarding lookup is already set. 




7 comments:

  1. Hello Mahadeo,

    I am regular viewer of your posts and it helped me may times. I was reading this post and I would like to know if it is possible in CRM 2013 to change the sender of an email, the "From" field?

    What we are trying to achieve is not to introduce Queue or New Mailbox (separate user) to Send Specific emails to a Static or Dynamic Marketing List. Also, Sender could be different for Different marketing lists, like dept head of each service group.

    So, we are thinking instead introducing a mailbox we could use the Secondary email of the User in the From Field automatically or atleast he or she could change the From field. But changing From field requires additional security setup may be, not explored that and may not be feasible because of Security Complications.

    I can see Admin has rights to change the From field not Normal Access rights users.

    So, we would like to do is User A has a primary email field, he logs into CRM using windows security against AD so he has not to worry about logging in logging out. Now, his primary email is "abc@abc.com" but when he creates quick campaign email activity the From field should show his secondary email like "no-reply@abc.com" and recipient should see sender as "no-reply@abc.com"

    Is it possible to change the From field to Secondary email of that user while creating only Quick Create email campaigns not regular emails. Regarding will be set to the Quick campaigns, so no worries. Can it be done without Plugin and with minimal security changes or introducing a new role (Additional Security Changes)

    Please help me pointing to any blogs or sample code or some idea that could be implemented to do so.

    Please reply.

    Thank you.

    ReplyDelete
  2. It is possible to use these parameters to set the regarding field:
    parameters.pId = Xrm.Page.data.entity.getId();
    parameters.pName = Xrm.Page.data.entity.getPrimaryAttributeValue();
    parameters.pType = 112;

    ReplyDelete
  3. Hi Mahadeo,

    You mean that we have to create parameters on both forms? on Contact/Lead... and in the activity? PhoneCall, email?

    ReplyDelete
  4. This article is an appealing wealth of informative data that is interesting and well-written. I commend your hard work on this and thank you for this information. You’ve got what it takes to get attention.
    PIPEDRIVE

    ReplyDelete
  5. THANK YOU FOR THE INFORMATION
    PLEASE VISIT US
    CRM Solutions



    ReplyDelete
  6. THANK YOU FOR THE INFORMATION
    PLEASE VISIT US
    CRM Solutions



    ReplyDelete