Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Error when using QuoteClose

(0) ShareShare
ReportReport
Posted on by 5

Hi team,

I'm currently experiencing an issue when I try and use QuoteCloseRequest in my code:

		public void CloseQuotes(string opportunityGuid) {
			var dataVerseConnection = new ServiceClient(_connectionString);
			if (!dataVerseConnection.IsReady) {
				throw new Exception("Authentication Failed!");
			}

			var columns = new ColumnSet(new string[] { "quoteid" });

			var entityCollection = DynamicsConnection.GetEntityCollection(dataVerseConnection, "quote", "opportunityid", opportunityGuid, columns);

			var quoteList = new List();
			foreach (var attr in entityCollection.Entities.ToArray()) {
				var quoteId = attr.Attributes["quoteid"].ToString();
				quoteList.Add(new Quote().Get(quoteId));
			}

			foreach (var quote in quoteList) {

				/*
					StateCode
					0 - Draft
					1 - Active
					2 - Won
					3 - Closed

					StatusCode
					1 - In Progress (Draft)
					2 - In Progress (Active)
					4 - Won
					5 - Lost
					7 - Revised
				*/

				if (quote.stateCodeName == "Active") {

					Console.WriteLine($"Closing Active Quote: {quote.name}");

					// close quote
					quote.stateCode = 3;
					quote.stateCodeName = "Closed";
					quote.statusCode = 7;
					quote.statusCodeName = "Revised";


					var req = new CloseQuoteRequest();
					var quoteClose = new Entity("quoteclose");
					quoteClose.Attributes.Add("quoteid", new EntityReference("quote", new Guid(quote.guid)));
					req.QuoteClose = quoteClose;
					req.RequestName = "CloseQuote";
					var o = new OptionSetValue();
					o.Value = -1;
					req.Status = o;

					dataVerseConnection.Execute(req);
					

				} else if (quote.stateCodeName == "Draft") {

					Console.WriteLine($"Deleting Draft Quote: {quote.name}");
					new Quote().Delete(quote);
				}

			}
		}

This is the error received when I run this code:

System.Private.CoreLib: Exception while executing function: OpportunityCloseQuotes. 

Microsoft.PowerPlatform.Dataverse.Client: Failed to connect to Dataverse. System.Private.CoreLib: One or more errors occurred. (Exception - Fault While initializing client - RefreshInstanceDetails). 
Microsoft.PowerPlatform.Dataverse.Client: Exception - Fault While initializing client - RefreshInstanceDetails. 
Microsoft.PowerPlatform.Dataverse.Client: Exception - Failed to lookup current organization data. 
Microsoft.PowerPlatform.Dataverse.Client: 

[A]Microsoft.Crm.Sdk.Messages.RetrieveCurrentOrganizationResponse cannot be cast to [B]Microsoft.Crm.Sdk.Messages.RetrieveCurrentOrganizationResponse. 

Type A originates from 'Microsoft.Crm.Sdk.Proxy, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location '*\DynamicsFunctions\bin\Release\netcoreapp3.1\bin\Microsoft.Crm.Sdk.Proxy.dll'. 

Type B originates from 'Microsoft.Cds.Sdk.Proxy, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location '*\DynamicsFunctions\bin\Release\netcoreapp3.1\bin\Microsoft.Cds.Sdk.Proxy.dll'.

When I comment out the CloseQuoteRequest instantiation, the code runs fine.

What is happening, and what changes can I make to make this code run?

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,492 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans