web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to trigger a javascript on create of a case record?

(0) ShareShare
ReportReport
Posted on by

Hello Experts,

I have a requirement that, on creation of a case record i should trigger a javascript which updates the case fields through Action.

My JavaScript code is mentioned below.

function callaction()
{
debugger;
var Id = Xrm.Page.data.entity.getId().replace('{', '').replace('}', '');
var parameters = {};

var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/incidents("+ Id +")/Microsoft.Dynamics.CRM.new_Update_Case_Acc_details", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var data = JSON.parse(this.response);
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(parameters));
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Hi, 

    Sounds like this would be better suited as a workflow that triggers on create (call an action from workflow) or pre/post create plugin.

    If you call this from your javascript your code will not be executed if the record is created by some other way than on the form itself.

    If this must be as a js you could add an eventhandler to the createdon field as it will only change when the record is created but it is a bit hacky and I wouldn't recommend it

    function onLoad() {
            Xrm.Page.getAttribute("createdon").addOnChange(callaction);
    }


    Hope this can help you

  • Community Member Profile Picture
    on at

    Thanks for your help Jon.

    But I am unable to find the event tab to write the

    javascript---->["function onLoad() {

           Xrm.Page.getAttribute("createdon").addOnChange(callaction);

    }"]

    in my createdon field on the case form.

    Please find the attachment for more information.

    8712.event.png

  • Suggested answer
    Community Member Profile Picture
    on at

    You could the trigger the JS (which in turn trigger the Action?) by putting it on the form OnSave even. in the code, check for formContext.ui.getFormType() == 1 (Create).

  • Verified answer
    Community Member Profile Picture
    on at

    Hi, you need to add this to the form's onload event

    8345.onLoad.PNG

    There you call the Xrm.Page.getAttribute("createdon").addOnChange(callaction) which registers the "callaction" function to be called when the "createdon" field changes.

    More about addOnChange here

  • Community Member Profile Picture
    on at

    Hi Jon,

    Thanks for your help.

    But even now its not working.

    Now let me explain the whole requirement :

    I am creating a case through Email then I am calling a action which updates the account details of the contact(email sender) on the case which is created through email using javascript.

    The account details should updated when the case is created through email.

    All the functionality is working fine , but what ever condition i am checking it is not getting the account details updated when the case was created.

    But it is working if once i open the case after creation and if i load again or if i save again.  

    Can you give a solution for this issue.

    Thanks for your time.

  • Suggested answer
    Community Member Profile Picture
    on at

    Have you taken a look at calling your action with workflow.

    Then you could create a workflow that triggers on create for cases and it should always run when a case is created even if it is by an external source.

    Think this approach is more convenient and relies less on code. 

    Otherwise if I am not misunderstanding, couldn't you just set the account details for that case when you are creating it ?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans