Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Create and Update record using XRM.WebAPI triggered by Custom Button

Posted on by Microsoft Employee

There are two entity, Entity A and Entity B. I just created custom button using ribbon workbench and  placed the button in the home page of the Entity A.

The main idea is simple.

First, user will select one or more records that displayed in the views of Entity A

Second, user press the custom button. The custom button contains script to create records in Entity B. When created this record, script will get GUID this new record. Then update look up in the selected records based on the GUID of the new records.

Here the script:

function CreateTicket(selectedIds){
	var select = selectedIds.toString();
	var guidTicket;
	var ticObj = new Object();
	ticObj.lnkt_name = " ";
	ticObj.new_tickettype = 100000001;
	
	//Create Record
	Xrm.WebApi.createRecord("lnkt_troubleticket", ticObj).then(
		function success(result) {
			
			guidTicket = result.id;
			//Make sure that ticket has been created
			Xrm.Utility.alertDialog("Ticket created with ID: " + guidTicket + "ID: " + select);
		

			If(select != null && select != "")
			{
				
				var arrIds = select.split(",");
					
				for(var indxs = 0; indxs < arrIds.length; indxs++)
				{
						//Call Update Function
					UpdateCase(guidTicket, arrIds[indxs]);
					
				}
				alert("All selected records Updated");
			}
			else
			{
				alert("No records has been selected !");
			}
		
		},
		function (error) {
			Xrm.Utility.alertDialog(error.message);
		}
	);
        
}

function UpdateCase(guidTicket, selectId){
	
	//Create object for update 
	var ticketObj = new Object();
		
	ticketObj["lnkt_TroubleTicketId@odata.bind"] = "/lnkt_troubletickets(" + guidTicket + ")";
	Xrm.Utility.alertDialog("Ticket ID: "+ guidTicket +"\nCaseID : " + selectId);
		
	// update the record
	Xrm.WebApi.updateRecord("incident", selectId, ticketObj).then(
		function success(result) {
			
			Xrm.Utility.alertDialog("Case Updated");
			// perform operations on record update
		},
		function (error) {
			Xrm.Utility.alertDialog(error.message);
			// handle error conditions
		}
	);
}


The script looks not working. Because nothing happen when i press the custom button after select the record.

I have checked that both var select and var guidTicket contains data. 

But if i remove the "BOLD" scripts. This is what i get

37048.1.png

Need your help :)

*This post is locked for comments

  • Shaminderpal Singh Profile Picture
    Shaminderpal Singh 1,565 on at
    RE: Create and Update record using XRM.WebAPI triggered by Custom Button

    yes ,xrm.webapi only works for 9.0 version .The namespace got added in 9.0 release ,for previous versions you can create xmlhttprequest and call api

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Create and Update record using XRM.WebAPI triggered by Custom Button

    Hi andrew, sorry to interrupt. Just short question, this Xrm.WebApi only work for CRM v.9 ?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Create and Update record using XRM.WebAPI triggered by Custom Button

    Hi, i have found that the one and only problem was in the "If". And when i changed into lower case, its fix my problem. Anyways thanks for your answers. I ll mark this

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Create and Update record using XRM.WebAPI triggered by Custom Button

    Hello,

    Here are several suggestions:

    1. If is not valid operator so replace line

    If(select != null && select != "")

    with line

    if(select != null && select != "")

    2. Normalize your guid - webapi doesn't like curly brackets.

    So change line

    guidTicket = result.id;

    to line

    guidTicket = result.id.replace("{", "").replace("}", "");

    3. I'm not sure regarding following line tha it's correct:

    ticketObj["lnkt_TroubleTicketId@odata.bind"] = "/lnkt_troubletickets(" + guidTicket + ")";

    I'm almost sure that it should be lowercased. Anyway the best and easiest way to get proper syntax is to use CRMRestBuilder - github.com/.../2.5.0.0

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