简体   繁体   中英

How to convert calculated formula from salesforce to SQL

We have one Opportunity table in salesforce and this table has one calculated column called as " Is_XYZ ".

Calculated formula for " Is_XYZ " column is -

calculatedFormula: IF(
    AND(
        OR(
            AND(
                UPPER(LeadSource__c) == 'XYZ',
                DateValue(CreatedDate) > Date(2017,01,01)
            ),
            AND( Is_PQR ,
                IF(
                    Effective_Date__c <> null,
                    DateValue(CreatedDate) > Effective_Date__c ,
                    TRUE
                ),
                IF(
                    Effective_Date__c <> null,
                    Effective_Date__c <= TODAY(),
                    TRUE
                )
            )
        ),
        UPPER(MailingState) <> 'NY',
        UPPER(Lead_Sub_Source__c) <> 'PQRS' 
    ), 
    TRUE,
    FALSE
)

We have created same Opportunity table in Hive SQL and we want to write select query to calculate "Is_XYZ" column value. We have converted formula from salesforce syntax to SQL syntax. So, formula in SQL will be -

SELECT
IF(
    (
        (
            (   UPPER(LeadSource__c) == 'XYZ' AND
                CreatedDate > '2017-01-01'
            )
            OR
            (   Is_PQR AND
                IF( Effective_Date__c IS NOT NULL,
                    CreatedDate > Effective_Date__c,
                    TRUE
                )
                AND
                IF( Effective_Date__c IS NOT NULL,
                    Effective_Date__c <= current_date,
                    TRUE
                )
            )
        )
        AND (UPPER(MailingState) <> 'NY')
        AND (UPPER(Lead_Sub_Source__c) <> 'PQRS')
    ),
    TRUE,
    FALSE
) as Is_XYZ
FROM Opportunity;

Can you help me to confirm that both formulas(salesforce and SQL) are same? I mean, can you verify that both above formulas are doing same thing. I tested it on both sides(salesforce and Hive SQL) and it is behaving differently. Values for that case are -

LeadSource__c                        = abcdef
Lead_Sub_Source__c                   = klmnop
CreatedDate                          = 2019-04-02T00:06:49.000Z
MailingState                         = HI
Is_PQR                               = true
Effective_Date__c                    = 2019-04-09

For above values, salesforce displays Is_XYZ = true and hive displays Is_XYZ = false . Please help me in identifying the issue.

I can tell that the date/time arithmetic is not correct, due to time components on the values. I don't know if this is the issue with your particular bad example.

For instance:

DateValue(CreatedDate) > Date(2017,01,01)

is not equivalent to:

CreatedDate > '2017-01-01'

The equivalence would be to:

CreatedDate >= '2017-01-02'

The issue is the DateValue() which removes the time component.

Similarly,

DateValue(CreatedDate) > Effective_Date__c ,

requires a modification.

Finally, we found that there is bug in Salesforce formula.

In Salesforce formula, we are checking " Effective_Date__c <> null " which is always returning false . In Salesforce, it is preferred to use IsBlank() function than IsNull() . So, we have changed CalculatedFormula in Salesforce to correct this issue. New formula will be - IsBlank(Effective_Date__c) .

I am not a Salesforce developer, so not able to pick this bug earlier. After discussing this with Salesforce developer, we are able to find this bug which is present in system from last one year.

I found no way in Salesforce workbench to test/debug this Calculated field formula which is very frustrating.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM