none
How do I use an existing data map in programme that imports data in to Dynamics CRM 2011

    Spørgsmål

  • I am having my first attempts at programming Dynamics CRM with Visual Basic - I am trying to import records automatically rather than use the import wizard. I am using code from the SDK as a starting point, but I am having difficulties with fields that are lookups - in particular I am trying to import into the product entity and am getting an error. The error surfaces during the transformation process - parsing works fine.

    One area that I think could be the problem is that the code I am using creates a new datamap each time I run the program.  When I try to import the data using one of the program generated datamaps the import fails when using the import wizard.  If I use a previously existing datamap then the import succeeds.  Can I use an existing datamap programmatically?  If so, what does the code look like (VB please)?

    Any help would be appreciated

    Cheers

    John

    9. oktober 2012 14:51

Svar

  • Hi,

    I think I have faced the same problem before. Try this:

    -Export your entity map from Dynamics CRM

    -Open the entity map in an editor and locate the following line:

    <EntityMap TargetEntityName="yourentity" SourceEntityName="" Dedupe="Ignore" ProcessCode="Process">

    -Then change this line in your code:

    .SourceEntityName = "Product_1",

    and instead of "Product_1", use the value of the "SourceEntityName" attribute in the above xml element.

    Hope this will work.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.



    10. oktober 2012 14:11
  • Hello John,

    I suppose you have already taken a look at this.

    This is the part where a new Import Map is created:

    Dim importMap As New ImportMap() With
                    {
                        .Name = "Import Map " & Date.Now.Ticks.ToString(),
                        .Source = "Import Accounts.csv",
                        .Description = "Description of data being imported",
                        .EntitiesPerFile = New OptionSetValue(
                            CInt(Fix(ImportMapEntitiesPerFile.SingleEntityPerFile))),
                        .EntityState = EntityState.Created
                    }
                Dim importMapId As Guid = _serviceProxy.Create(importMap)

    In your case though, you can just perform a "Retrieve" using the Organization Service to get the Import Map Id. You can do this setting a filter in your QueryExpression, i.e. using the Name of the map as an attribute filter. Here is a sample in C#:

    var query = new QueryByAttribute("importmap");
    query.AddAttributeValue("name", "<the name of your import map">);
    
    var importMaps = organizationServiceProxy.RetrieveMultiple(query).Entities;
    
    if (Entities.Count > 0)
    {
    	var importMapId = importMaps[0].Id;
    }

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.


    9. oktober 2012 15:24

Alle besvarelser

  • Hello John,

    I suppose you have already taken a look at this.

    This is the part where a new Import Map is created:

    Dim importMap As New ImportMap() With
                    {
                        .Name = "Import Map " & Date.Now.Ticks.ToString(),
                        .Source = "Import Accounts.csv",
                        .Description = "Description of data being imported",
                        .EntitiesPerFile = New OptionSetValue(
                            CInt(Fix(ImportMapEntitiesPerFile.SingleEntityPerFile))),
                        .EntityState = EntityState.Created
                    }
                Dim importMapId As Guid = _serviceProxy.Create(importMap)

    In your case though, you can just perform a "Retrieve" using the Organization Service to get the Import Map Id. You can do this setting a filter in your QueryExpression, i.e. using the Name of the map as an attribute filter. Here is a sample in C#:

    var query = new QueryByAttribute("importmap");
    query.AddAttributeValue("name", "<the name of your import map">);
    
    var importMaps = organizationServiceProxy.RetrieveMultiple(query).Entities;
    
    if (Entities.Count > 0)
    {
    	var importMapId = importMaps[0].Id;
    }

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.


    9. oktober 2012 15:24
  • Pavlos,

    Many thanks for you suggestion.  I will translate that code into VB.  Your solution has raised a further question.  Once I retrieve an existing data map, do I still have to define the import columns, or is that taken care of by the import map?

    John

    10. oktober 2012 08:15
  • Pavlos,

    Many thanks for you suggestion.  I will translate that code into VB.  Your solution has raised a further question.  Once I retrieve an existing data map, do I still have to define the import columns, or is that taken care of by the import map?

    John

    10. oktober 2012 08:16
  • Hi John,

    no, you do not need to define import columns and mappings as long as you have the required import map.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.

    10. oktober 2012 08:19
  • Pavlos,

    Almost there!! The application now runs and reports each stage as successful, however when I look for the data in CRM it is not there!  I checked the import log to see what happened and can see that the job ran, but the status is "Failed".  When I delve deeper in to the system logs for that job the three stages show as successful.  Any idea where it might be going wrong?

    John

    10. oktober 2012 09:43
  • Hi John,

    I'm afraid I can't help you unless you can share your code, so that I can find the issue. I don't have enough info to be able to guess where the problem might be.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.

    10. oktober 2012 09:46
  • Pavlos,

    I am using the code in the dataimports example in the SDK - Dataimportsvb --> ImportWithCreate.vb

    The procedure is ImportRecords.

    The code is pretty much as standard apart from the modifications you suggest. Below is the code that I have modified or added:

     

                Dim query = New QueryByAttribute("importmap")
                query.AddAttributeValue("name", "FM_1_Products")
                Dim importMaps = _serviceProxy.RetrieveMultiple(query).Entities
                'If Entities.Count > 0 Then
                Dim importMapId = importMaps(0).Id
                'End If
                Dim import As New Import() With
                    {
                        .ModeCode = New OptionSetValue(CInt(Fix(ImportModeCode.Create))),
                        .Name = "Importing data"
                    }
                ' IsImport is obsolete; use ModeCode to declare Create or Update
                Dim importId As Guid = _serviceProxy.Create(import)
                Dim importFile As New ImportFile() With
                    {
                        .Content = BulkImportHelper.ReadCsvFile("c:\siderise\FM_1_product.csv"),
                        .Name = "Product record import",
                        .IsFirstRowHeader = True,
                        .ImportMapId = New EntityReference(ImportMap.EntityLogicalName, importMapId),
                        .UseSystemMap = False,
                        .Source = "c:\siderise\FM_1_product.csv",
                        .SourceEntityName = "Product_1",
                        .TargetEntityName = Product.EntityLogicalName,
                        .ImportId = New EntityReference(Import.EntityLogicalName, importId),
                        .EnableDuplicateDetection = False,
                        .FieldDelimiterCode = New OptionSetValue(CInt(Fix(ImportFileFieldDelimiterCode.Comma))),
                        .DataDelimiterCode = New OptionSetValue(CInt(Fix(ImportFileDataDelimiterCode.DoubleQuote))),
                        .ProcessCode = New OptionSetValue(CInt(Fix(ImportFileProcessCode.Process)))
                    }

    The import file looks like this: 

    ID,Name,Unit Group,Default Unit,Product Type,Decimals Supported
    "CRS/045/050/WT/B","45KG/M3 SLAB WHITE TISSUE FACED","Linear Meter","LM","Sales Inventory","0"
    "T3W1/80/161-040-153B","80kg/m3 Mineral Fibre Acoustic Infil for Profiled Deckingl","Linear Meter","LM","Sales Inventory","0"

    Here is a screen dump of the output from the program:

     As I mentioned earlier the program looks to work ok reporting success all the way, but when I look for the data in CRM it isn't there.  The Import list shows that the job failed.  When the job is inspected I get the following:

    Is there any other information I can provide you?

    Regards

    John

    10. oktober 2012 10:45
  • Hi,

    your code seems ok. Try performing the three steps one after another. First try to do only the parsing part and see if any errors occur, then the parsing and the transform part etc. We have to identify at which stage of the import the error occurs.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.

    10. oktober 2012 10:57
  • Good call.  It would seem that the Transform stage is where the process fails (code below)

                    Dim transformImportRequest As New TransformImportRequest() With
                        {.ImportId = importId}
                    Dim transformImportResponse As TransformImportResponse = CType(_serviceProxy.Execute(transformImportRequest), TransformImportResponse)
                    Console.WriteLine("Waiting for Transform async job to complete")
                    '</snippetImportWithCreate5>
                    BulkImportHelper.WaitForAsyncJobCompletion(_serviceProxy, transformImportResponse.AsyncOperationId)
                    BulkImportHelper.ReportErrors(_serviceProxy, importFileId)

    However this code runs and reports success - which of course I now don't believe. Any suggestions on how to debug further? 

    John

    10. oktober 2012 13:56
  • Hi,

    I think I have faced the same problem before. Try this:

    -Export your entity map from Dynamics CRM

    -Open the entity map in an editor and locate the following line:

    <EntityMap TargetEntityName="yourentity" SourceEntityName="" Dedupe="Ignore" ProcessCode="Process">

    -Then change this line in your code:

    .SourceEntityName = "Product_1",

    and instead of "Product_1", use the value of the "SourceEntityName" attribute in the above xml element.

    Hope this will work.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.



    10. oktober 2012 14:11
  • Pavlos,

    Thank you so much.  This worked.  I can understand why the problem occured, having changed the structure of the routine to use an existing data map, the code I used assumed that there would be a new onee created and so the name would not matter.

    Thank you once again,

    John

    10. oktober 2012 14:29
  • showing error that 

    ImportMapEntitiesPerFile does not exit in current context.please help what to do

    22. oktober 2013 14:36