ShabSid / Mycoode

0 stars 0 forks source link

Js #2

Open ShabSid opened 2 days ago

ShabSid commented 2 days ago

async function syncAllTesters(primaryControl) { try { const mainTable = "main_table_name"; // Replace with your main table logical name const relationshipTable = "relationship_table_name"; // Replace with your relationship table logical name const relationshipName = "relationship_name"; // Replace with your relationship schema name

    const batchSize = 5000; // Max records per batch in Dataverse
    let allRecords = [];
    let nextLink = null;

    // Fetch all records from the main table
    do {
        const query = nextLink || `/api/data/v9.2/${mainTable}?$top=${batchSize}`;
        const response = await Xrm.WebApi.online.retrieveMultipleRecords(mainTable, query);
        allRecords = allRecords.concat(response.entities);
        nextLink = response["@odata.nextLink"]; // Check if there's more data
    } while (nextLink);

    // Fetch associated testers and update the main table
    for (const record of allRecords) {
        const recordId = record["main_tableid"]; // Replace with the primary key of your main table

        // Fetch associated records
        const associatedResponse = await Xrm.WebApi.online.retrieveMultipleRecords(
            relationshipTable,
            `?$filter=${relationshipName}/main_table_id eq '${recordId}'`
        );

        // Extract tester names
        const testerNames = associatedResponse.entities.map((entity) => entity.tester_name); // Replace 'tester_name'

        // Update the main table with tester names
        if (testerNames.length > 0) {
            await Xrm.WebApi.updateRecord(mainTable, recordId, {
                testers: testerNames.join("; ") // Assuming 'testers' is a text field
            });
        }
    }

    // Notify the user of completion
    Xrm.Navigation.openAlertDialog({ text: "Testers synced successfully for all records." });
} catch (error) {
    console.error("Error syncing testers:", error);
    Xrm.Navigation.openErrorDialog({ message: "Failed to sync testers for all records." });
}

}

ShabSid commented 2 days ago

function syncTesters(executionContext) { // Define table and relationship details const acticeDetailsTable = "actice_details"; const relationshipName = "clss_active_clss_user"; const testersColumn = "testers";

// Get the Web API reference
const webApi = Xrm.WebApi;

// Fetch all records from actice_details
webApi.retrieveMultipleRecords(acticeDetailsTable, "?$select=activityid").then(function (records) {
    if (records.entities.length > 0) {
        // Iterate through each actice_details record
        records.entities.forEach(record => {
            const activityId = record.activityid;

            // Fetch related testers through the relationship
            const relatedRecordsQuery = `/${acticeDetailsTable}(${activityId})/${relationshipName}?$select=tester_name`;

            webApi.retrieveMultipleRecords(relationshipName, relatedRecordsQuery).then(function (relatedRecords) {
                if (relatedRecords.entities.length > 0) {
                    // Collect tester names
                    const testerNames = relatedRecords.entities.map(r => r.tester_name).join(", ");

                    // Update the actice_details record
                    const updateData = {};
                    updateData[testersColumn] = testerNames;

                    webApi.updateRecord(acticeDetailsTable, activityId, updateData).then(function () {
                        console.log(`Updated testers for activity ID: ${activityId}`);
                    }).catch(function (error) {
                        console.error(`Error updating testers for activity ID: ${activityId}`, error.message);
                    });
                }
            }).catch(function (error) {
                console.error(`Error fetching related testers for activity ID: ${activityId}`, error.message);
            });
        });
    } else {
        console.log("No actice_details records found.");
    }
}).catch(function (error) {
    console.error("Error fetching actice_details records:", error.message);
});

}

ShabSid commented 1 day ago

