How to create the scheduled job based on two fields

manjupawadhA
Giga Guru

@Ankur Bawiskar  Hi Hope you doing good

I have one requiremet can you please help to resolved this 

 

I need to create one notification based on the field.In afe table i have two fields total request and amount spent so A nightly job should run and compare the project total request to amount spend on project if the amount spend is greater than the total request send notification.  Set a flag to indicate that the notification has been sent and capture the date and time that the over budget notification was sent for the project.  Log this information in the Activity log for the active AFE on the project and in the Activity  Log on the Project record.

1 ACCEPTED SOLUTION

manjupawadhA
Giga Guru

hello Everyone i have tried below scheduled job its working fine

 

//var recordCount = 0;

try {

    var gr = new GlideRecord('x_asfi_afe_afe');

    gr.addEncodedQuery('amount_spentISNOTEMPTY^total_requestISNOTEMPTY^budget=true');

    gr.query();

 

    while (gr.next()) {

        try {

            var amountSpent = parseFloat(gr.getValue('amount_spent')) || 0;

            var totalRequest = parseFloat(gr.getValue('total_request')) || 0;

 

            // Skip if totalRequest is 0

            if (totalRequest === 0) continue;

 

            var percentageSpent = (amountSpent / totalRequest) * 100;

 

            if (percentageSpent >= 90) {

                recordCount++;

 

                // Format currency values

                var formattedAmountSpent = amountSpent.toLocaleString('en-US', {

                    style: 'currency',

                    currency: 'USD'

                });

                var formattedTotalRequest = totalRequest.toLocaleString('en-US', {

                    style: 'currency',

                    currency: 'USD'

                });

 

                //Log record details

                gs.info(

         "AFE Details:\n" +

         "Number: " + gr.number + "\n" +

         "Amount Spent: " + formattedAmountSpent + "\n" +

         "Total Budget: " + formattedTotalRequest + "\n" +

         "Percentage: " + percentageSpent.toFixed(2) + "%"

     );

 

                // Queue the event notification

                gs.eventQueue('afe_90_of_there_budget', gr, '');

               

                // Update the record

                // gr.setValue('over_budget', 'true');

                // gr.update();

            }

        } catch (recordError) {

            gs.error("Error processing AFE record " + gr.number + ": " + recordError);

           gr.setValue('budget','true');

        }

    }

 

} catch (error) {

    gs.error("Script execution failed: " + error);

}

View solution in original post

7 REPLIES 7

@manjupawadhA 

what debugging did you do?

are you able to find records matching that query?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@manjupawadhA 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

manjupawadhA
Giga Guru

hello Everyone i have tried below scheduled job its working fine

 

//var recordCount = 0;

try {

    var gr = new GlideRecord('x_asfi_afe_afe');

    gr.addEncodedQuery('amount_spentISNOTEMPTY^total_requestISNOTEMPTY^budget=true');

    gr.query();

 

    while (gr.next()) {

        try {

            var amountSpent = parseFloat(gr.getValue('amount_spent')) || 0;

            var totalRequest = parseFloat(gr.getValue('total_request')) || 0;

 

            // Skip if totalRequest is 0

            if (totalRequest === 0) continue;

 

            var percentageSpent = (amountSpent / totalRequest) * 100;

 

            if (percentageSpent >= 90) {

                recordCount++;

 

                // Format currency values

                var formattedAmountSpent = amountSpent.toLocaleString('en-US', {

                    style: 'currency',

                    currency: 'USD'

                });

                var formattedTotalRequest = totalRequest.toLocaleString('en-US', {

                    style: 'currency',

                    currency: 'USD'

                });

 

                //Log record details

                gs.info(

         "AFE Details:\n" +

         "Number: " + gr.number + "\n" +

         "Amount Spent: " + formattedAmountSpent + "\n" +

         "Total Budget: " + formattedTotalRequest + "\n" +

         "Percentage: " + percentageSpent.toFixed(2) + "%"

     );

 

                // Queue the event notification

                gs.eventQueue('afe_90_of_there_budget', gr, '');

               

                // Update the record

                // gr.setValue('over_budget', 'true');

                // gr.update();

            }

        } catch (recordError) {

            gs.error("Error processing AFE record " + gr.number + ": " + recordError);

           gr.setValue('budget','true');

        }

    }

 

} catch (error) {

    gs.error("Script execution failed: " + error);

}