Brad Mc
Brad Mc

Reputation: 151

Silverlight 4 wait for ASync

I have a silverlight 4 control in my Dynamics CRM.

I have it to create multiple opportunities based on whether or not it has multiple ship dates. Users have a simple form at the bottom of the opportunity create form with product information, dates and revenue. This creates copy of the original with the alternate information that users entered in the control.

Currently it creates the opportunities as long as the user waits for a while for the calls to be completed; ideally I would like the form to close automatically after all the new opportunities have been created. Here is the relevant code; can anyone help me with what I need to add to have the CRM wait for everything to be created.

        //OpportunitySet contains all necessary information to create multiple records, 
        //data is valid(it will create information if enough time is given.
        _context.BeginSaveChanges(OnCreateOpportunityComplete, _context.OpportunitySet);
        xrm.Page.data.entity.attributes.get("new_haschildren").setValue(true);
        xrm.Page.data.entity.save("saveandclose");

        private void OnCreateOpportunityComplete(IAsyncResult result)
        {
          try
          {
            _context.EndSaveChanges(result);
            Opportunity createdOpportunity = result.AsyncState as Opportunity;
            //result.AsyncWaitHandle.WaitOne();
            MessagePanel.Children.Add(new TextBlock()
            {
              Text = String.Format("Created a new Opportunity named \"{0}\"\n\twith OpportunityId = \"{1}\".",
                    createdOpportunity.Name, createdOpportunity.OpportunityId)
            });

            BeginRetrieveOpportunity(createdOpportunity.OpportunityId);
            OnRetrieveOpportunityComplete(result);
          }
          catch (SystemException se)
          {
            _syncContext.Send(new SendOrPostCallback(showErrorDetails), se);
          }
        }

Any help would really be appreciated.

Upvotes: 0

Views: 161

Answers (1)

Guido Preite
Guido Preite

Reputation: 15128

You can just put the xrm save&close method as last row to be executed inside your complete method.

Upvotes: 1

Related Questions