Sage CRM 7.2: Set DefaultValue not update at the first time

Hi,

I'm doing calculation to find remaining day of the membership. Everything looks find except when I set 'DefaultValue', it was not updated at the first time.

For instance, today date = 21 Feb and membership expiry date = 28 Feb so Days remaining should be 7 but it still display the old value (not 7). I need to click refresh then the value is updated.

Here's my code that I put in 'Create Script':

var strCompanyId = CRM.GetContextInfo("Company", "comp_companyId");
var SQLQuery = "SELECT DATEDIFF(day, GETDATE(), comp_enddate) as Value FROM Company WHERE Comp_CompanyId = " + strCompanyId;
var Query = CRM.CreateQueryObj(SQLQuery);
Query.SelectSQL();

if(!Query.eof)
{
var SQLUpd = "UPDATE Company SET comp_remainingday = " + Query("Value") + " WHERE Comp_CompanyId = " + strCompanyId;
var UpdQuery = CRM.CreateQueryObj(SQLUpd);
UpdQuery.ExecSQL();

var strRemainingDay = Query("Value");
DefaultValue = strRemainingDay;
}
else
{
ErrorStr = "No Membership expiry date";
}

I'm very new with sage CRM so anyone has any idea? I've been stuck here for a week now.


Thank you.

  • 0

    Hi,

    There was few things that needed to be handled in the code like if you run your code when the End date is blank then it crashes as there is no error handing . I have made some modifications in the code. Try it and see if this its working.

    var strCompanyId = CRM.GetContextInfo("Company", "comp_companyId");

    var SQLQuery = "SELECT DATEDIFF(day, GETDATE(), comp_enddate) as Value FROM Company WHERE Comp_CompanyId = " + strCompanyId;

    var Query = CRM.CreateQueryObj(SQLQuery);

    Query.SelectSQL();

    var strRemainingDay=new String(Query.FieldValue('Value'))

    if(strRemainingDay=="" || strRemainingDay=="null" || strRemainingDay=="undefined")strRemainingDay="0";

    if(!Query.eof)

    {

    var SQLUpd = "UPDATE Company SET comp_remainingday = " + strRemainingDay + " WHERE Comp_CompanyId = " + strCompanyId;

    var UpdQuery = CRM.CreateQueryObj(SQLUpd);

    UpdQuery.ExecSQL();

    DefaultValue = strRemainingDay;

    }

    else

    {

    ErrorStr = "No Membership expiry date";

    }

    Hope this helps!

    Regards,

    Dinesh

  • 0

    Thank you Greytrix for your reply.

    The code works but I still encounter the same issue. When I open company record at the first time, the remaining day is not updated. I have to refresh the screen to update the display value on screen.

    Any other ideas?

  • 0

    Hi,

    I have checked this point and found that below code works properly when End Date is set for the first time. However it gives below behavior when you navigate to Company which already has Remaining Date field filled.

    To get rid of this, you can use client side script code. I suggest you to perform the below steps:

    1) Create a hidden variable and set the flag in it once Remaining Days field is updated.

    2) Now you need to write an client side script which will refresh company summary based on this flag.

    Hope this helps!

    Regards,

    Dinesh

  • 0

    Hi Dinesh,

    Thank you very much for your reply.

    Could you please explain more about that hidden variable? How exactly it works?

    I already prepare for Client API side but there's one problem. How to set default for 'RemainingDay' field?

    help.sagecrm.com/.../fields.html

    According to this page, there're 2 options here, 'val' and 'value', but they said "This will only be applied in edit mode" so do you know any other option?

    Thank you.