c# - Get related entity statecode value to use as a check -


i need able execute code upon condition related opportunity has statecode of 1

in code able use generatesalesorderfromopportunityrequest class provided microsoft dynamics sdk create new sales order when opportunityclose activity created.

the drawback of approach opportunityclose activity created system when opportunity closed won(1) or lost(2). also, there no attributes on opportunityclose activity if won or lost. way find out attribute related opportunity.

in code i'm able other attributes related opportunity, name, have not been able other value statecode other 0.

here code:

entity postimageentity = (context.postentityimages != null && context.postentityimages.contains(this.postimagealias)) ? context.postentityimages[this.postimagealias] : null;              if (postimageentity.logicalname == "opportunityclose" && postimageentity.attributes.contains("opportunityid") && postimageentity.attributes["opportunityid"] != null)             {                 // create entity reference related opportunity id generatesalesorderfromopportunityrequest class                 entityreference entityref = (entityreference)postimageentity.attributes["opportunityid"];                  // retrieve opportunity closed opportunity activity created for.                 entity relatedentityref = service.retrieve("opportunity", entityref.id, new columnset( new string[] {"statecode","statuscode", "name"}));                  optionsetvalue statecode = (optionsetvalue)relatedentityref.attributes["statecode"];                  optionsetvalue statuscode = (optionsetvalue)relatedentityref.attributes["statuscode"];                  string oppname = (string)relatedentityref.attributes["name"];                  if (entityref.logicalname == "opportunity" && statecode.value == 1)                 {                     try                     {                         generatesalesorderfromopportunityrequest req = new generatesalesorderfromopportunityrequest();                         req.opportunityid = entityref.id;                         req.columnset = new columnset(true);                         generatesalesorderfromopportunityresponse resp = (generatesalesorderfromopportunityresponse)service.execute(req);                     }                     catch (faultexception ex)                     {                         throw new invalidpluginexecutionexception("an error occurred in plug-in.", ex);                     }                 }             } 

recap: work need able actual statecode value of opportunity related opportunityclose. have been able 0 if know state code of opportunity 1.

other info:

  • this microsoft dynamics online 2013/2015(works both)
  • using skd v6.1.1
  • plugin works, fires whether opportunity won or lost. (not intended)

can't view opportunity see if status won or lost.

you can retrieve opportunityclose , change status if need https://msdn.microsoft.com/en-us/library/gg334301.aspx

i assume it's setting opportunityclose 1 because executing generatesalesorderfromopportunityrequest if won opportunity (e.g. wouldn't progress lost opportunity).