function syncTesters(executionContext) { // Define table and relationship details const acticeDetailsTable = "actice_details"; // Main table logical name const relationshipName = "clss_active_clss_user"; // Relationship logical name const testersColumn = "cliatech_actualtesters"; // Column in the main table to update with tester names const recordIdColumn = "cliatech_active_scriptdetailsid"; // Record ID column in the main table const testerNameColumn = "cliatech_name"; // Tester name column in the relationship table

// Get the Web API reference
const webApi = Xrm.WebApi;

// Fetch all records from actice_details
webApi.retrieveMultipleRecords(acticeDetailsTable, `?$select=${recordIdColumn}`).then(function (records) {
    if (records.entities.length > 0) {
        // Iterate through each record in the actice_details table
        records.entities.forEach(record => {
            const recordId = record[recordIdColumn]; // Get the ID of the current record

            // Fetch related testers using the relationship
            const relatedRecordsQuery = `/${acticeDetailsTable}(${recordId})/${relationshipName}?$select=${testerNameColumn}`;

            webApi.retrieveMultipleRecords(relationshipName, relatedRecordsQuery).then(function (relatedRecords) {
                if (relatedRecords.entities.length > 0) {
                    // Collect tester names into a comma-separated string
                    const testerNames = relatedRecords.entities.map(r => r[testerNameColumn]).join(", ");

                    // Prepare the data for updating the testers column
                    const updateData = {};
                    updateData[testersColumn] = testerNames;

                    // Update the current actice_details record
                    webApi.updateRecord(acticeDetailsTable, recordId, updateData).then(function () {
                        console.log(`Updated testers for record ID: ${recordId}`);
                    }).catch(function (error) {
                        console.error(`Error updating testers for record ID: ${recordId}`, error.message);
                    });
                } else {
                    console.log(`No related testers found for record ID: ${recordId}`);
                }
            }).catch(function (error) {
                console.error(`Error fetching related testers for record ID: ${recordId}`, error.message);
            });
        });
    } else {
        console.log("No records found in actice_details.");
    }
}).catch(function (error) {
    console.error("Error fetching actice_details records:", error.message);
});

}

ShabSid commented 1 day ago

function syncTesters(executionContext) { // Define table and relationship details const acticeDetailsTable = "actice_details"; // Main table logical name const relationshipName = "clss_active_clss_user"; // Relationship logical name const testersColumn = "cliatech_actualtesters"; // Column in the main table to update with tester names const recordIdColumn = "cliatech_active_scriptdetailsid"; // Record ID column in the main table const testerNameColumn = "cliatech_name"; // Tester name column in the relationship table

// Get the Web API reference
const webApi = Xrm.WebApi;

// Fetch all records from actice_details
webApi.retrieveMultipleRecords(acticeDetailsTable, `?$select=${recordIdColumn}`).then(function (records) {
    if (records.entities.length > 0) {
        // Iterate through each record in actice_details
        records.entities.forEach(record => {
            const recordId = record[recordIdColumn]; // Get the ID of the current record

            // Fetch related testers using the relationship
            const relatedRecordsQuery = `/${acticeDetailsTable}(${recordId})/${relationshipName}?$select=${testerNameColumn}`;

            webApi.retrieveMultipleRecords("", relatedRecordsQuery).then(function (relatedRecords) {
                if (relatedRecords.entities.length > 0) {
                    // Collect tester names into a comma-separated string
                    const testerNames = relatedRecords.entities.map(r => r[testerNameColumn]).join(", ");

                    // Prepare the data for updating the testers column
                    const updateData = {};
                    updateData[testersColumn] = testerNames;

                    // Update the current actice_details record
                    webApi.updateRecord(acticeDetailsTable, recordId, updateData).then(function () {
                        console.log(`Updated testers for record ID: ${recordId}`);
                    }).catch(function (error) {
                        console.error(`Error updating testers for record ID: ${recordId}`, error.message);
                    });
                } else {
                    console.log(`No related testers found for record ID: ${recordId}`);
                }
            }).catch(function (error) {
                console.error(`Error fetching related testers for record ID: ${recordId}`, error.message);
            });
        });
    } else {
        console.log("No records found in actice_details.");
    }
}).catch(function (error) {
    console.error("Error fetching actice_details records:", error.message);
});

}

ShabSid commented 1 day ago

