function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chris GroverChris Grover 

How can i fix the data type issue between integer and decimal when using dates in a calculation

I cannot resolve this error'Date expressions must use Integer or Long (8:45)'
trigger WarrantySummary on Case (before insert) {
   
for (Case myCase : Trigger.new){
// Set up variables to use in the summary field
 Date purchaseDate           = myCase.Product_Purchase_Date__c;
 Datetime createdDate        = myCase.CreatedDate;
 Integer warrantyDays        = myCase.Product_Total_Warranty_Days__c.intValue();
 Decimal warrantyPercentage  = purchaseDate.daysBetween(Date.today()) / warrantyDays;
 Boolean hasExtendedWarranty = myCase.Product_Has_Extended_Warranty__c;

//Populate summary field
 myCase.Warranty_Summary__c = 'Product Purchased on ' + purchaseDate + ' '
  + 'and case created on '  + createdDate + '.\n'
  + 'Warranty is for '      + warrantyDays + ' '
  + 'and is '               + warrantyPercentage +'% through its warranty period.\n'
  + 'Extended warranty: '   + hasExtendedWarranty + '\n'
  + 'Have a nice day!';
    }


}
Best Answer chosen by Chris Grover
Ajay K DubediAjay K Dubedi
Hi Chris,

* I am assuming field(Product_Total_Warranty_Days__c)'s Datatype is Number then Please use below code:

Trigger--->
 
trigger WarrantySummary on Case (before insert)
{
    for (Case myCase : Trigger.new)
    {
        // Set up variables to use in the summary field
        Date purchaseDate           = myCase.Product_Purchase_Date__c;
        Datetime createdDate        = myCase.CreatedDate;
        Integer warrantyDays        = (integer)myCase.Product_Total_Warranty_Days__c;
        Decimal warrantyPercentage  = purchaseDate.daysBetween(Date.today()) / warrantyDays;
        Boolean hasExtendedWarranty = myCase.Product_Has_Extended_Warranty__c;
        
        //Populate summary field
        myCase.Warranty_Summary__c = 'Product Purchased on ' + purchaseDate + ' '
            + 'and case created on '  + createdDate + '.\n'
            + 'Warranty is for '      + warrantyDays + ' '
            + 'and is '               + warrantyPercentage +'% through its warranty period.\n'
            + 'Extended warranty: '   + hasExtendedWarranty + '\n'
            + 'Have a nice day!';
    }
}

* If field(Product_Total_Warranty_Days__c)'s Datatype is Date then change code's line
 
(Integer warrantyDays = (integer)myCase.Product_Total_Warranty_Days__c;)   
         with Integer warrantyDays = (Integer)myCase.Product_Total_Warranty_Days__c.day();

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Ajay K DubediAjay K Dubedi
Hi Chris,

* I am assuming field(Product_Total_Warranty_Days__c)'s Datatype is Number then Please use below code:

Trigger--->
 
trigger WarrantySummary on Case (before insert)
{
    for (Case myCase : Trigger.new)
    {
        // Set up variables to use in the summary field
        Date purchaseDate           = myCase.Product_Purchase_Date__c;
        Datetime createdDate        = myCase.CreatedDate;
        Integer warrantyDays        = (integer)myCase.Product_Total_Warranty_Days__c;
        Decimal warrantyPercentage  = purchaseDate.daysBetween(Date.today()) / warrantyDays;
        Boolean hasExtendedWarranty = myCase.Product_Has_Extended_Warranty__c;
        
        //Populate summary field
        myCase.Warranty_Summary__c = 'Product Purchased on ' + purchaseDate + ' '
            + 'and case created on '  + createdDate + '.\n'
            + 'Warranty is for '      + warrantyDays + ' '
            + 'and is '               + warrantyPercentage +'% through its warranty period.\n'
            + 'Extended warranty: '   + hasExtendedWarranty + '\n'
            + 'Have a nice day!';
    }
}

* If field(Product_Total_Warranty_Days__c)'s Datatype is Date then change code's line
 
(Integer warrantyDays = (integer)myCase.Product_Total_Warranty_Days__c;)   
         with Integer warrantyDays = (Integer)myCase.Product_Total_Warranty_Days__c.day();

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer
Chris GroverChris Grover
Ajay thank you for your response.  I was able to successfully compile with your suggestion.  You were correct Product_Total_Warranty_Days__c is a Number field.  The issue I have now is that the warrantyPercentage is always returning 0 no matter what date I enter in  a case for field Product_Purchase_Date__c or number i enter in field Product_Total_Warranty_Days__c.
Chris GroverChris Grover
Fixed my math error:
Decimal warrantyPercentage  = 100*(purchaseDate.daysBetween(Date.today()) / myCase.Product_Total_Warranty_Days__c);

Thank you for your help!
Deepali KulshresthaDeepali Kulshrestha
Hi Chris,

I've gone through your requirement and you can typecast the decimal value into integer and vice-versa,see the example below:

1.Decimal to Integer:

Decimal dValue = 7.3;
Integer intConvertedValue = dValue.intValue();

2.Integer to Decimal:

Integer IntegerValue = 7;
Decimal DecConvertedValue = Decimal.valueOf(IntegerValue);


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com