Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum
Answered

Set From field in new email activity to related queue mailbox

Posted on by 25

I am attempting to set the "From" field in a new Email activity related to a Case. I would like to have the default "From" field be the mailbox of the queue where the Case is currently related, instead of the current user's email. 

Cases are being created automatically from emails sent to mailboxes and then routed to the appropriate queue to be worked on by users. The systems has roughly a dozen queues associated with different mailboxes. 

At a high level the requirement is: 

- User selects a Case from a queue for which they are a member

- User selects to create a new email related to the case, or clicks reply/forward to an email in the Case timeline

- On the new mail form, the From field should default to the Queue mailbox

I have been able to set the From field to a specific queue mailbox using a web resource that is triggered by the Onload event of the Email Form. However, the queue name is hardcoded in the JavaScript. I am struggling to find a way to either pass the current queue to the web resource as a parameter or pull the current queue from the ExecutionContext. 

Would appreciate any suggestions/ideas. 

Below is the code I amusing to set the From field (value in red is hard coded and I want it to be dynamic): 

function _setFromUser(executionContext) {
    var formContext = executionContext.getFormContext();
    var formType = formContext.ui.getFormType();


    var entityName = "queue";
    if (formType == 2 || formType == 1 || formType == 0) { //Return Value Client API Reference: docs.microsoft.com/.../getformtype
        Xrm.WebApi.online.retrieveMultipleRecords("queue", "?$select=queueid,name&$filter=name eq 'Facilities'").then(

            function success(results) {
                console.log(results);
                for (var i = 0; i < results.entities.length; i++) {
                    var result = results.entities[i];
                    // Columns
                    var queueid = result["queueid"]; // Guid
                    var name = result["name"]; // Text
                    formContext.getAttribute("from").setValue([
                        {
                            id: queueid,
                            name: name,
                            entityType: entityName
                        }]);
                }
            },

            function (error) {
                console.log(error.message);
            });
    }
}
  • Suggested answer
    MM-11060324-0 Profile Picture
    MM-11060324-0 11 on at
    Set From field in new email activity to related queue mailbox
    Hi there,
     
    I created a workflow with the following logic:
     
    IF email status = Draft
    CHECK email is linked to queue = Parent Activity ID (Email):Accepting Entity = [Queue]
    THEN change sender to [Shared Mailbox]
     
    Workflow works like magic!
     
    Thanks
    Marissa
     
  • RE: Set From field in new email activity to related queue mailbox

    I might have made line 6:

           if (formContext.getAttribute("regardingobjectid") && formContext.getAttribute("regardingobjectid").getValue() != null) {

    ...but it has nothing to do with the logic.  It only protects against run-time errors

    - first phrase assures that the regarding field is on the form

    - next that there is something from which line 8 can "get"

    HOWEVER, it looks to me that this does NOT limit this to case queues.  Naming the var caseId doesn't make it a caseid.  It could be an accountid, contactid, opportunityid &c.

    ALSO, seems that you had better be sure your regarding object hasn't gotten itself into multiple queues since running the array the way you do guarantees you get the last queue in results NOT necessarily the queue from which the user is responding.  And if every queue is not rigged up for outbound emailing, that could cause problems.

    Of course to answer your question, if I understand it, if the email is regarding something NOT IN A QUEUE, there will be no queueitem, the results.length will be 0 and the loop will never execute -- From: will not be updated.

  • jazucco Profile Picture
    jazucco 25 on at
    RE: Set From field in new email activity to related queue mailbox

    I'm glad you found this helpful.

    In our implementation, we only want this behavior (setting the email from to be the Queue email address) for emails that are related to Cases. The check for whether regardingobjectid is null is to check to see if we are sending an email related to (regarding) a Case. If we are not, then we don't set the From field to the Queue email address, and it defaults to the logged in user.

    Hope this clarifies.

  • AZWildcat1290 Profile Picture
    AZWildcat1290 47 on at
    RE: Set From field in new email activity to related queue mailbox

    Hi jazucco!

    This code is great! Thank you for sharing it! One question i have is (newbie to javascript) what does the check for null in the regarding do? We have a scenario where we will have emails come into the queue that we wont necessarily create a case for but will still need to send a reply to.  When i tested this scenario the From email address didnt update like it did when i created an email from a case that was inside of a queue. Any suggestions?

    Thanks so much!

    Stephanie

  • jazucco Profile Picture
    jazucco 25 on at
    RE: Set From field in new email activity to related queue mailbox

    This works great. I did make one change so that I can use the same email form whether an email is related to a queue item or not.

    I added a check to see if the "regarding item" is null before I search for the queue.

    if (formContext.getAttribute("regardingobjectid").getValue() != null)

    I also removed formtype 2 from the check. This allows the user to manually change the From field and not have it revert back to the queue on save or send.

    if (formType == 1 || formType == 0)

    So the full code now looks like this:

    function _setFromUser(executionContext) {
        var formContext = executionContext.getFormContext();
        var formType = formContext.ui.getFormType();
    
        if (formType == 1 || formType == 0) { //Return Value Client API Reference removed formType == 2 from check: docs.microsoft.com/.../getformtype
            if (formContext.getAttribute("regardingobjectid").getValue() != null) {
                //Get case id
                var caseId = formContext.getAttribute("regardingobjectid").getValue()[0].id;
    
                //Retrieve queue item to get queue
                Xrm.WebApi.retrieveMultipleRecords("queueitem", "?$select=_queueid_value&$filter=_objectid_value eq "   caseId).then(
                    function success(results) {
                        console.log(results);
                        for (var i = 0; i < results.entities.length; i  ) {
                            var result = results.entities[i];
                            // Columns
                            var queueitemid = result["queueitemid"]; // Guid
                            var queueid = result["_queueid_value"]; // Lookup
                            var queueid_formatted = result["_queueid_value@OData.Community.Display.V1.FormattedValue"];
                            var queueid_lookuplogicalname = result["_queueid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
    
                            //populate email from 
                            formContext.getAttribute("from").setValue([
                                {
                                    id: queueid,
                                    name: queueid_formatted,
                                    entityType: queueid_lookuplogicalname
                                }]);
                        }
                    },
                    function (error) {
                        console.log(error.message);
                    }
                );
            }
        }
    }

  • Verified answer
    Leah Ju Profile Picture
    Leah Ju Microsoft Employee on at
    RE: Set From field in new email activity to related queue mailbox

    Hi jazucco,

    If you want to get current queue as a parameter, you can refer to the following steps:

    1.Clicks reply/forward to an email in the Case timeline.

    The Regarding field of email will be populated with case automatically, so you can use the regarding value to get case guid.

    var caseId = formContext.getAttribute(“regardingobjectid”).getValue()[0.id;

    2.The queue where the Case is currently related.

    When the case has added to one queue, one 'queue item' record will be created at the same time, which can be linked to the case and the queue.

    pastedimage1676451366601v1.png

    So you need retrieve queue items based on case guid firstly, then you can get the related queue record.

    Example Code:

    function _setFromUser(executionContext) {
        var formContext = executionContext.getFormContext();
        var formType = formContext.ui.getFormType();
    
        if (formType == 2 || formType == 1 || formType == 0) { //Return Value Client API Reference: docs.microsoft.com/.../getformtype
    
            //Get case id
            var caseId = formContext.getAttribute("regardingobjectid").getValue()[0].id;
    
            //Retrieve queue item to get queue
            Xrm.WebApi.retrieveMultipleRecords("queueitem", "?$select=_queueid_value&$filter=_objectid_value eq "   caseId).then(
                function success(results) {
                    console.log(results);
                    for (var i = 0; i < results.entities.length; i  ) {
                        var result = results.entities[i];
                        // Columns
                        var queueitemid = result["queueitemid"]; // Guid
                        var queueid = result["_queueid_value"]; // Lookup
                        var queueid_formatted = result["_queueid_value@OData.Community.Display.V1.FormattedValue"];
                        var queueid_lookuplogicalname = result["_queueid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
    
                        //populate email from 
                        formContext.getAttribute("from").setValue([
                            {
                                id: queueid,
                                name: queueid_formatted,
                                entityType: queueid_lookuplogicalname
                            }]);
                    }
                },
                function (error) {
                    console.log(error.message);
                }
            );
        }
    }

    pastedimage1676453337931v2.png

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans