Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Unanswered

Error when using QuoteClose

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,532 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,501 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans