Klevr Product Documentation logo

Update Klevr Gifts due to Platform Cost change

In order to build Platform Cost into the system with minimal impact, we've introduced a new field on gift called klevr_useramount and have swapped to...

In order to build Platform Cost into the system with minimal impact, we've introduced a new field on gift called klevr_useramount and have swapped to using this for user input, and not klevr_amount. Klevr amount is now system calculated, and can include things like platform cost (if specified).

Please update your forms to use klevr_useramount and make klevr_amount readonly.

Here's a script you can run to backfill the klevr_useramount fields with the correct amount. Open your target Dynamics environment, then opend the browser debugger, and paste this code on the console. It will search for gifts where the amount contains a value and the user amount is blank, and update them, giving progress along the way in the console:

This script directly updates records in your environment. Test it in a non-production environment first.
(async function() {
    try {
        entityName = "klevr_gift";
        console.log("Starting bulk update for entity: " + entityName);
        
        // Build the query - fetch records where klevr_useramount is null and klevr_amount is not null
        var fetchXml = [
            "<fetch>",
            "  <entity name='" + entityName + "'>",
            "    <attribute name='" + entityName + "id' />",
            "    <attribute name='klevr_amount' />",
            "    <attribute name='klevr_useramount' />",
            "    <filter type='and'>",
            "      <condition attribute='klevr_useramount' operator='null' />",
            "      <condition attribute='klevr_amount' operator='not-null' />",
            "    </filter>",
            "  </entity>",
            "</fetch>"
        ].join("");
        
        console.log("Fetching records...");
        
        // Execute FetchXML query
        var encodedFetchXml = encodeURIComponent(fetchXml);
        var queryUrl = "/api/data/v9.2/" + entityName + "s?fetchXml=" + encodedFetchXml;
        
        var response = await fetch(queryUrl, {
            method: "GET",
            headers: {
                "OData-MaxVersion": "4.0",
                "OData-Version": "4.0",
                "Accept": "application/json",
                "Content-Type": "application/json; charset=utf-8",
                "Prefer": "odata.include-annotations=\"*\""
            }
        });
        
        if (!response.ok) {
            throw new Error("Failed to fetch records: " + response.statusText);
        }
        
        var data = await response.json();
        var records = data.value;
        
        console.log("Found " + records.length + " record(s) to update");
        
        if (records.length === 0) {
            alert("No records found where klevr_useramount is null and klevr_amount has a value.");
            return;
        }
        
        // Confirm before proceeding
        var confirmUpdate = confirm("Found " + records.length + " record(s) to update.\n\nDo you want to proceed with updating klevr_useramount from klevr_amount?");
        
        if (!confirmUpdate) {
            console.log("Update cancelled by user");
            return;
        }
        
        // Update each record
        var successCount = 0;
        var errorCount = 0;
        var errors = [];
        
        for (var i = 0; i < records.length; i++) {
            var record = records[i];
            var recordId = record[entityName + "id"];
            var amountValue = record.klevr_amount;
            
            console.log("Updating record " + (i + 1) + "/" + records.length + " (ID: " + recordId + ")...");
            
            try {
                var updateUrl = "/api/data/v9.2/" + entityName + "s(" + recordId + ")";
                var updateData = {
                    "klevr_useramount": amountValue
                };
                
                var updateResponse = await fetch(updateUrl, {
                    method: "PATCH",
                    headers: {
                        "OData-MaxVersion": "4.0",
                        "OData-Version": "4.0",
                        "Accept": "application/json",
                        "Content-Type": "application/json; charset=utf-8"
                    },
                    body: JSON.stringify(updateData)
                });
                
                if (updateResponse.ok) {
                    successCount++;
                    console.log("✓ Record " + (i + 1) + " updated successfully (amount: " + amountValue + ")");
                } else {
                    errorCount++;
                    var errorText = await updateResponse.text();
                    console.error("✗ Failed to update record " + (i + 1) + ": " + errorText);
                    errors.push("Record " + (i + 1) + " (ID: " + recordId + "): " + errorText);
                }
                
            } catch (error) {
                errorCount++;
                console.error("✗ Error updating record " + (i + 1) + ":", error.message);
                errors.push("Record " + (i + 1) + " (ID: " + recordId + "): " + error.message);
            }
        }
        
        // Show summary
        var summary = "Update Complete!\n\n" +
                     "Total records processed: " + records.length + "\n" +
                     "✓ Successfully updated: " + successCount + "\n" +
                     "✗ Failed: " + errorCount;
        
        console.log("\n" + "=".repeat(50));
        console.log(summary);
        
        if (errors.length > 0) {
            console.log("\nErrors:");
            errors.forEach(function(err) {
                console.log("  - " + err);
            });
        }
        
        alert(summary);
        
    } catch (error) {
        console.error("✗ Script error:", error);
        alert("Error running script: " + error.message);
    }
})(); 
 

Last updated on