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)

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

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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

  • Community Member Profile Picture
    on at

    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

  • Community Member Profile Picture
    on at

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

  • Shaminderpal Singh Profile Picture
    1,565 on at

    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

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