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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

OnChange events always called twice after new record creation

(0) ShareShare
ReportReport
Posted on by 27

Does anyone else have the problem that all onChange events are called twice after a record has been newly created?

I have javascript code where the onLoad method is called twice after onSave and all addOnChange events registered twice. Thats leads to the problem, that every onChange events called twice, which causes multiple problems.

It only happens on newly created records.

Can anybody else reproduce this issue? Is this a bug?

I have the same question (0)
  • Nya Profile Picture
    29,060 on at

    Hi,

    Your description is a little difficult to understand.

    Is the separate OnChange event called twice after the record is created or is this a problem due to the presence of the OnLoad and OnSave events?

    I have tried adding a separate OnChange event to a field and I haven't reproduced the problem of it being called twice when a new record is created.

    8176.pastedimage1618281873936v1.png

    So can you describe in detail how the other events relate to the OnChange event?

     

    Looking forward to your reply.

    Best Regards,

    Nya

  • Chris Penalty Profile Picture
    27 on at

    Hi Nya,

    Thanks for your reply and sorry for my confusing description.

    The problem is that we add OnChange events in the OnLoad event and the OnLoad event is called twice after a new record is created. This leads to the problem that all Onchange events added and called twice.

    Can you reproduce this behaviour?

    Thanks,

    Chris

  • meelamri Profile Picture
    13,216 User Group Leader on at

    Hello,

    This problem is not reproducible in my environment. Can you please share your code and screenshots? This will help to better understand and solve the problem

  • Chris Penalty Profile Picture
    27 on at

    This is a snippet of my code...

        private counter = 0;
    	private counterChange = 0;
    	
        private Signature_onChange(): void
    	{
    	    this.counterChange  ;
    		console.log(`inside onchange the ${this.counterChange}. time`);
    	    const callerId = this.context.data.entity.getId();
    
    		if (callerId)
    		{
    			const signatureCode = this.crmAttr(FieldNames.SignaturePad).getValue();
    			const annotationData = {};
    			annotationData[AnnotationFieldNames.FileName] = 'Signature.jpeg';
    			annotationData[AnnotationFieldNames.IsDocument] = true;
    			annotationData[AnnotationFieldNames.MimeType] = 'image/jpeg';
    			annotationData[AnnotationFieldNames.DocumentBody] = signatureCode.replace('data:image/png;base64,', '');
    			annotationData[AnnotationFieldNames.NoteText] = 'Customer Signature';
    			annotationData[`${AnnotationFieldNames.ObjectId}_new_quote@odata.bind`] = `/new_quotes(${callerId.replace(/[{​​}​​]/g, '')})`;
    			annotationData[AnnotationFieldNames.Subject] = 'Customer Signature';
    
    			Xrm.WebApi.createRecord(EntityNames.Annotation, annotationData).then
    			(
    				(result) =>
    				{
    					console.log(`Created an annotation with id (${result.id}).`);
    				}
    			);
    			this.counterChange = 0;
    		}
    	}
        
        /**
    	 * Standard entry point
    	 */
    	public async onLoad(executionContext?: Xrm.Events.EventContext): Promise
    	{
    		this.counter  ;
    		console.log(`inside onload the ${this.counter}. time`);
    		this.crmAttr(FieldNames.SignaturePad).addOnChange(() => this.Signature_onChange());
    		
    		...more code is added here
    	}

    When I create a new record, the onLoad event is called correctly and my onChange event is added the first time...

    7853.pastedimage1618388036237v1.png

    Then I save my newly created record and the onLoad event is called the second time (which is IMHO wrong behaviour) and my onChange event is added the second time...

    4478.pastedimage1618388741596v4.png

    When I now trigger my onChange event, the onChange method is called twice and two annotations are created...

    2376.pastedimage1618388704856v3.png

    I hope this helps to address the issue and somebody can confirm if this a bug.

    Thanks.

  • RahulS Profile Picture
    360 on at

    Hi,

    I am seeing the same behavior, I've been able to work around this by setting a flag when the first time onload executes and then prevent executing next time. Were you able to figure why this happens and if this is a product bug?

  • Suggested answer
    Wahaj Rashid Profile Picture
    11,321 on at

    Hi,

    This is not a bug.

    Any function registered on load of the form, triggers every time the form loads (even after you save the form), this is expected behavior.

    Do you save the form in your OnLoad function? If yes, it will execute the OnLoad function again.

  • RahulS Profile Picture
    360 on at

    Hi, thanks for your reply, no I'm not saving through onload.

    Noticed that only when form is in CREATE mode and you save (manually) the onload will fire again, onload firing again is okay but why is it registering onchange handler twice may it is not clearing out previous handler, so if I've an alert when the field is changed that alert would show twice. I can share the code but it is same like how Chris described above.

    someone else facing similar behavior - community.dynamics.com/.../onchange-events-always-called-twice-after-new-record-creation

    Exact steps are like below -

    pastedimage1622961019806v1.png

  • Wahaj Rashid Profile Picture
    11,321 on at

    Like the post suggests, are you having this issue in a specific browser?

  • RahulS Profile Picture
    360 on at

    Hi, thanks for your reply.

    I am facing this both in edge and chrome.

  • AjayPant93 Profile Picture
    15 on at

    Hi, Did you find the resolution to this? i am facing the same issue.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 137 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans