function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Naitik RaiNaitik Rai 

How can we Insert Note Under Opportunity?

Hi Friends 
 I want to Insert Note under related list of Opportunity
 using Apex Code Script.
User-added image
Can Someone Please Help me to acheive this task.
Any help would be appreciated.
Best Answer chosen by Naitik Rai
sfdcMonkey.comsfdcMonkey.com
HI Naitik ,
 this is file (content document object), use below code to insert Notes in opportunity :
ContentNote cn = new ContentNote(
   Title = 'test Note',
   Content = Blob.valueOf('Test Content')
 );
insert cn ;   

//create ContentDocumentLink  record 
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = '0066F00000qNVUv'; // update opportunity id here
cdl.ContentDocumentId = cn.Id;
cdl.shareType = 'V';
insert cdl;
Kindly mark this as solved if this will helps you
Thanks

 

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Naitik,

Sorry for this issue you are encountering.

I assume you are talking about adding the text from that notes field into the note&attachement section of opportunity record. You can do so by using the below code.
note n = new note();
n.parentId= '0000000XXXXXXX; //Id of the opportunity for which you want to add this note
n.body='inserting note from code'; // body of the note. this should be populated from the notes field that you have shown in the screenshot
n.title='New Note from code'; // the title of the note
n.isPrivate=false;  // if private it will be visible only for the owner and any user with modify all permission
insert n; //insert the record
find more info here In your case what you should do is, first insert the opportunity and then take the opportunity id and then insert the note.

Please let us know if this helps.

Kindly mark this as solved if the information was helpful.

Thanks,
Nagendra
sfdcMonkey.comsfdcMonkey.com
hi by using below code you can insert new note to opportunity in apex :
Note oNote = new Note();
 oNote.Title ='my opp note';
 oNote.Body = 'test note body';
 oNote.ParentId = '0066F00000qNVUv'; // opportyniy id 
insert oNote;
Thanks kindly let us know if it helps you
 
Naitik RaiNaitik Rai
Hi Nagendra & Piyush Thanks for the response 
        But i have already tried this and it is inserting in Notes & Attachment.
        But i want to insert record in 'Note' shown in Screenshot
DevADSDevADS
Naitik,

There could be two reasons -
1. Probably the Note is custom object and you need to insert records in the custom object associating it with Opportunity.
2. Attachment are being replaced with Salesforce files  (https://help.salesforce.com/articleView?id=Attachments-are-not-returned-in-Lightning-Experience-search&language=en_US&type=1)so that could be a reason the that It's not showing the attachment option. So use following code to attach Note to Opportunity.
Note noteObj = new Note();
noteObj.Title ='Follow up with customer';
noteObj.Body = 'test body';
noteObj.ParentId = <opportunityId>;

insert noteObj;
Hope this would resolves your issue.

Happy Coding!
 
sfdcMonkey.comsfdcMonkey.com
HI Naitik ,
 this is file (content document object), use below code to insert Notes in opportunity :
ContentNote cn = new ContentNote(
   Title = 'test Note',
   Content = Blob.valueOf('Test Content')
 );
insert cn ;   

//create ContentDocumentLink  record 
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = '0066F00000qNVUv'; // update opportunity id here
cdl.ContentDocumentId = cn.Id;
cdl.shareType = 'V';
insert cdl;
Kindly mark this as solved if this will helps you
Thanks

 
This was selected as the best answer