Help Center

Import API

Propel Import can be used programmatically. Below is the Import Class Body that documents how to programmatically interface with Propel's Import Service.

An understanding of Importing Data is required before using this API.

Note: You will need to ensure all the column headings are mapped through the GUI interface prior to programmatically invoking Import.

 

@RestResource(urlMapping='/Import/*')
Global with sharing class ImportAPI {

  /**
   *  pass the chatter file into Propel import, return the results of the load
   *  uses the internal system default mapping
     *
     *   REST GET url format : INSTANCE/services/apexrest/PDLM/Import/06936000004tWwK?hasItem=true&hasBom=true
     *   Options of the form ?hasATT=true&hasBOM set the operation of the import
     *
   *
     */
    @httpGet
    global static List<RowResult> importBOM() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String chatterFile = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
    //System.debug(LoggingLevel.WARN, chatterFile);
        //System.debug(LoggingLevel.WARN,req.params);

        // read the chatter file into a string
        string content;
        ContentDocument doc = [ select LatestPublishedVersionId from ContentDocument where id = :chatterFile limit 1];
        ContentVersion ver = [ select id, VersionData from ContentVersion where id = :doc.LatestPublishedVersionId limit 1];
        content = ver.VersionData.toString();

        //
        // Set defaults here, then read from get request to override those
        //                                  0     1     2      3      4      5     6     7      8       9
        Boolean[] options = new Boolean[]{  true, true, false, false, false, true, true, false, false, false};
        System.debug(LoggingLevel.WARN,'default =>' + options);
        for ( string opt  : req.params.keySet() ) {
            System.debug(LoggingLevel.WARN, opt + ' is ' + req.params.get(opt));
            if ( opt.equalsIgnoreCase('hasItem')) { options[0] = Boolean.valueOf(req.params.get(opt));                }
            if ( opt.equalsIgnoreCase('hasBOM')) { options[1] = Boolean.valueOf(req.params.get(opt));                }
            if ( opt.equalsIgnoreCase('hasAML'))  { options[2] = Boolean.valueOf(req.params.get(opt));                }
            if ( opt.equalsIgnoreCase('hasATT')) { options[3] = Boolean.valueOf(req.params.get(opt));                }
            if ( opt.equalsIgnoreCase('matchDescription')) { options[4] = Boolean.valueOf(req.params.get(opt));                }
            if ( opt.equalsIgnoreCase('isUpsertingItem')) { options[5] = Boolean.valueOf(req.params.get(opt));                }
            if ( opt.equalsIgnoreCase('isUpsertingBOM')) { options[6] = Boolean.valueOf(req.params.get(opt));                }
            if ( opt.equalsIgnoreCase('isReplacingBOM')) { options[7] = Boolean.valueOf(req.params.get(opt));                }
            if ( opt.equalsIgnoreCase('isUpsertingAML')) { options[8] = Boolean.valueOf(req.params.get(opt));                }
            if ( opt.equalsIgnoreCase('isReplacingAML')) { options[9] = Boolean.valueOf(req.params.get(opt));                }
        }
        System.debug(LoggingLevel.WARN,'parsed =>'+ options);
      //
      // build the mapping from all possible mappings ever done in this org, usually not > 100 even on large orgs
      //
        string mapping ='';
        map<string, Mapping__c> allMap = Mapping__c.getAll();
        for ( Mapping__c  m: allMap.values() ) {
            mapping += m.Name+ ','+m.Api_Name__c +CSVReader.NL ;   // build a master map ( every one ever used)
        }
         System.debug(LoggingLevel.WARN,mapping);

        ImportService srv = new ImportService(content,  mapping, options);
        return DataLoadCon.loadDataUsingMapWorker( srv );
    }

}

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.