Upload File (image/audio/video) to FeedItem in chunks - salesforce

​public static String upload(String folderId, String documentId, String fileName, String base64BlobValue) {
if(documentId == '' || documentId == null) {
Document document = new Document(Name=fileName, FolderId=folderId, Body=EncodingUtil.Base64Decode(base64BlobValue), IsPublic=true);
insert document;
return document.Id;
} else {
Document document = [select Id, Body from Document where Id = :documentId];
update new Document(Id = documentId, Body = EncodingUtil.Base64Decode(EncodingUtil.Base64Encode(document.Body) + base64BlobValue));
return documentId;
}
}
I'm trying to upload files (image/audio/video) to FeedItem in chunks, the above example shows how I'm doing the same while writing to a document. But If try to do the same with FeedItem it says "FeedItem.ContentData is not writeable" so I've tried the following code:
FeedItem imageFeedItem = [select Id, ContentData, RelatedRecordId from FeedItem where Id = :imageFeedItemId];
ContentVersion content = new ContentVersion();
content.versionData = EncodingUtil.Base64Decode(EncodingUtil.Base64Encode(imageFeedItem.ContentData) + base64BlobValue);
content.pathOnClient = fileName;
content.ContentDocumentId = [Select ContentDocumentId from ContentVersion where id=:imageFeedItem.RelatedRecordId].ContentDocumentId;
insert content;
But this creates ContentVersions of incomplete chunks. Any pointers on how can I achieve this more neat.
Thanks

You can try setting IsMajorVersion to false
ContentVersion content = new ContentVersion();
content.versionData = EncodingUtil.Base64Decode(EncodingUtil.Base64Encode(imageFeedItem.ContentData) + base64BlobValue);
content.pathOnClient = fileName;
content.IsMajorVersion = false;
content.ContentDocumentId = [Select ContentDocumentId from ContentVersion where id=:imageFeedItem.RelatedRecordId].ContentDocumentId;
insert content;
I hope this help!

Related

Salesforce- retrieve Contact.opportunites

Here is snippet I am using in my anonymous window . Purpose is to retrieve Opportunities of a contact. Even after adding opportunity contact role , contact.Opportunities.size is resulting in zero (last debug line). Am I missing something ? you may use the below code directly.
Update: able to get size now but same logic doesn't work for code coverage in test class . details listed below:
Only 'if' part of controller is covered and 'else' part is never covered even though size of contact.opportunities is more than 0.
Controller method :
public PageReference sendingEmail() {
//contact1 has query records
sizeVar = contact1.Opportunities.size();
if(contact1.npo02__OppAmountLastYear__c>0 ) {
if(sizeVar==0) {
// if size is 0 then navigate to a particular vf page
PageReference pr = Page.NoDonationOrNoEmail;
pr.getParameters().put('id',(String)contact1.id);
pr.setRedirect(true);
return pr;
}
else
{ //when contact.opportunities size is more than 0 then navigate to
other vf page.
PageReference pr1 = Page.NoPrint;
pr1.getParameters().put('id',(String)contact1.id);
pr1.setRedirect(true);
return pr1;
}
} return null;
}
Test Class:
//creating account
Account a = new Account();
a.Name = 'Test Co.';
a.BillingStreet = '298 S. Ringo Street';
a.BillingCity = 'Little Rock';
insert a;
//Creating contact
Contact contact1 = new Contact();
contact1.FirstName = 'Paul';
contact1.LastName = 'Test';
contact1.AccountId = a.id;
contact1.npo02__OppAmountLastYear__c=100;
insert contact1;
//creating opportunity
Opportunity o = new Opportunity();
o.RecordType = [SELECT Id, Name, DeveloperName FROM RecordType
WHERE Name = 'Membership' LIMIT 1];
o.Name = 'New Record';
o.StageName = 'Posted';
o.AccountId = contact1.AccountId;
o.CloseDate = Date.today();
o.Description = 'Test Record';
insert o;
//creating opportunity contact role
OpportunityContactRole ocr = new OpportunityContactRole();
ocr.ContactId = contact1.Id;
ocr.OpportunityId = o.Id;
ocr.IsPrimary = TRUE;
ocr.Role = 'Decision Maker';
insert ocr;
System.debug('created opportunity contact role for primary');
Update o;
contact1 = [SELECT Id, Name,(SELECT id FROM opportunities) FROM
Contact WHERE Id=:contact1.Id];
PageReference pr = Page.NoPrint;
pr.getParameters().put('id', String.valueOf(contact1.id));
Test.setCurrentPage(pr);
ApexPages.StandardController cont5 = new
ApexPages.StandardController(contact1);
BulkEmailController testAccPlan = new
BulkEmailController(cont5);
testAccPlan.sendingEmail();
When you create and insert a record, you don't get formula fields nor you can navigate lookup. The same goes with child relationship.
You have to query the fields you need.
Change Update contact1; to contact1 = [SELECT Id, (SELECT Id FROM Opportunities) FROM Contact WHERE Id = :contact1.Id]; and the last debug line will print 1.

