Home Self-Service Portal - Community

API Commit new Incident

Morten_MeislerMorten_Meisler Premier Partner Advanced IT Monkey ✭✭✭

Hi,

Does anyone have a working example of how to commit a new Incident from the API? Using Portal 9.4.1.2016

The javascript code is executed inside a Request Offering, where I'm getting an Incident Template first, and then commiting it.

Here's the code:

ajax({ 
		          type: "GET",
		          url: "/api/V3/Projection/CreateProjectionByTemplate",
				  data: {id: "21ae97b6-fc1e-712a-fe57-cc686c7a2bf4", createdById: session.user.Id},
		          dataType: "json",
		          contentType: "application/json; charset=utf-8",
		          success: function(data){
					console.log("Incident template retrieved");
					console.log(data.Id);
					var newData = JSON.parse(JSON.stringify(data));
					
					var strData = { "formJson":{ "current": newData}, "original": null };
					
					$.ajax({
						type: 'POST',
						url: '/api/V3/Projection/Commit',
						data: strData,
						contentType: "application/json",
						dataType: 'json',
						success: function() {
							console.log('Incident created');
						},
						error: function(error){        
		            		console.log(error);
		          }
					}); //ajax commit


				 //TODO: Exit form
		          },
		          error: function(error){        
		            console.log(error);
		          }
		        }); //ajax get projection

Here's the error from the commit api:

"System.NullReferenceException: Object reference not set to an instance of an object.
   at CiresonWebConsole.Controllers.api.V3.ProjectionController.<Commit>d__5.MoveNext() in D:\a\1\s\Cireson.WebConsole\Cireson.ServiceManager.WebConsole\Controllers\api\V3\ProjectionController.cs:line 44
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()"


Best Answers

  • Morten_MeislerMorten_Meisler Premier Partner Advanced IT Monkey ✭✭✭
    Answer ✓

    Thanks John. I figured it out actually, very simple, I just had to add a JSON.stringify at the data

    I'll give you credit for the effort :)

    Working code:

    $.ajax({ 
    		          type: "GET",
    		          url: "/api/V3/Projection/CreateProjectionByTemplate",
    				  data: {id: "0d754ad9-f64c-1141-4c1c-d1a277f0b7fd", createdById: session.user.Id},
    		          dataType: "json",
    		          contentType: "application/json; charset=utf-8",
    		          success: function(data){
    					console.log("Incident created. Enjoy");
    					console.log(data.Id);
    					var newData = JSON.parse(JSON.stringify(data));
    					//var newData = JSON.stringify(data);
    					
    					var strData = { "formJson":{ "current": newData}, "original": null };
    					
    					$.ajax({
    						type: 'POST',
    						url: '/api/V3/Projection/Commit',
    						data: JSON.stringify(strData),
    						contentType: "application/json",
    						dataType: 'json',
    						success: function() {
    							console.log('done');
    						},
    						error: function(error){        
    		            		console.log(error);
    		          }
    					}); //ajax commit
    
    
    				 //TODO: Exit form
    		          },
    		          error: function(error){        
    		            console.log(error);
    		          }
    		        }); //ajax get projection
    

Answers

  • Morten_MeislerMorten_Meisler Premier Partner Advanced IT Monkey ✭✭✭

    I'm guessing some property is missing or a relationship in the template it doesn't like? I tried some different templates to no avail, but the error-message doesn't give much unfortunately. Hope someone can help, thanks :)

  • John_LongJohn_Long Customer Advanced IT Monkey ✭✭✭

    @Morten_Meisler I'm doing something similar but in Python to enable integration of SCSM objects with LogicApps/PowerApps.

    Without comparing JSON objects, I don't think we'll solve it from the above. Happy to assist with comparing items if I can!

  • Morten_MeislerMorten_Meisler Premier Partner Advanced IT Monkey ✭✭✭

    Ok thanks, yes if you have your request input or just the json input for the data you know is working, I could compare that with the input I'm getting from the template. Thanks

  • Morten_MeislerMorten_Meisler Premier Partner Advanced IT Monkey ✭✭✭
    Answer ✓

    Thanks John. I figured it out actually, very simple, I just had to add a JSON.stringify at the data

    I'll give you credit for the effort :)

    Working code:

    $.ajax({ 
    		          type: "GET",
    		          url: "/api/V3/Projection/CreateProjectionByTemplate",
    				  data: {id: "0d754ad9-f64c-1141-4c1c-d1a277f0b7fd", createdById: session.user.Id},
    		          dataType: "json",
    		          contentType: "application/json; charset=utf-8",
    		          success: function(data){
    					console.log("Incident created. Enjoy");
    					console.log(data.Id);
    					var newData = JSON.parse(JSON.stringify(data));
    					//var newData = JSON.stringify(data);
    					
    					var strData = { "formJson":{ "current": newData}, "original": null };
    					
    					$.ajax({
    						type: 'POST',
    						url: '/api/V3/Projection/Commit',
    						data: JSON.stringify(strData),
    						contentType: "application/json",
    						dataType: 'json',
    						success: function() {
    							console.log('done');
    						},
    						error: function(error){        
    		            		console.log(error);
    		          }
    					}); //ajax commit
    
    
    				 //TODO: Exit form
    		          },
    		          error: function(error){        
    		            console.log(error);
    		          }
    		        }); //ajax get projection
    
  • John_LongJohn_Long Customer Advanced IT Monkey ✭✭✭

    Aw shucks! :D Glad you got it going!

Sign In or Register to comment.