web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How can I create multiple records from a multiline text field?

(0) ShareShare
ReportReport
Posted on by

I want to be able to copy and paste a list of phone numbers into a dialog page multi line text field and be able to convert this list into a number of custom entity records "Lines" related to the relevant account and order (which can be pulled from the order entity the dialog runs from).  
I'm pretty certain after searching the web I would need to do this with a custom workflow in visual studio.  I have coded with VB before so I'm pretty sure the text field will have to be converted to an array the run in a loop creating records until the end of the array.  I just don't have a clue how to do this in c# or xml.  

Has anyone ever done anything like this before with samples of code? 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Ivan Ficko Profile Picture
    1,380 on at

    You need to create DLL project and add one code activity in it. That class must look like:

    namespace Workflows {
      public class CreateLines : CodeActivity {
        [Input("LinesString")]
        public InArgument<string> LinesString { get; set; }
        protected override void Execute(CodeActivityContext context) {
          IWorkflowContext workflowcontext = context.GetExtension<IWorkflowContext>();
          IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
          IOrganizationService service = serviceFactory.CreateOrganizationService(workflowcontext.InitiatingUserId);
    
          var lines = LinesString.Get<string>(context).Split(',').ToList<string>();;
    
          foreach (var line in lines) {
            var lineEntity = new Entity("YOUR_ENTITY_NAME");
            lineEntity["ATTRIBUTE_NAME"]=line;
            service.Create(lineEntity);
          }
        }
      }
    }

    Then you need to deploy it with Plugin registration tool (can be found in CRM SDK). When you deployed your custom step you can use it inside workflow designer.

  • Community Member Profile Picture
    on at

    Thanks that's great, I need to pass through an account and an order record so it can be written into each line record so how does this look:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using System.Activities;

    using Microsoft.Xrm.Sdk.Workflow;

    using Microsoft.Xrm.Sdk;

     

    namespace CreateMultipleRecords

    {

    public sealed class CreateRecords : CodeActivity

    {

    [Input("Phone Numbers")]

    [RequiredArgument]

    public InArgument<string> LinesString { get; set; }

    [Input("Billing Account")]

    [RequiredArgument]

    [ReferenceTarget("account")]

    public InArgument<EntityReference> Account { get; set; }

    [Input("Order")]

    [RequiredArgument]

    [ReferenceTarget("order")]

    public InArgument<EntityReference> Order { get; set; }

    protected override void Execute(CodeActivityContext context)

    {

    throw new NotImplementedException();

    IWorkflowContext workflowcontext = context.GetExtension<IWorkflowContext>();

    IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();

    IOrganizationService service = serviceFactory.CreateOrganizationService(workflowcontext.InitiatingUserId);

    var lines = LinesString.Get<string>(context).Split(',').ToList<string>(); ;

    foreach (var line in lines)

    {

    var lineEntity = new Entity("rp_line");

    lineEntity["rp_cli"] = line;

    lineEntity["new_billingaccount"] = Account;

    lineEntity["new_order"] = Order;

    service.Create(lineEntity);

    }

    }

    }

    }

  • Suggested answer
    Ivan Ficko Profile Picture
    1,380 on at

    It looks ok on the first look (of course you need to remove NotImplementedException before deploying), but you will get an error.

    You need to fetch account and order parameter just like you did with lines.

    var order = Account.Get<EntityReference>(context);
    var account = Order.Get<EntityReference>(context);

    You need to add those line before foreach loop and then use those variables (order, account) in foreach loop.


  • Community Member Profile Picture
    on at

    This is probably something silly but I'm getting this on execution in CRM:

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Unexpected exception from plug-in (Execute): CreateLineRecords.CreateRecords: System.NotImplementedException: The method or operation is not implemented.Detail:

    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance&quot; xmlns="schemas.microsoft.com/.../Contracts&quot;>

     <ActivityId>cd87914b-50ad-4daf-ad2a-20d97526d2df</ActivityId>

     <ErrorCode>-2147220891</ErrorCode>

     <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic&quot; />

     <Message>Unexpected exception from plug-in (Execute): CreateLineRecords.CreateRecords: System.NotImplementedException: The method or operation is not implemented.</Message>

     <Timestamp>2017-10-24T12:03:54.3731824Z</Timestamp>

     <ExceptionRetriable>false</ExceptionRetriable>

     <ExceptionSource i:nil="true" />

     <InnerFault>

       <ActivityId>cd87914b-50ad-4daf-ad2a-20d97526d2df</ActivityId>

       <ErrorCode>-2147220970</ErrorCode>

       <ErrorDetails xmlns:d3p1="schemas.datacontract.org/.../System.Collections.Generic&quot; />

       <Message>Plugin Trace:

    [CreateLineRecords: CreateLineRecords.CreateRecords]

    [CreateLineRecords (1.0.0.0): CreateLineRecords.CreateRecords]

    Error Message:

    Unhandled Exception: Microsoft.Crm.CrmException: Unexpected exception from plug-in (Execute): CreateLineRecords.CreateRecords: System.NotImplementedException: The method or operation is not implemented.

      at Microsoft.Crm.Sandbox.SandboxCodeUnit.Execute(IExecutionContext context)

      at Microsoft.Crm.Workflow.Services.ProxyCustomActivity.Execute(CodeActivityContext executionContext)

    </Message>

       <Timestamp>2017-10-24T12:03:54.3731824Z</Timestamp>

       <ExceptionRetriable>false</ExceptionRetriable>

       <ExceptionSource i:nil="true" />

       <InnerFault i:nil="true" />

       <OriginalException i:nil="true" />

       <TraceText i:nil="true" />

     </InnerFault>

     <OriginalException i:nil="true" />

     <TraceText i:nil="true" />

    </OrganizationServiceFault>

  • Suggested answer
    Ivan Ficko Profile Picture
    1,380 on at

    Did you removed

    throw new NotImplementedException();

    before you deployed DLL to CRM?

  • Community Member Profile Picture
    on at

    Yes I've removed it and re added it but I just get a more generic error:

     <Message>System.Runtime.Serialization.SerializationException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #43D3B65B</Message>

  • Community Member Profile Picture
    on at

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using System.Activities;

    using Microsoft.Xrm.Sdk.Workflow;

    using Microsoft.Xrm.Sdk;

    namespace CreateLineRecords

    {

       public class CreateRecords : CodeActivity

       {

           [Input("Phone Numbers")]

           [RequiredArgument]

           public InArgument<string> LinesString { get; set; }

           [Input("Billing Account")]

           [RequiredArgument]

           [ReferenceTarget("account")]

           public InArgument<EntityReference> Account { get; set; }

           [Input("Order")]

           [RequiredArgument]

           [ReferenceTarget("salesorder")]

           public InArgument<EntityReference> Order { get; set; }

                   protected override void Execute(CodeActivityContext context)

           {

               IWorkflowContext workflowcontext = context.GetExtension<IWorkflowContext>();

               IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();

               IOrganizationService service = serviceFactory.CreateOrganizationService(workflowcontext.InitiatingUserId);

               var lines = LinesString.Get<string>(context).Split(',').ToList<string>(); ;

               foreach (var line in lines)

               {

                   var lineEntity = new Entity("rp_line");

                   lineEntity["rp_cli"] = line;

                   var order = Account.Get<EntityReference>(context);

                   var account = Order.Get<EntityReference>(context);

                   lineEntity["new_billingaccount"] = Account;

                   lineEntity["new_order"] = Order;

                   service.Create(lineEntity);

               }

           }

       }

    }

  • Community Member Profile Picture
    on at

    account and order variable should have really been named something else changed them to the lowercase name and we have progress.

    Now I get

    <Message>Account With Id = 7769c7a4-2963-e711-8109-e0071b6e1401 Does Not Exist</Message>

    Complains I do not have permission to the record or it does not exist.  

    I'm wondering if this is because its a customer field?

  • Verified answer
    Ivan Ficko Profile Picture
    1,380 on at

    You are using wrong values here, you need to use non capsed variables here

    var order = Account.Get<EntityReference>(context);
    var account = Order.Get<EntityReference>(context);
    lineEntity["new_billingaccount"] = Account account;
    lineEntity["new_order"] = Order order;


  • Suggested answer
    Ivan Ficko Profile Picture
    1,380 on at

    Than you need to add account read premission to the user that is executing workflow.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans