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

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...

Parents
  • 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. 

  • 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.

  • 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;

  • 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 ?

  • 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.

  • 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...

  • 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  

Reply
  • 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  

Children
No Data