Set Opportunity channel based on company channel whilst in a workflow

Hi.

this is the first time i've posted here, so please be gentle with me :)


i am, as the subject says, wanting to link the Company and opportunity channels when i create a new opportunity using a workflow.

if i create an opportunity whilst in the context of a company then i have the following Create Script working :-

var strStatus = eWare.GetContextInfo("company", "comp_channelid");
defaultvalue = strStatus;

however i can't figure out how to do it, when the opportunity is created outside the context of a company, and when the company has to be selected, or if the company is changed before saving the opportunity.


Sorry if this has been asked before, but i could not find it.

  • 0

    Hi,

    Which version of CRM are you using?

    If it is pre v7.1, you will need to use Ajax to get the server side info based on the value of oppo_primarycompanyId, there is an article on it here:

    https://community.sagecrm.com/partner_community/b/hints_tips_and_tricks/archive/2009/04/21/using-ajax-to-accessing-server-side-objects-client-side.aspx

    If it is post v7.1, you can use the client side api and Sdata to get the info:

    http://help.sagecrm.com/js-api/classes/sdata.html

    Alternatively, you could set this using a table level script, post insert.

    Thanks

  • 0

    HI Toby.

    Thanks for this we are currently on 7.1, but will be going to 7.3 soon.

    I will look at the sdata stuff.

  • 0

    HI,

    thanks so far, i have looked at the SDATA stuff, but am hitting the same problems. i can't get it to work on the onchange script.

    I have taken example 1 because that comes very close to what i want to do, i paste that into the onchange script of the Company advanced select, i have pasted it into the custom content box, and get errors.

    I broke the script down and added just the first line to the onchange script and got a message "CRM" is undefined.

    I feel like i am missing some important step, but have no idea what

  • 0

    I took a look at this, and this is what I have found:

    First I created a function, and placed it on the custom content:

    function Team()
    {

    var companyID = crm.fields("oppo_primarycompanyId").value();
    var successCompany = function (crmRecord) {
    crm.fields("oppo_channelId").val(crmRecord.comp_channelId);
    }
    crm.sdata({
    entity: "company",
    id: companyID,
    success: successCompany
    });

    }


    Then, on the on change of the oppo_primarychannelId I placed:

    Team();

    However, the team was not changing on change. I ran fiddler, and I can see the sdata request being made:

    So I changed my code to write to a test field, see what is going on:

    function TellMeTheTeam()

    {
    var companyID = crm.fields("oppo_primarycompanyid").value();
    var successCompany = function (crmRecord) {
    crm.infoMessage("The companies team is :" + crmRecord.comp_channelId +' '+ crmRecord.comp_name+' '+crmRecord.comp_type);
    }
    crm.sdata({
    entity: "company",
    id: companyID,
    success: successCompany
    });
    }

    So I am writing a few value to the info message, to see what I am getting out:

    So the team doesn't seem to be returned, at the moment, I am not sure why, but I am sure I am just missing something simple.

  • 0

    OK, the undefined was my fault, my code was using:

    comp_channelId

    it should be:

    comp_channelid

    I ran the URL fiddler gave me, to look at the sdata schema. Knew it'd be something simple like that.

    So my code is now:

    function TeamUpdate()

    {
    var companyID = crm.fields("oppo_primarycompanyid").value();
    var successCompany = function (crmRecord) {
    crm.fields("oppo_channelid").val(crmRecord.comp_channelid);
    }
    crm.sdata({
    entity: "company",
    id: companyID,
    success: successCompany
    });
    }


    On the screen with name: OpportunityDetailBox

    And then I call the function TeamUpdate(); on the on change of the oppo_primarycompanyId.

    However, I think there is an issue here, in that in the sdata schema, the comp_channelid is the caption:

    Trying to set the oppo_channelid to a caption in the console doesn't do anything.

    crm.fields("oppo_channelid").val("Direct Sales"); - Doesn't work

    crm.fields("oppo_channelid").val(1); - Does

  • 0

    Hi Toby,

    This would be a selected value yes?

    Rob Hurson helped me with a similar problem if this is of value to you and Toby.

    community.sagecrm.com/.../11786.aspx

  • 0

    Hi Toby,

    Thanks again for the response. I'll have a look later, but if i understand what you are saying, then it does not work anyway, because of the schema issue?

    i also tried to do a table level script which i could make work for other fields, but it did nothing for the channelid, i suspect for the same reason.

    var oppoRecord = CRM.FindRecord("Opportunity",WhereClause);

    var compRecord = CRM.FindRecord("Company","comp_companyid="+oppoRecord.oppo_primarycompanyid);

    Values("oppo_channelid")=compRecord.comp_channelid

    Values("oppo_microssoffice")=compRecord.comp_microssoffice;

    the microssoffice field was fine, but the channelid did not change.

    I've hacked an sql trigger for now, to get my boss of my back, but would prefer a more elegant solution

  • 0

    Hi,

    It will work, you would just need to loop round the document.getElementById("oppo_channelid").options for the value, based on the translation passed from the Sdata, , then set the oppo_channelid to equal its value, as opposed to its translations, as it is that that is stopping it working.

    I ran out of time to try this, but hopefully I can look at it sometime as I am interested to get it working.

    The TLS will not be working, not because of that issue, that issue is Sdata specific.

    Setting this as a delayed table level script seems to work for me:

    function PostInsertRecord()

    {

    // Handle post insert record actions here

    var Oppo = CRM.FindRecord("Opportunity",WhereClause);

    var OppoId = Oppo.oppo_opportunityId;

    var OppoRec = CRM.FindRecord("Opportunity","oppo_opportunityId = "+OppoId)

    var OppoCompId = OppoRec.oppo_primarycompanyId;

    var CompRec = CRM.FindRecord("Company","comp_companyId ="+OppoCompId);

    var Chan = CompRec.comp_channelid;


    sql = "UPDATE Opportunity SET oppo_channelId ="+Chan+" WHERE oppo_opportunityId ="+OppoId;

    UpdateQuery = CRM.CreateQueryObj(sql);
    UpdateQuery.ExecSql();

    }

  • 0

    OK, got the code working:

    function TeamUpdate()

    {

    var companyID = crm.fields("oppo_primarycompanyid").value();

    var successCompany = function (crmRecord) {

    var SdChan = crmRecord.comp_channelid;

    //Loop through the list for the oppo_channelid and get the text.

    $("#oppo_channelid > option").each(function() {

    //If it matches what is returned by sdata...

    if(this.text== SdChan)

    {

    //set the field to be the value as opposed to the text

    crm.fields("oppo_channelid").val(this.value);

    }

    });

    }

    crm.sdata({

    entity: "company",

    id: companyID,

    success: successCompany

    });

    }


    Then call that function on change of the oppo_primarycompanyid

    If anyone knows a way to get sdata to return the value as opposed to the caption, please tell me!

  • 0

    Hi Penny,

    Thanks for pointing me in the direction of that thread, thats exactly what we've experienced.

    Nice feature to code around.

    Thanks,

    Toby

  • 0

    Hi

    Alas, i dont get to see that. :(

    But thanks Toby for all your help, i'll give your latest working a try over the weekend and see how it goes.