Visualforce email template unit test

I have a trigger that sends an email using a VF template. I'm trying to write a unit test for that code, but keep running into a problem when trying to create the test data.
Here is my test method:
static TestMethod void testQuestionAttachment(){
Id profileId = SYSTEM_ADMIN_PROFILE_ID;
List<User> users = TestUtils.createUsers(profileId, 1);
insert users;
string templateText = '<messaging:emailTemplate subject="{!relatedTo.Name}" recipientType="User" relatedToType="Cutom_Object__c"><messaging:htmlEmailBody ><ul><li>test content</li></ul></messaging:htmlEmailBody></messaging:emailTemplate>';
EmailTemplate template = new EmailTemplate(
developerName = 'TestEmailVFTemplate',
TemplateType= 'visualforce',
FolderId = users[0].Id,
Name = 'TestEmailVFTemplate',
IsActive = true);
template.HtmlValue = templateText;
template.Body = templateText;
System.runAs(users[0]){
insert template;
}
...
And it fails with FIELD_INTEGRITY_EXCEPTION, <messaging:emailTemplate> is required and must be the outermost tag in the markup at line 1 column 1: [Markup].
I really don't understand why this isn't working. I must be missing something...
The issue is with your TemplateType = 'Visualforce'. Instead change it to template.Markup=templateText;. Markup field can be used to assign the body of visualforce template. I tried it in my test class.
Refer below example:
static #isTest void myTest () {
Profile pf = [SELECT Id,Name FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
User usr = new User(
Alias = 'usralias',
Email = 'theuser#email.com',
Emailencodingkey = 'UTF-8',
Lastname = 'user_lastname',
Languagelocalekey = 'en_US',
Localesidkey = 'en_US',
Profileid = pf.Id,
Timezonesidkey = 'America/Los_Angeles',
Username = Math.random() + 'test#testuser.com',
CompanyName = 'the company',
UserRoleId='00E28000000zqCy'
);
insert usr;
string templateText = '<messaging:emailTemplate subject="{!relatedTo.Name}" recipientType="User" relatedToType="Custom_Object__c"><messaging:htmlEmailBody ><ul><li>test content</li></ul></messaging:htmlEmailBody></messaging:emailTemplate>';
EmailTemplate template = new EmailTemplate(DeveloperName = 'TestEmailVFTemplate', TemplateType= 'Visualforce', FolderId = UserInfo.getUserId(),
Name = 'TestEmailVFTemplate',
IsActive = true);
template.Markup=templateText;
System.runAs(usr){
insert template;
}
}

Unable to send attachment - Salesforce Docusign API

I am trying to send attachment (record has one attachment) in opportunity record via Apex and Docusign "CreateAndSendEnvelope" API.
But I am getting this error "The DocuSign EnvelopeId:Exception - System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: An Error Occurred during anchor tag processing. Invalid document faultcode=soap:Client faultactor=https://demo.docusign.net/api/3.0/dsapi.asmx"
Below is the piece of code used.
// Render the contract
System.debug('Rendering the contract');
PageReference pageRef = new PageReference('/apex/RenderContract');
pageRef.getParameters().put('id',contract.Id);
//Blob pdfBlob = pageRef.getContent();
Attachment att = [SELECT Id, Name, Body, ContentType FROM Attachment WHERE Parentid = :contract.Id LIMIT 1];
Blob pdfBlob = att.Body;
// Document
DocuSignAPI.Document document = new DocuSignAPI.Document();
document.ID = 1;
document.pdfBytes = EncodingUtil.base64Encode(pdfBlob);
document.Name = 'Contract';
document.FileExtension = 'pdf';
envelope.Documents = new DocuSignAPI.ArrayOfDocument();
envelope.Documents.Document = new DocuSignAPI.Document[1];
envelope.Documents.Document[0] = document;
// Recipient
System.debug('getting the contact');
Contact contact = [SELECT email, FirstName, LastName
from Contact where id = :contract.CustomerSignedId];
DocuSignAPI.Recipient recipient = new DocuSignAPI.Recipient();
recipient.ID = 1;
recipient.Type_x = 'Signer';
recipient.RoutingOrder = 1;
recipient.Email = contact.Email;
recipient.UserName = contact.FirstName + ' ' + contact.LastName;
// This setting seems required or you see the error:
// "The string '' is not a valid Boolean value.
// at System.Xml.XmlConvert.ToBoolean(String s)"
recipient.RequireIDLookup = false;
envelope.Recipients = new DocuSignAPI.ArrayOfRecipient();
envelope.Recipients.Recipient = new DocuSignAPI.Recipient[1];
envelope.Recipients.Recipient[0] = recipient;
// Tab
DocuSignAPI.Tab tab1 = new DocuSignAPI.Tab();
tab1.Type_x = 'SignHere';
tab1.RecipientID = 1;
tab1.DocumentID = 1;
tab1.AnchorTabItem = new DocuSignAPI.AnchorTab();
tab1.AnchorTabItem.AnchorTabString = 'By:';
DocuSignAPI.Tab tab2 = new DocuSignAPI.Tab();
tab2.Type_x = 'DateSigned';
tab2.RecipientID = 1;
tab2.DocumentID = 1;
tab2.AnchorTabItem = new DocuSignAPI.AnchorTab();
tab2.AnchorTabItem.AnchorTabString = 'Date Signed:';
envelope.Tabs = new DocuSignAPI.ArrayOfTab();
envelope.Tabs.Tab = new DocuSignAPI.Tab[2];
envelope.Tabs.Tab[0] = tab1;
envelope.Tabs.Tab[1] = tab2;
System.debug('Calling the API');
try {
DocuSignAPI.EnvelopeStatus es
= dsApiSend.CreateAndSendEnvelope(envelope);
envelopeId = es.EnvelopeID;
} catch ( CalloutException e) {
System.debug('Exception - ' + e );
envelopeId = 'Exception - ' + e;
}
Any ideas how to overcome this error?
Thanks.
The Original Poster's (OP's) comment is
it worked fine on rendering the whole record to pdf...but now i tried sending attachments only instead of whole record.. i started to get this error.
So my guess is that the envelope request has a document problem.
Best way to debug: see what is being sent to the DocuSign platform.
Try the beta API logger or the regular logger. Then add the log to your question by editing your question.
This problem came across me with same error .
" An Error Occurred during anchor tag processing. Invalid document faultcode=soap:Client faultactor=https://demo.docusign.net/api/3.0/dsapi.asmx "
you need to replace anchor tab string with desired string given in your attached document where signature is required.
Replace :
tab1.AnchorTabItem.AnchorTabString = 'By:';
tab2.AnchorTabItem.AnchorTabString = 'Date Signed:';
To :
tab1.AnchorTabItem.AnchorTabString = 'Signature label in your document';
tab2.AnchorTabItem.AnchorTabString = 'Signature label in your document';

