Any ideas on why I can't get an OnChange script to work?

I have a custom field on the opportunity called 'Event' which is a selection field that contains a year value.

If I add the following to this fields OnChange event it works fine:

myStr = this.value;
const currentYear = new Date().getFullYear();

if (myStr.includes(currentYear))
{
oppo_source.value = 'Web';
}

But what I want to do is set the oppo_source value to be the same as the pers_source value, so I amended the line to:

oppo_source.value = CRM.GetContextInfo("person","pers_source");

But it doesn't change the Source.

Any suggestions appreciated.

  • 0

    Hi

    You can try with the CRM client-side API.

    Here are some examples of how you can assign values to a CRM field in a “On change” or “Custom Content”  scripts

    Sage CRM client-side API reference

        // Sets the comp_name field value to ACME Inc.

        crm.fields("comp_name").val("ACME Inc.");

     

    in your case it should be

        crm.fields("oppo_source").val(CRM.GetContextInfo("person","pers_source"));

    Hope this helps

  • 0 in reply to TinoJovani

    Hi, replaced my line with your suggestion and the source field doesn't change.
    Do I need this to be a script in Custom Content that is called from the On Change event?

  • 0 in reply to RBC_1

    No you don't. Yo can do it but isn't necessary. You cant put the code at the on-change content.

    So here must be something else that is preventing this from work. Check this points.

    • The field oppo_source should be on screen an be in edit mode. In both cases the field doesn't exists on screen and any client side script affecting the field will not work. 
    • Check console errors: right click on the mouse an select inspect. Check if there are any errors on the console tab: 

    The debugging tools on the browsers are a powerful tool that can help me to find boring errors. 

    Let me know it this works

  • 0 in reply to TinoJovani

    Hi, source is on screen and will get updated if I use the original script to 'Web' if I select an event with 2025 in it.
    The inspect shows:


    crm.fields(" oppo_source").val(crm.getcontextinfo("person","pers_source"));="" 


    But no error or value change on the oppo_source!?!?

    Thanks for assistance.

  • 0 in reply to RBC_1

    Hi again.

    I apologize by not detecting before, but there are some mistakes in your code. 

    crm.fields("_oppo_source").val(crm.getcontextinfo("person","pers_source"));="" 

    • javascript is case sensitive and you must respect it carefully. The correct syntax is CRM.GetContextInfo
    • There is an unwanted blank space before  oppo_source: crm.fields(" oppo_source")
    • After the end of the line, after the semi colon  this text appears ="" ,  

    When coding in JavaScript you should type very carefully.

    Anyway, the above code will not work, the problem is more complex and require some extra coding.

    Finally, I realized you are using CRM.GetContextInfo in a client-side script. It will not work.

    CRM.GetContextInfo is a function only available at server-side (On Create / Validate Scripts). You cannot use this on "onChange" scripting.

    Instead you must use AJAX or sData API’s to retrieve the actual value from the server.

    Put this code on the custom Content box:


    //onchange script

    function SetOppoSource(){

    var SetValues = function(person){
    crm.fields("oppo_source").val(person.pers_source);
    //if the above code doesnt work try with jquery
    //$("#oppo_source").val(person.pers_source); //set the value
    }

    //your code starts here
    myStr = this.value;
    const currentYear = new Date().getFullYear();

    if (myStr.includes(currentYear))
    {

    var personid = crm.fields("oppo_primarypersonid").val(); //retrieve the cuirrent value of the person id
    // make the sdata call to retrieve the full person record stored in the database
    crm.sdata({
    entity: "person",
    id: personid,
    success: SetValues // if the call succeed if will call this function to process the results. Note thta the function is defined above
    });
    }
    }

    Then call the function SetOppoSource()  in the onchange box of your custom field.

    Hope this helps.

    PS: I not tested this code, so it will be some mistakes from my side.