CRM Create Script - change the default value based on an IF statement

SOLVED

I've been scratching my head on this one for the past couple of days and wonder if anyone can help? I've seen a couple of similar queries here but when I've tried to implement the results I've not had any luck. 

I'm trying to set the default value of a fee field to 150 when the loan type and loan purpose meet specific criteria, so far I've had no luck! 

Below is what I've tried using most recently. 

var Application = CRM.FindRecord('application',Value('appl_applicationid'));
var loanType = CRM.GetContextInfo("Application","appl_loantype");
if (Application.appl_loan_purpose == 'Purchase' && loanType == 'Buy')
{
	DefaultValue = 150;
}
else
{
	DefaultValue = 100;
}

Thanks! 

James.

  • 0

    If I recall correctly, you cannot use GetContextInfo with a custom entity.  If you switch this out to an SQL query, it should work.

    Hope this helps!!

  • 0 in reply to Michele Gaw

    You can as I have used it in the past, for example I use the following on CreateScript to control the searching on an Adv. Search Select

    ​var myrecord = CRM.GetContextInfo("Project_Milestone","prmi_opportunityid");
    SearchSQL = "rebr_opportunityid=" + myrecord;

    I'm weak on JavaScript but James's script doesn't 100% make sense to me - the var gets the ID from appl_applicationid but doesn't do anything with it, then in the IF I'm not shaw how it is gettting the appl_loan_purpose field, is this not confusing. shouldn't it be something like 

    var Purpose = CRM.GetContecxtInfo("Application","appl_load_purpose");
    var loanType = CRM.GetContextInfo("Application","appl_loantype");
    if (Purpose == 'Purchase' && loanType == 'Buy')
    ......
  • 0 in reply to Matthew Shaw

    Thank you Michele & Matthew for your replies, based on this I've tried;

    var purpose = CRM.GetContextInfo("Application","appl_loan_Purpose");
    var loanType = CRM.GetContextInfo("Application","appl_loantype");
    if (purpose =='Purchase' && loanType == 'Buy')
    {
    	DefaultValue = 150;
    }
    else
    {
    	DefaultValue = 100;
    }

    but, when the entity is created the default value doesn't change. 

    I'm new to javascript so please excuse any rookie mistakes! 

  • 0 in reply to James Bolton

    Sorry!  I should have clarified as to whether your entity "Application" is a primary or secondary entity.  It is a secondary entity that cannot use GetContextInfo()

    "Secondary entities are technically never in context so the GetContextInfo method doesn't work for these entities. "

    Please refer to the this article for additional details...

    https://www.sagecity.com/sage-global-solutions/sage-crm/b/sage-crm-hints-tips-and-tricks/posts/accessing-information-about-secondary-entities-with-getcontextinfo

    Again, I should have looked at what you are trying to do in more detail as I think you are actually trying to pull values off the "screen" before record is commited rather than pulling data off of a commited record since your are using "Create Script".  If I am understanding that correctly, you will want to user the Values collection.  :-)

    Something like this...

    Therefore, you will put this in the create script field for the "fee" field. :-)
    I am assuming that the purpose and type are entered as the application record is being created. I am
    also assuming the Purpose and Type are both fields on the Application entity.

    var loanPurpose = Values(appl_loan_purpose);
    var loanType = Values(appl_loantype);
    if (loanPurpose == 'Purchase' && loanType == 'Buy')
    {
    DefaultValue = 150;
    }
    else
    {
    DefaultValue = 100;
    }

  • 0 in reply to Michele Gaw

    Here is a link to more details on the Values() Collection

    www.sagecity.com/.../the-values-collection

  • 0 in reply to Michele Gaw

    Thank you so much for your answers, I have now done it...the reason it wasn't working when I was trying it was because the create script was being overrriden by ASP! which if I had known it would have been nice! 

  • 0 in reply to James Bolton

    Glad you were able to work it out.  Hope the info was helpful.  Based on your response, it sounds like you were working with the create script in a workflow action and not on a data entry screen.  If you can, please provide the final script that worked for you.  That way if someone else has the same question, they have an example of what worked.  :-)  Thanks!

  • +1 in reply to Michele Gaw
    verified answer

    var sappl_loantype = NZ(Request.Form("appl_loantype"));
    var sappl_loan_purpose = NZ(Request.Form("appl_loan_purpose"));
    
    IF ((sappl_loantype == "Buy" || sappl_loantype == "Resi") && sappl_loan_purpose == "purchase")
    {
    recApplication.appl_adminfee = 149.99;
    }
    else
    {
    recApplication.appl_adminfee = 100;
    }

    This is what I used in the ASP in the end.