locked
Work Order Creation - Via Plugin RRS feed

  • Question

  • Hi folks -

    I'm attempting to create a work order when a project task is created. The code I'm using for my plug-in appears to run fine, but no work order is created. Has anyone else attempted to create a work order programmatically and can you help me work through what I'm missing?

    I chose the fields to fill on the basis of the Quick Work Order form presuming that there would therefore be no unidentified required fields present.

    Here's the code I'm working from:

    Entity workorder = new Entity("msdyn_workorder");
    
                        // Customer Information Column
                        workorder["msdyn_workordersummary"] = "Created work order as a result of project task:  " +
                            entity["msdyn_subject"] + "."; // Not required
                        workorder["msdyn_serviceaccount"] = "Blue Yonder Oil"; // Required field
                        //workorder["msdyn_billingaccount"] = "Blue Yonder Oil"; // Not required field
                        workorder["msdyn_reportedbycontact"] = "Allan Saddler"; // Not required field
    
                        // Incident Information Column
                        workorder["msdyn_primaryincidenttype"] = "New Installation"; // Not required field
                        workorder["msdyn_primaryincidentdescription"] = "Planned installation of new device on-site."; // Not required field
                        // Primary Incident Customer Asset - Not required field
                        // Primary Incident Estimated Duration - Not required field
                        workorder["msdyn_workordertype"] = "Installation"; // Required field
    
                        // Details Column
                        // Priority - Not required field
                        workorder["msdyn_systemstatus"] = "Open - Unscheduled"; // Required field
                        // Sub-Status - Not required field
                        // Service Territory - Not required field
                        workorder["msdyn_pricelist"] = "Default Price List"; // Required field
    
    
                        // Refer to the project task in the work order.
                        //if (context.OutputParameters.Contains("id"))
                        //{
                        //    Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
                        //    string regardingobjectidType = "account";
                        //
                        //    workorder["regardingobjectid"] =
                        //    new EntityReference(regardingobjectidType, regardingobjectid);
                        //}
    
                        // Obtain the organization service reference.
                        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                        // Create the work order in Microsoft Dynamics CRM.
                        tracingService.Trace("CreateWorkOrder: Creating the work order.");
                        service.Create(workorder);

    Any and all suggestions are appreciated and welcomed.

    Thank you,

    John-Michael Scott

    Tuesday, March 14, 2017 7:18 PM

Answers

  • Hi John,

    It looks like that the Work Order entity has special requirement to provide the Service Account and the Price List even when you create a record via code. Tha's the minimum you need to provide. You don't have to provide all the required fields like work order type, billing account etc.

    So add the following couple of lines as well before you call the create method.

    workorder["msdyn_serviceaccount"] = new EntityReference("account", new Guid("Guid of the account"));
    workorder["msdyn_pricelist"] = new EntityReference("pricelevel", new Guid("Guid of the price list"));

    Thanks


    Sachith Chandrasiri

    Friday, March 17, 2017 1:02 AM
  • Just to close this out -

    This worked perfectly.

    The essential code looks like this in my example when done:

    // Create a work order based on another entity throwing an event (could be create, could be update, etc).
     
    Entity workorder = new Entity("msdyn_workorder");
    
    // Information Required
    workorder["msdyn_workordersummary"] =
       "Work order to work with the customer. Add service tasks as needed.";
    workorder["msdyn_serviceaccount"] = new EntityReference("account", accountGUID);
    workorder["msdyn_pricelist"] = new EntityReference("pricelevel", pricelevelGUID);
                        
    // Create the work order in Microsoft Dynamics CRM.
    service.Create(workorder);

    Thanks Sachith for helping me clear this up!

    Cheers!

    John-Michael Scott


    Friday, March 17, 2017 11:44 PM

