Hi Ishan, Please refer below code.
Thanks,Parteek​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)
{
} */
}
}
}
2 answers