function syncTesters(executionContext) { // Define table and column details const mainTable = "cliatech_active_scriptdetailses"; // Main table const relationshipName = "cliatech_CLSSUser_cliatech_Active_ScriptDetails_cliatech_Active_ScriptDetails"; // Relationship table's name const testersColumn = "cliatech_actualtesters"; // Column in the main table to be updated const recordIdColumn = "cliatech_active_scriptdetailsid"; // Record ID column in the main table const testerNameColumn = "cliatech_name"; // Column in the relationship table for tester names

// Get the Web API reference
const webApi = Xrm.WebApi;

// Fetch all records from the main table
webApi.retrieveMultipleRecords(mainTable, `?$select=${recordIdColumn}&$expand=${relationshipName}(${testerNameColumn})`)
    .then(function (records) {
        if (records.entities.length > 0) {
            // Iterate through each record in the main table
            records.entities.forEach(record => {
                const recordId = record[recordIdColumn]; // Get the ID of the current record

                // Fetch related testers through the relationship
                const relatedRecords = record[relationshipName];

                if (relatedRecords && relatedRecords.length > 0) {
                    // Collect tester names into a comma-separated string
                    const testerNames = relatedRecords.map(r => r[testerNameColumn]).join(", ");

                    // Prepare the data for updating the testers column
                    const updateData = {};
                    updateData[testersColumn] = testerNames;

                    // Update the current main table record
                    webApi.updateRecord(mainTable, recordId, updateData)
                        .then(function () {
                            console.log(`Successfully updated testers for record ID: ${recordId}`);
                        })
                        .catch(function (error) {
                            console.error(`Error updating testers for record ID: ${recordId}`, error.message);
                        });
                } else {
                    console.log(`No related testers found for record ID: ${recordId}`);
                }
            });
        } else {
            console.log("No records found in the main table.");
        }
    })
    .catch(function (error) {
        console.error("Error fetching records from the main table:", error.message);
    });

}

ShabSid commented 1 day ago

function syncTesters(executionContext) { const mainTable = "cliatech_active_scriptdetailses"; // Main table name const relationshipName = "cliatech_CLSSUser_cliatech_Active_ScriptDetails_cliatech_Active_ScriptDetails"; // Relationship name const testersColumn = "cliatech_actualtesters"; // Tester column in the main table const recordIdColumn = "cliatech_active_scriptdetailsid"; // Unique identifier in the main table const testerNameColumn = "cliatech_name"; // Tester name column in the relationship table

const webApi = Xrm.WebApi;

// Step 1: Fetch all records from the main table
webApi.retrieveMultipleRecords(mainTable, `?$select=${recordIdColumn}`)
    .then(function (records) {
        if (records.entities.length > 0) {
            records.entities.forEach(record => {
                const recordId = record[recordIdColumn];

                // Step 2: Fetch related testers for each record
                const relationshipEndpoint = `/${mainTable}(${recordId})/${relationshipName}`;
                webApi.retrieveMultipleRecords(relationshipName, `?$select=${testerNameColumn}`)
                    .then(function (relatedRecords) {
                        if (relatedRecords.entities.length > 0) {
                            // Collect tester names
                            const testerNames = relatedRecords.entities
                                .map(r => r[testerNameColumn])
                                .join(", ");

                            // Step 3: Update the main record with tester names
                            const updateData = {};
                            updateData[testersColumn] = testerNames;

                            webApi.updateRecord(mainTable, recordId, updateData)
                                .then(function () {
                                    console.log(`Updated record ID ${recordId} with testers: ${testerNames}`);
                                })
                                .catch(function (error) {
                                    console.error(`Error updating record ID ${recordId}`, error.message);
                                });
                        } else {
                            console.log(`No testers found for record ID: ${recordId}`);
                        }
                    })
                    .catch(function (error) {
                        console.error(`Error fetching related testers for record ID ${recordId}:`, error.message);
                    });
            });
        } else {
            console.log("No records found in the main table.");
        }
    })
    .catch(function (error) {
        console.error("Error fetching main table records:", error.message);
    });

}

ShabSid commented 3 hours ago

