Sample Code:
//Here I'm creating a sample txt file. List<Folder> folderList = [SELECT Id, Name From Folder WHERE Name = 'Test Documents']; String docFolderId = folderList[0].Id; //Get Folder Id String fileContent = 'This is a test file.'; //File Content Datetime currentDT = System.now(); String fileName = currentDT.format('yyyy-MM-dd-HH-mm-ss-SSS'); //Timestamp for unique file name Document doc = new Document(); doc.Name = fileName; doc.Body = Blob.valueOf(fileContent); doc.ContentType = 'text/plain'; doc.Type = 'txt'; doc.FolderId = docFolderId; doc.IsPublic = true; Insert doc;