Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Commenting out alert make my script failed

Posted on by Microsoft Employee

Hi everyone,

The following script is trigger on field change.

if I comment the line  alert(""+projectTitleValue); then my variable projectTitleValue has the value before change and not after change. My script has no failure but don´t have the expected behavior. Do you have any idea why?

I´m a newbie with CRM customization and JavaScript. Thanks.

function onChange_Duplicate() {
	Xrm.Page.data.entity.save();
	var projectTitle = Xrm.Page.getAttribute("eno_projecttitle_kd");
	var projectTitleValue = projectTitle.getValue();
	var serverUrl = Xrm.Page.context.getClientUrl();
	var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
	var retrieveResultProjectTitle = new XMLHttpRequest();
	alert(""+projectTitleValue);
	if (projectTitleValue != null) {

		retrieveResultProjectTitle.open("GET", ODataPath +"/OpportunitySet?$select=eno_projecttitle_kd&$filter=eno_projecttitle_kd eq '"+projectTitleValue+"'",false);
		retrieveResultProjectTitle.setRequestHeader("Accept", "application/json");
		retrieveResultProjectTitle.setRequestHeader("Content-Type", "application/json; charset=utf-8?");
		retrieveResultProjectTitle.send('grant_type=client_credentials');
		if (retrieveResultProjectTitle.readyState == 4) {
			if (retrieveResultProjectTitle.status == 200) 
			{
				var retrieved = this.parent.JSON.parse(retrieveResultProjectTitle.responseText).d;
				var Result = retrieved.results;
				if (typeof Result !== "undefined")
				{
					var count = Result.length;
					if (count>1)
					{
						alert("There is already an entity with this Project Title");
					} 
				}
			}

		}
	}
}

*This post is locked for comments

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Commenting out alert make my script failed

    Try to validate with below reference

    dynamicscrmgirl.wordpress.com/.../crm-2013-javascript-retrieve-record-using-odatarest-endpoint-without-jquery

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Commenting out alert make my script failed

    Hi Goutam,

    thanks for your answer. I test it and I meet an error: JSON is undefined or null at this line: var retrieved = this.parent.JSON.parse(retrieveResultProjectTitle.responseText).d;

    Do you have an idea?

    Thanks a lot!

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Commenting out alert make my script failed

    Hi Leo,

    Try with this -

    //Need to register only below method field Onchange.

    function onChange_Duplicate() {   
        Xrm.Page.data.save().then(succesCallback, errorCallback);
    	
    }
    
    function succesCallback()
    {
        var projectTitle = Xrm.Page.getAttribute("eno_projecttitle_kd");
        var projectTitleValue = projectTitle.getValue();
        var serverUrl = Xrm.Page.context.getClientUrl();
        var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
        var retrieveResultProjectTitle = new XMLHttpRequest(); 
        if (projectTitleValue != null) {
    
            retrieveResultProjectTitle.open("GET", ODataPath + "/OpportunitySet?$select=eno_projecttitle_kd&$filter=eno_projecttitle_kd eq '" + projectTitleValue + "'", false);
            retrieveResultProjectTitle.setRequestHeader("Accept", "application/json");
            retrieveResultProjectTitle.setRequestHeader("Content-Type", "application/json; charset=utf-8?");
            retrieveResultProjectTitle.send('grant_type=client_credentials');
            if (retrieveResultProjectTitle.readyState == 4) {
                if (retrieveResultProjectTitle.status == 200) {
                    var retrieved = this.parent.JSON.parse(retrieveResultProjectTitle.responseText).d;
                    var Result = retrieved.results;
                    if (typeof Result !== "undefined") {
                        var count = Result.length;
                        if (count > 1) {
                            alert("There is already an entity with this Project Title");
                        }
                    }
                }
    
            }
        }
    
    }
    
    function errorCallback()
    {
        alert("error");
    }


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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans