none
Import data programmatically using an importmap in CRM 2011

    Question

  • Hi Forum,

    I got the following problem:

    I imported a normal CSV File and created a mapping, i saved that mapping  and called it DAB_DEPOSITE. I currently try to write a small Excel AddIn. I open an Excel Sheet, do some "magic" to get the format right. Pick up the DAB_DEPOSITE Mapping (from a dropdownlist in my Excel AddIn) and then start the import.

    But I ran into trouble with a few lines of code

    // Create a logical source file of the data being imported.
          ImportFile _importFile = new ImportFile();
    
          _importFile.Name = wbook.FullName;
          _importFile.Source = wbook.FullName;
    
          _importFile.FileTypeCode = new OptionSetValue(0); // 0: CSV: 1: XML
    
          // Read contents from the datatable
          _importFile.Content = ConvertToString(impTable);
    
          // Allow duplicate records to import.
          _importFile.EnableDuplicateDetection = cbDublicateCheck.Checked;
    
          // Relate the import file to the parent import (data import).
          _importFile.ImportId = new EntityReference();
          //_importFile.ImportId.LogicalName = Import.EntityLogicalName;
          _importFile.ImportId.Name = wbook.FullName;
          _importFile.ImportId.Id = importId;
    
          // Use DataMap from System
          _importFile.ImportMapId = new EntityReference();
          _importFile.ImportMapId.LogicalName = ImportMap.EntityLogicalName;
          _importFile.ImportMapId.Id = iMapDetails[ddlMapping.SelectedItemIndex].ImportMapId;
          _importFile.ImportMapId.Name = iMapDetails[ddlMapping.SelectedItemIndex].Name;
          
          // Relate this import file to the parent data map.
          _importFile.UseSystemMap = false;
          _importFile.SourceEntityName = impTable.TableName;
          _importFile.TargetEntityName = impTable.TableName;
          _importFile.Source = wbook.FullName;
          //_importFile.Size = file.Length.ToString();
    
          _importFile.DataDelimiterCode = new OptionSetValue(2); // None
          _importFile.FieldDelimiterCode = new OptionSetValue(4); // Semicolon
    
          // Do not use the first row during import.
          _importFile.IsFirstRowHeader = true;
    
          // Specify to process the column.
          _importFile.ProcessCode = new OptionSetValue(1); // 1: Process, 2: Ignore, 3: Internal
          _importFile.RecordsOwnerId = new EntityReference(SystemUser.EntityLogicalName, userId);
    

    My current problem is: If this code ran, the import view of crm (Workplace=>Import) says ERROR. If I open the importjob no errors are shown and the system jobs says successfull. Just in the trace log of the crm server an exception is thrown

    >None of the columns are mapped, can not continue with this ImportFile id = {E65544B1-1D61-E011-A686-000C291EA86C}

    I debuged the Excel-AddIn and copy and paste the content of  _importFile.Content into notepad and saved it as CSV and started the import job via the CRM User Interface. Everything went fine.

    If I comment the lines to link the import file to a map. Everything works as expected (Errors are SHOWN).

    // Use DataMap from System
    //      _importFile.ImportMapId = new EntityReference();
    //      _importFile.ImportMapId.LogicalName = ImportMap.EntityLogicalName;
    //      _importFile.ImportMapId.Id = iMapDetails[ddlMapping.SelectedItemIndex].ImportMapId;
    //      _importFile.ImportMapId.Name = iMapDetails[ddlMapping.SelectedItemIndex].Name;
    

    Does someone has some experience with that problem ?

    Thanks in advance

    Christian

    Thursday, 7 April, 2011 2:24 PM

All replies

  • Hi, 

    I have the same problem. Have you found a solution for this problem? 

     

    Regards, 

     

    Daniel 

    • Proposed as answer by thuld Wednesday, 20 April, 2011 8:59 AM
    • Unproposed as answer by thuld Wednesday, 20 April, 2011 9:00 AM
    Tuesday, 19 April, 2011 4:12 PM
  • Hi,

    I found the reason for this behaviour: We need to set the value to the SourceEntityName of the importfile record to the corresponding value of the use importmap. When you export the data-map you will see the sourceEntityName:

    <EntityMap TargetEntityName="account" SourceEntityName="account.import.many" Dedupe="Ignore" ProcessCode="Process">
    
    

    Or you could use the following query to get the sourceentityname:

     

    var query = new QueryExpression(ImportEntityMapping.EntityLogicalName){ ColumnSet = new ColumnSet("sourceentityname")};
    query.Criteria.AddCondition(new ConditionExpression("importmapid", ConditionOperator.Equal, mappingid));
    
    var result = crmservice.RetrieveMultiple(query);
    
    return result.Entities.Select(c => c.ToEntity<ImportEntityMapping>()).First().SourceEntityName;
    

    Regards, 

    Daniel

    • Proposed as answer by thuld Wednesday, 20 April, 2011 9:03 AM
    Wednesday, 20 April, 2011 9:03 AM
  • I have a similiar solution and I am running into an issue.  I have a user role set up so it does not customize the schema if bad data comes in.  The goal is to have no bad data come in, but if it does happened to, the permission will restrict the creation of additional picklist values or field length.

    The max length of a zip code is 5 digits.  When ever I import a zip code that is 5 digits, I get an error saying the "The target field could not be increased".  This only occurs when the zip code is 5 digits.  It doens't seem to impact the data being imported, but it is reporting the error during the transform process. 

    When I import under system admin, no error is reported and the field is not increased.  I am thinking this might be a bug in CRM, but did anyone else run into this?

    Monday, 15 August, 2011 4:42 PM
  • Is there a Solution in this issue?
    Monday, 5 September, 2011 9:20 AM