All replies

  • Hi John,

    You are setting strings for lookup fields and options sets in the work order entity here and that’s why your code does not work.

    e.g.

    workorder["msdyn_reportedbycontact"] = "Allan Saddler";

    This is not correct and should be written like this as msdyn_reportedbycontact is a lookup :

    workorder["msdyn_reportedbycontact"] = new EntityReference(“contact”,  GUID of the contact);

    You need to get the GUID of the contact first.

    There are so many other places like this:

    msdyn_serviceaccount, msdyn_primaryincidenttype, msdyn_workordertype, msdyn_pricelist are all lookups and you need to set them similar to the above. You need to figure out how to get their GUIDs in the first place in your plugin logic.

    And

    workorder["msdyn_systemstatus"] = "Open -Unscheduled";

    should be written like below as that field is an option set.

    workorder["msdyn_systemstatus"] =new OptionSetValue(integer value);

    Had you generated early bound classes and written your code accordingly rather than using late binding you would have found all these errors at compile time.

    Thanks


    Sachith Chandrasiri

    Wednesday, March 15, 2017 3:47 AM
  • As sachith mentioned. You should set all the lookups in Work Order like this:

    workorder["lookupfieldname"] = new EntityReference(“entitytype”,  GUID of the record);


    Regards, Saad

    Wednesday, March 15, 2017 9:55 AM
  • Hi Sachith & Saad -

    I've made progress along the lines you reference yesterday and my workorder setups now look like this:

    workorder["msdyn_serviceaccount"] = new EntityReference("account", serviceaccountId); // Required field
    workorder["msdyn_workordertype"] = new EntityReference("msdyn_workordertype", wotypeId); // Required field
    workorder["msdyn_taxable"] = false;

    It's not perfectly clear that msdyn_taxable is a required field. As far as I can tell - if you just attempt to create a new work order without filling in any fields on a standard form - the first two are definitely required - and the third may or may not be required.

    Other fields appear to have defaults.

    Do either of you know the default fields that are absolutely necessary to create a Work Order - once I'm 100% clear on that - I should be able to get the rest of the typecasting correct.  Thank you very much for the explanations!

    Thursday, March 16, 2017 7:38 PM
  • Hi John,

    You  don't have to provide values for required fields when you create or update records via SDK. They are enforced only on client side.

    Thanks


    Sachith Chandrasiri

    Thursday, March 16, 2017 7:47 PM
  • so - if I don't have to provide any required fields - should the following work then ?

    I've tried this and no work order is created. I can create a task no problem - but trying to do the same thing to create a work order has proven challenging. :)

    // Obtain the organization service reference. IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); // Create a work order associated with the project task if the task field work order is set to yes. Entity workorder = new Entity("msdyn_workorder"); // Customer Information Column workorder["msdyn_workordersummary"] = "Work order to work with the customer. Add service tasks as needed.";

    // Create the work order in Microsoft Dynamics CRM. service.Create(workorder);

    Any ideas on what I'm missing here?

    Thank you very much!

    John-Michael

    Friday, March 17, 2017 12:27 AM
  • Hi John,

    It looks like that the Work Order entity has special requirement to provide the Service Account and the Price List even when you create a record via code. Tha's the minimum you need to provide. You don't have to provide all the required fields like work order type, billing account etc.

    So add the following couple of lines as well before you call the create method.

    workorder["msdyn_serviceaccount"] = new EntityReference("account", new Guid("Guid of the account"));
    workorder["msdyn_pricelist"] = new EntityReference("pricelevel", new Guid("Guid of the price list"));

    Thanks


    Sachith Chandrasiri

    Friday, March 17, 2017 1:02 AM
  • Just to close this out -

    This worked perfectly.

    The essential code looks like this in my example when done:

    // Create a work order based on another entity throwing an event (could be create, could be update, etc).
     
    Entity workorder = new Entity("msdyn_workorder");
    
    // Information Required
    workorder["msdyn_workordersummary"] =
       "Work order to work with the customer. Add service tasks as needed.";
    workorder["msdyn_serviceaccount"] = new EntityReference("account", accountGUID);
    workorder["msdyn_pricelist"] = new EntityReference("pricelevel", pricelevelGUID);
                        
    // Create the work order in Microsoft Dynamics CRM.
    service.Create(workorder);

    Thanks Sachith for helping me clear this up!

    Cheers!

    John-Michael Scott


    Friday, March 17, 2017 11:44 PM