function syncTesters(executionContext) { const mainTable = "cliatech_active_scriptdetailses"; // Main table const relationshipName = "cliatech_CLSSUser_cliatech_Active_ScriptDetails"; // Relationship name const testersColumn = "cliatech_actualtesters"; // Target column to update const recordIdColumn = "cliatech_active_scriptdetailsid"; // Record ID column in main table const testerNameColumn = "cliatech_name"; // Tester name in related table

const webApi = Xrm.WebApi;

// Step 1: Fetch all records from the main table with expanded relationships
const query = `?$select=${recordIdColumn}&$expand=${relationshipName}($select=${testerNameColumn})`;

webApi.retrieveMultipleRecords(mainTable, query)
    .then(function (records) {
        if (records.entities.length > 0) {
            // Iterate over each record in the main table
            records.entities.forEach(record => {
                const recordId = record[recordIdColumn];

                // Check if related testers exist
                if (record[relationshipName] && record[relationshipName].length > 0) {
                    // Collect tester names
                    const testerNames = record[relationshipName]
                        .map(relRecord => relRecord[testerNameColumn])
                        .join(", ");

                    // Step 2: Update the main table with tester names
                    const updateData = {};
                    updateData[testersColumn] = testerNames;

                    webApi.updateRecord(mainTable, recordId, updateData)
                        .then(function () {
                            console.log(`Updated record ID ${recordId} with testers: ${testerNames}`);
                        })
                        .catch(function (error) {
                            console.error(`Error updating record ID ${recordId}`, error.message);
                        });
                } else {
                    console.log(`No related testers found for record ID: ${recordId}`);
                }
            });
        } else {
            console.log("No records found in the main table.");
        }
    })
    .catch(function (error) {
        console.error("Error fetching records from the main table:", error.message);
    });

}

https://org2d081997.crm.dynamics.com/api/data/v9.0/cliatech_active_scriptdetailses?$select=cliatech_active_scriptdetailsid&$expand=cliatech_CLSSUser_cliatech_Active_ScriptDetails($select=cliatech_name)

ShabSid commented 3 hours ago

function syncTesters(executionContext) { const pluralizedMainTable = "cliatech_active_scriptdetailses"; // Pluralized entity name for Web API const relationshipName = "cliatech_CLSSUser_cliatech_Active_ScriptDetails"; // Relationship schema name const testersColumn = "cliatech_actualtesters"; // Column to update const recordIdColumn = "cliatech_active_scriptdetailsid"; // Record ID column const testerNameColumn = "cliatech_name"; // Name field in related records

const webApi = Xrm.WebApi;

// Step 1: Define the query with $select and $expand
const query = `?$select=${recordIdColumn}&$expand=${relationshipName}($select=${testerNameColumn})`;

webApi.retrieveMultipleRecords(pluralizedMainTable, query)
    .then(function (records) {
        if (records.entities.length > 0) {
            // Iterate over each main table record
            records.entities.forEach(record => {
                const recordId = record[recordIdColumn];

                // Extract related testers
                if (record[relationshipName] && record[relationshipName].length > 0) {
                    const testerNames = record[relationshipName]
                        .map(relRecord => relRecord[testerNameColumn])
                        .join(", ");

                    // Step 2: Prepare update payload
                    const updateData = {};
                    updateData[testersColumn] = testerNames;

                    // Step 3: Update the main table record
                    webApi.updateRecord("cliatech_active_scriptdetails", recordId, updateData)
                        .then(function () {
                            console.log(`Updated record ID ${recordId} with testers: ${testerNames}`);
                        })
                        .catch(function (error) {
                            console.error(`Error updating record ID ${recordId}`, error.message);
                        });
                } else {
                    console.log(`No related testers found for record ID: ${recordId}`);
                }
            });
        } else {
            console.log("No records found in the main table.");
        }
    })
    .catch(function (error) {
        console.error("Error fetching records from the main table:", error.message);
    });

}

ShabSid commented 2 hours ago

