Create Script If Blank Then Else Default Value

I am trying to write a script in Opportunity that says if the pers_markets field is blank then use the comp_markets, otherwise use pers_markets, but i cant seem to get it to work. What am i missing?

var CompMarkets = CRM.GetContextInfo("Company","comp_markets");
var PersMarkets = CRM.GetContextInfo("Person","pers_markets");

if(Values('PersMarkets')!== null && Values('PersMarkets')!== '')
{
DefaultValue = PersMarkets;
}
else
{
DefaultValue = CompMarkets;
}

  • 0

    I'd imagine it is the if statement that is not right.

    if(Values('PersMarkets')!== null && Values('PersMarkets')!== '')

    Firstly, don't use single quotes around your variable names otherwise the script will treat it as a string, so Values(PersMarkets) and not Values('PersMarkets') also you should be doing || and not && because it can't be both.

    Secondly, sometimes dealing with nulls is a pain. I would do something like this var CompMarkets = CRM.GetContextInfo("Company","comp_markets") + ''; so that you will always get an empty string if there isn't anything, which makes your logic a little easier:

    DefaultValue = CompMarkets;
    
    if(Values(PersMarkets) !== '')
        DefaultValue = PersMarkets;

  • 0 in reply to Vega

    If you still get nowhere, you will need to start writing the data out to the screen. The easiest way to do that is to output the variable into the field caption so you can see it as you can't use alerts in createscripts.

  • 0

    Hi Anthony

    You don't need to use 'Values' at all - 'Values' is the collection of form values so, to give it any chance of working in a CreateScript, you would need to pass it a CRM column name (e.g. Values("pers_markets")).

    I would simply use the 'ErrorStr' to write out the values 'GetContextInfo' is giving you and take it from there (but I would assume a blank to be just that, i.e. an empty string).

    So, something like this should show you what's going on and, hopefully, set the default value at the same time (assuming these fields are text):

    var CompMarkets = CRM.GetContextInfo("Company", "comp_markets");
    var PersMarkets = CRM.GetContextInfo("Person", "pers_markets");
    
    ErrorStr = "pers_markets value: " + PersMarkets " | comp_markets value: " + CompMarkets;
    
    if (String(PersMarkets) !== "null" && String(PersMarkets) !== "")
    {
    	DefaultValue = PersMarkets;
    }
    else
    {
    	DefaultValue = CompMarkets;
    }


    Hope this helps.

  • 0

    I know you're asking about doing this in JavaScript. Personally I'm a SQL person, so would handle this as a trigger on the opportunity table, with something like the following.

    DECLARE @ID INT
    DECLARE @Comp INT
    DECLARE @Pers INT
    DECLARE @Market nVarChar(30)

    SELECT
    @ID = oppo_opportunityid, 
    @Comp = oppo_primarycompanyid,
    @Pers = oppo_primarypersonid
    FROM Inserted

    BEGIN
    SELECT @Market = COALESEC(pers_markets, comp_markets)
    FROM Company (NOLOCK)
    OUTER APPLY (Select pers_markets FROM Person (NOLOCK) WHERE pers_personid = @Pers AND pers_deleted IS NULL) PersMark
    WHERE comp_companyid = @Comp

    UPDATE Opportunity 
    SET oppo_market = @Market
    WHERE oppo_opportunityid = @ID
    END

  • 0

    Thank you all for your input!

    -Anthony