Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Set Error message in activity entity if contact is inactive

(1) ShareShare
ReportReport
Posted on by
Hi All,
 
I have a requirement where I need to restrict user to create meeting/chat/email/phonecall. if the related contact is inactive.
 
 
 
 
 
 
Thanks
Sandeep
Categories:
  • sandeepc Profile Picture
    on at
    Set Error message in activity entity if contact is inactive
     
    I also need to check if required attendees(contact) is inactive then dnt allow to create meeting
  • Verified answer
    Aymen CHELBI Profile Picture
    on at
    Set Error message in activity entity if contact is inactive
    Hello,
     

    To restrict users from creating activities (meeting, chat, email, phone call) when the related contact is inactive, you can modify the provided JavaScript as follows:

    Check if the form is in "Create" mode.
    Retrieve the related contact (regardingobjectid).
    Fetch the contact's state (active/inactive) using Xrm.WebApi.retrieveRecord.
    If the contact is inactive (statecode = 1), show a warning and prevent further processing.
    If the contact has an associated account, update the regarding object to that account.
    Handle Chat activity separately, ensuring the contact is added to the "To" field.

    Code Update:

    Ensure the function is async.
    Use try-catch for error handling around the API call.
    Implement an early return to stop further activity creation when the contact is inactive.
     
    Try this code : 
    async function updateRegardingInActivities(executionContext) {
        const formContext = executionContext.getFormContext();
        formContext.ui.clearFormNotification("notification");
        // Get the regarding object (the related contact)
        const regarding = formContext.getAttribute("regardingobjectid").getValue();
        // If the form is in Create mode (Form Type 1)
        if (formContext.ui.getFormType() === 1 && regarding !== null) {
            // Check if the regarding object is a contact
            if (regarding[0].entityType === "contact") {
                
                // Retrieve the related contact's status (statecode) from the system
                try {
                    const contactDetails = await Xrm.WebApi.retrieveRecord("contact", regarding[0].id, "?$select=fullname,statecode&$expand=parentcustomerid_account($select=name,accountid)");
                    // If the contact is inactive (statecode == 1)
                    if (contactDetails.statecode === 1) {
                        formContext.ui.setFormNotification("Related contact is inactive, you cannot create activity.", "WARNING", "notification");
                        return; // Prevent further processing if the contact is inactive
                    }
                    // If the contact has a related account, update the regardingobjectid to the account
                    if (contactDetails.parentcustomerid_account !== null) {
                        const accountLookup = [{
                            id: contactDetails.parentcustomerid_account.accountid,
                            name: contactDetails.parentcustomerid_account.name,
                            entityType: "account"
                        }];
                        formContext.getAttribute("regardingobjectid").setValue(accountLookup);
                    }
                    // Handle Chat activity: Add related contact to 'To' field
                    if (formContext.ui.formSelector.getCurrentItem().getLabel() === "Chat") {
                        const contactLookup = [{
                            id: regarding[0].id,
                            name: contactDetails.fullname,
                            entityType: "contact"
                        }];
                        formContext.getAttribute("to").setValue(contactLookup);
                    }
                } catch (error) {
                    console.error("Error retrieving contact details: ", error);
                    formContext.ui.setFormNotification("Error fetching contact details, please try again later.", "ERROR", "notification");
                }
            }
        }
    }
     

    This approach should prevent activity creation for inactive contacts and update the UI accordingly.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa Moderator

Leaderboard

Product updates

Dynamics 365 release plans