function syncTesters(executionContext) { const mainTableName = "cliatech_active_scriptdetails"; // Main table logical name const testersColumn = "cliatech_actualtesters"; // Column to update const recordIdColumn = "cliatech_active_scriptdetailsid"; // Record ID column const testerNameColumn = "cliatech_name"; // Name field in related records const relationshipName = "cliatech_CLSSUser_cliatech_Active_ScriptDetails"; // Relationship schema name

const webApi = Xrm.WebApi;

// Display "Processing" notification
Xrm.Utility.showProgressIndicator("Processing... Testers are being synced.");

// Step 1: Define the query with $select and $expand
const query = `?$select=${recordIdColumn}&$expand=${relationshipName}($select=${testerNameColumn})`;

webApi.retrieveMultipleRecords(mainTableName, query)
    .then(function (records) {
        if (records.entities.length > 0) {
            const updatePromises = [];

            // Iterate over each main table record
            records.entities.forEach(record => {
                const recordId = record[recordIdColumn];

                // Extract related testers
                if (record[relationshipName] && record[relationshipName].length > 0) {
                    const testerNames = record[relationshipName]
                        .map(relRecord => relRecord[testerNameColumn])
                        .join(", ");

                    // Prepare update payload
                    const updateData = {};
                    updateData[testersColumn] = testerNames;

                    // Update the main table record and collect promises
                    updatePromises.push(
                        webApi.updateRecord(mainTableName, recordId, updateData)
                    );
                }
            });

            // Wait for all updates to complete
            Promise.all(updatePromises)
                .then(() => {
                    Xrm.Utility.closeProgressIndicator(); // Close the "Processing" message
                    Xrm.Utility.alertDialog("Tester(s) are successfully synced.");
                })
                .catch(error => {
                    Xrm.Utility.closeProgressIndicator(); // Close the "Processing" message
                    Xrm.Utility.alertDialog(`Error syncing testers: ${error.message}`);
                });
        } else {
            Xrm.Utility.closeProgressIndicator(); // Close the "Processing" message
            Xrm.Utility.alertDialog("No records found to sync testers.");
        }
    })
    .catch(function (error) {
        Xrm.Utility.closeProgressIndicator(); // Close the "Processing" message
        Xrm.Utility.alertDialog(`Error fetching records from the main table: ${error.message}`);
    });

}

ShabSid commented 2 hours ago

function syncTesters(executionContext) { const mainTableName = "cliatech_active_scriptdetails"; // Main table logical name const testersColumn = "cliatech_actualtesters"; // Column to update const recordIdColumn = "cliatech_active_scriptdetailsid"; // Record ID column const testerNameColumn = "cliatech_name"; // Name field in related records const relationshipName = "cliatech_CLSSUser_cliatech_Active_ScriptDetails"; // Relationship schema name

const webApi = Xrm.WebApi;

// Get the form context
const formContext = executionContext.getFormContext();

// Display "Processing" notification
Xrm.Utility.showProgressIndicator("Processing... Testers are being synced.");

// Step 1: Define the query with $select and $expand
const query = `?$select=${recordIdColumn}&$expand=${relationshipName}($select=${testerNameColumn})`;

webApi.retrieveMultipleRecords(mainTableName, query)
    .then(function (records) {
        if (records.entities.length > 0) {
            const updatePromises = [];

            // Iterate over each main table record
            records.entities.forEach(record => {
                const recordId = record[recordIdColumn];

                // Extract related testers
                if (record[relationshipName] && record[relationshipName].length > 0) {
                    const testerNames = record[relationshipName]
                        .map(relRecord => relRecord[testerNameColumn])
                        .join(", ");

                    // Prepare update payload
                    const updateData = {};
                    updateData[testersColumn] = testerNames;

                    // Update the main table record and collect promises
                    updatePromises.push(
                        webApi.updateRecord(mainTableName, recordId, updateData)
                    );
                }
            });

            // Wait for all updates to complete
            Promise.all(updatePromises)
                .then(() => {
                    Xrm.Utility.closeProgressIndicator(); // Close the "Processing" message
                    Xrm.Utility.alertDialog("Tester(s) are successfully synced.");

                    // Refresh the form to show the updated data
                    formContext.data.refresh(true);
                })
                .catch(error => {
                    Xrm.Utility.closeProgressIndicator(); // Close the "Processing" message
                    Xrm.Utility.alertDialog(`Error syncing testers: ${error.message}`);
                });
        } else {
            Xrm.Utility.closeProgressIndicator(); // Close the "Processing" message
            Xrm.Utility.alertDialog("No records found to sync testers.");
        }
    })
    .catch(function (error) {
        Xrm.Utility.closeProgressIndicator(); // Close the "Processing" message
        Xrm.Utility.alertDialog(`Error fetching records from the main table: ${error.message}`);
    });

}