Add Account attachment to Chatter Post Automatically

I have need to add account attachments in Salesforce to the account chatter feed automatically. I've got the following code, which adds a chatter post for every object, not just account attachments, how can I make it specific to accounts? Or how can I make it specific to a certain file name?
trigger AttachFileToAccountFeed on Attachment (before insert) {
ID accountId;
list<FeedItem> listOfFeedFiles = new List<FeedItem>();
if(Trigger.isBefore){
for(Attachment attachment : trigger.new){
string checkIfAccount = string.valueof(attachment.description);
{
//Adding a Content post
accountId = attachment.ParentId;
FeedItem post = new FeedItem();
post.ParentId = accountId; //eg. Opportunity id, custom object id..
post.Body = 'Attachment added';
post.Type = 'ContentPost';
post.ContentData = attachment.body;
post.ContentFileName = attachment.Name;
post.Title = attachment.Name;
listOfFeedFiles.add(post);
}
}
}
if(listOfFeedFiles!=null){
insert listOfFeedFiles;
}
}
Here's what I ended up using:
trigger AttachFileToAccountFeed on Attachment (before insert) {
ID accountId;
list<FeedItem> listOfFeedFiles = new List<FeedItem>();
if(Trigger.isBefore){
for(Attachment attachment : trigger.new) {
// ensure the Id is an Account Id
if(attachment.ParentId.getSObjectType() != Account.SObjectType)
continue;
// ensure file contains Signed Authorization in File Name
if(attachment.Name.contains('Signed Authorization')) {
//Adding a Content post
accountId = attachment.ParentId;
FeedItem post = new FeedItem();
post.ParentId = accountId;
post.Body = 'Attachment added';
post.Type = 'ContentPost';
post.ContentData = attachment.body;
post.ContentFileName = attachment.Name;
post.Title = attachment.Name;
listOfFeedFiles.add(post);
}
}
}
if(listOfFeedFiles!=null){
insert listOfFeedFiles;
}
}

