Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
i want to insert record xls file using email handler. Any ideas?
2 answers
  1. Aug 7, 2017, 12:47 PM
    Hi Ishan,

    Please refer below code.

    trigger insertAttachmentRecordtoObjectonCase on case (after update) {

    public Blob csvFileBody{get;set;}

    public string csvAsString{get;set;}

    public String[] csvFileLines = new String[]{};

    string[] csvHeaders;

    public List<account> acclist = New List<Account>();

    Map<String,Integer> mapfileHeaders = new Map<String,Integer>();

    ID caseid;

    String strHeader;

    if(trigger.isupdate){

    if(checkRecursiveinsertAttachment.runonce()){

    // try{

    // csvAsString = csvFileBody.toString();

    // csvFileLines = csvAsString.split('\n');

    for(case case1 : trigger.new){

    caseid = case1.id;

    }

    System.debug(caseid);

    EmailMessage Em = [Select ID,ParentID from EmailMessage where ParentId =:caseid AND HasAttachment = true AND Incoming = True];

    System.debug(Em);

    Attachment att= [Select Id,ParentId, Name,body,ContentType From Attachment where ParentID =:Em.id limit 1];

    System.debug(att);

    String[] filelines = new String[]{};

    string nameFile;

    nameFile=att.body.toString();

    filelines = nameFile.split('\n');

    String[] fileHeaders;

    System.debug(filelines);

    System.debug(filelines.size());

    for(Integer i=0;i < 1;i++){

    fileHeaders = filelines[i].split(',');

    }

    System.debug(fileHeaders);

    System.debug('Test Debug 1');

    for(Integer i=1;i < filelines.size();i++){

    csvHeaders = filelines[i].split(',');

    }

    System.debug(csvHeaders.size());

    for(Integer i=0;i < csvHeaders.size();i++){

    // System.debug(fileHeaders[i]);

    mapfileHeaders.put(fileHeaders[i],i);

    }

    System.debug(mapfileHeaders);

    for(Integer i=1;i < filelines.size();i++){

    Account accObj = new Account();

    string[] csvRecordData = filelines[i].split(',');

    System.debug(mapfileHeaders.values());

    System.debug(mapfileHeaders.get('Type'));

    if(mapfileHeaders.containsKey('Type')){

    integer var3 = mapfileHeaders.get('Type');

    System.debug(csvRecordData[var3]);

    accObj.Type = csvRecordData[var3];

    }

    if(mapfileHeaders.containsKey('AccountSource')){

    Integer var4 = mapfileHeaders.get('AccountSource');

    accObj.AccountSource = csvRecordData[var4];

    }

    if(mapfileHeaders.containsKey('Accountnumber')){

    integer var1 = mapfileHeaders.get('Accountnumber');

    accObj.accountnumber = csvRecordData[var1];

    }

    if(mapfileHeaders.containsKey('Company Name')){

    Integer var2 = mapfileHeaders.get('Company Name');

    accObj.name = csvRecordData[var2];

    }

    if(mapfileHeaders.containsKey('Industry')){

    Integer var5 = mapfileHeaders.get('Industry');

    accObj.Industry = csvRecordData[var5];

    }

    if(mapfileHeaders.containsKey('Phone')){

    Integer var6 = mapfileHeaders.get('Phone');

    accObj.Phone = csvRecordData[var6];

    }

    if(mapfileHeaders.containsKey('NumberOfEmployees')){

    Integer var7 = mapfileHeaders.get('NumberOfEmployees');

    // accObj.NumberOfEmployees = csvRecordData[var7];

    }

    acclist.add(accObj);

    }

    insert acclist;

    /* }

    catch (Exception e)

    {

    } */

    }

    }

    }

    Thanks,

    Parteek​
Loading
0/9000