Change channel based on selected user in an onchange Workflow script ?

SUGGESTED

Hi all,

I've read as much as possible in the forum and find it strange that no one ever asked for that exactly...

During Case creation, which runs under an enabled new Case Workflow rule, I want that the Channel field is dynamically changed when the user changes the assigned user.

I supposed basically that I would solve this easily with :

1) a Create Script that sets the case_assigneduserid "Defaultvalue=CurrentUser.user_userid;"

2)an OnChange Script on the case_assigneduserid to change the case_channelid, but then I find it impossible to do that easily as I am here stuck at a client side level.

var AssignedUserID = crm.fields("case_assigneduserid").value(); //this is quite easy to retrieve the assigned user id.

But what about finding the Channel Id of this AssignedUserId ?

How can I do that within the scope of the new Case Workflow rule ?

Any help would be appreciated...

  • 0

    You need to do a client side sdata Ajax call passing the Id of the user found in the above query then read the field from the crmRecord object returned I.e crmRecord.user_primarychannelid. The client side api documentation has code detailing how to do it in principal. 

  • 0 in reply to Daniel Lewis

    Hmm I've never used Ajax with Sage CRM. I wonder what the authentication for the sdata call would look like. I imagine you wouldn't be storing the username & password in clear text.

  • 0 in reply to Gabriel Faulhaber
    SUGGESTED

    You don’t need to authenticate as the sage crm client side api takes care of it by using the session id your logon generated as part of the call. 10 lines of code is all you need. 

    help.sagecrm.com/.../sdata.html

  • 0 in reply to Daniel Lewis

    Wow this looks very elegant. It's a very clean way for OP to do this.

  • 0 in reply to Daniel Lewis

    I tried this but it's not working...

    // Retrieves a User record from within a Ticket and collects that record in var.
    function getUser(userID)
    {
        var returnedValue, successUser = function (crmRecord) {
                    returnedValue = crmRecord;
                }
            crm.sdata({
                entity: "users",
                async: false,
                id: userID,
                success: successUser
                });
          return returnedValue;
    }

    var curUserID = crm.fields("case_assigneduserid").value();
    var ChannelID = getUser(curUserID).user_primarychannelid;
    crm.fields("case_channelid").value(ChannelID);

    Could you help me ?

  • 0 in reply to PPI-VDM
    SUGGESTED

    Your code looks sound, however I think something might be going wrong when you want to return from the getUser function, it might be returning a null because of the way the sdata calls works. 

    Perhaps try something more "ancient" like this - 

    customUserObject = null;
    function getUser(userID)
    {
    crm.sdata({
    entity: "users",
    async: true,
    id: userID,
    success: function(crmRecord){customUserObject=crmRecord}
    });
    }

    so yes, there is a "global" variable for the user object, but then you can handle the getUser as a void and just call the customUserObject every time you need values from it.
    Like so - 

    getUser(33);
    var channelid = customUserObject.user_primarychannelid;

  • 0 in reply to Conrad Roux

    Sorry but it doesn't work either.

    Do I have to put all that code in the Workflow rule "New Ticket", in the action "Display field for Amendment" for the field "case_assigneduserid", in the "OnChange Script" ?

    Or somewhere else ? If so, where then ?

  • 0 in reply to PPI-VDM

    I would recommend testing in the console first, then placing it in CRM. So in the console you can just copy the code to the console and it should work fine. 

    I would not recommend putting functions in the onchange script, rather use the custom js folder for that and then call the script with onchange, if you want to. 

    It also depends when you want it to fire, on the change of the field, or on load of the screen.

  • 0 in reply to Conrad Roux

    Thank you for your advice. I must confess that I'm not used to deal with the console mode at all. I can read / write js, but I am not a developer and I don't know how to develop / debug using the right tools...

    I get the idea for the custom script by the way I will try it.

    What I want is that the script changes the team field value when I "leave" the assignedUserId field (or on the LostFocus Event of the form if you prefer). I don't if it should be better a "client side" or a "server side" script to work properly.

    Almost first time I'm stuck with that...

  • 0 in reply to PPI-VDM
    SUGGESTED

    If it is for the user select list, then you will have better luck with adding the "blur" effect when loading the page, so again it needs to be in the custom js folder (it can all be in one script file). I have had some issues with the onchange when working with the user selection list.

    so when you open the console on the correct page, try this ->

    $( "#table_fieldid" ).blur(function() {console.log("blur fired")});

    Then change the field value and click out. See if it prints the message in the console. Remember to use the inspect element as the field ID on that field can sometimes be different. See my example below of it working.

    You can see the script I added, and then the console message logging "blur fired"
    Here is the code I used -> 

    $( "#case_assigneduseridInput" ).blur(function() {console.log("blur fired")});

    From here, you can then attach and event/function to the event, so instead of firing a message, you can execute that function that I added in an earlier post.

    Hope this helps

    EDIT - When working with the custom js folder, it is always advise that you run the code of a script only when needed. You can do this with the ACT function and using an if to block execution on certain pages, otherwise it will try and run on every page, and that could break other functions in CRM. Please do some reading on this matter as it will be very important  

  • 0

    If you are still struggling with this, then you could just hide the Team field in the 'New Case' Primary Rule and add an 'Execute SQL Statement' to be fired at the end. 

    UPDATE cases SET case_channelid = (SELECT user_primarychannelid from users WHERE user_userid = #case_assigneduserid#) WHERE case_caseid = #case_caseid#

    You can add the same action on the 'Re-assign' rule.

  • 0 in reply to Sean from Spire

    You're right, and that was m first idea, but this approach does not let the current user to select another team than the user assigned default one.

    By the way I'm going to ask my client if it can be ok for them.

  • 0 in reply to PPI-VDM
    SUGGESTED

    You could then do it as a SQL trigger on Cases and compare 'Inserted' to 'Deleted' thus if the Channelid is the same in both then you can overrule it with the primary team of the assigned user, if they're different then you just keep with the value being entered via the Insert (just need something else in there to stop a general update to Cases triggering a change to Channelid, some flag field that is written to if driven by the workflow or something)