System.DmlException: Update failed. first error: INVALID_CROSS_REFERENCE_KEY,

i am trying to update a ObjectPermissions Object in slaesforce such that a profile will get the permissions to access a object similar to other profile.
i write a code
my code segment is
PermissionSet set1 = [SELECT Id From PermissionSet
WHERE profileId = : SourceProfileId LIMIT 1] ;
PermissionSet set2 = [SELECT Id FROM PermissionSet
WHERE profileId = : TargetProfileId LIMIT 1];
List<ObjectPermissions> oo = [SELECT Id,
SObjectType,
ParentId,
PermissionsCreate,
PermissionsDelete,
PermissionsEdit,
PermissionsModifyAllRecords,
PermissionsRead,
PermissionsViewAllRecords
FROM ObjectPermissions
WHERE ParentId = : set1.id];
List<ObjectPermissions> oo1 = [SELECT ParentId,
Id,
SObjectType,
PermissionsCreate,
PermissionsDelete,
PermissionsEdit,
PermissionsModifyAllRecords,
PermissionsRead,
PermissionsViewAllRecords
FROM ObjectPermissions
WHERE ParentId = : set2.Id];
Map<String , ObjectPermissions> source_obj = new Map<String, ObjectPermissions>();
Map<String , ObjectPermissions> target_obj = new Map<String, ObjectPermissions>();
for (ObjectPermissions o : oo) {
source_obj.put(o.SObjectType, o);
}
for (ObjectPermissions o : oo1) {
target_obj.put(o.SObjectType, o);
}
ObjectPermissions target, source;
for (String s : source_obj.keySet() ) {
if (target_obj.containsKey(s)) {
target = target_obj.get(s);
source = source_obj.get(s);
System.debug('Source is:' + source);
System.debug('Target is : ' + target);
target.PermissionsCreate = source.PermissionsCreate;
target.PermissionsDelete = source.PermissionsDelete;
target.PermissionsEdit = source.PermissionsEdit;
target.PermissionsModifyAllRecords = source.PermissionsModifyAllRecords;
target.PermissionsRead = source.PermissionsRead;
target.PermissionsViewAllRecords = source.PermissionsViewAllRecords;
update target;
} else {
target = new ObjectPermissions(SObjectType = s );
source = source_obj.get(s);
target.PermissionsCreate = source.PermissionsCreate;
target.PermissionsDelete = source.PermissionsDelete;
target.PermissionsEdit = source.PermissionsEdit;
target.PermissionsModifyAllRecords = source.PermissionsModifyAllRecords;
target.PermissionsRead = source.PermissionsRead;
target.PermissionsViewAllRecords = source.PermissionsViewAllRecords;
insert target;
}
}
when the control come to this line
update target;
it is giving error
Update failed. First exception on row 0 with id 110i0000007KNEvAAO; first error: INVALID_CROSS_REFERENCE_KEY, You can't create, edit, or delete records for this permission set parent because it's associated with a profile. Parent ID: 0PSi00000009BE9: []
i am unable to figure it out why i am facing this error please some one help to resolve this error
the persmission set you are trying to update is already associated with some of your profile with id=0PSi00000009BE9
go to
https://ap1.salesforce.com/0PSi00000009BE9 and check there.

Resources