In this case, you need to create a Workflow with:
- Target Entity: Lead
- Trigger Type: After record updated
- Conditions:
- Actions: Execute Formula Script
Code:
$leadId = workflow\targetEntity\attribute('id'); $opportunityId = workflow\targetEntity\attribute('createdOpportunityId'); $opportunityName = workflow\targetEntity\attribute('createdOpportunityName'); $notesIds = record\findMany('Note', 10000, 'createdAt', 'asc', 'parentId=', $leadId, 'type=', 'Post'); $notesCount = array\length($notesIds); $i = 0; while( $i < $notesCount, ( $noteId = array\at($notesIds, $i); $noteCreatedAt = datetime\format(record\attribute('Note', $noteId, 'createdAt'), 'Europe/Kiev'); $notePost = record\attribute('Note', $noteId, 'post'); $noteCreatedById = record\attribute('Note', $noteId, 'createdById'); $noteCreatedByName = record\attribute('Note', $noteId, 'createdByName'); // posts for converted opportunity creating $noteForOpportunityId = record\create( 'Note', 'post', string\concatenate($noteCreatedAt, '\n\n', $notePost), 'type', 'Post', 'parentId', $opportunityId, 'parentType', 'Opportunity', 'parentName', $opportunityName, 'createdById', $noteCreatedById, 'createdByName', $noteCreatedByName, ); // attachments for new posts creating $noteAttachmentsIds = record\attribute('Note', $noteId, 'attachmentsIds'); $newAttachmentsIds = list(); $j = 0; while( $j < array\length($noteAttachmentsIds), ( $noteAttachmentId = array\at($noteAttachmentsIds, $j); $newAttachmentsIds = array\push( $newAttachmentsIds, record\create( 'Attachment', 'role', 'Attachment', 'type', record\attribute('Attachment', $noteAttachmentId, 'type'), 'size', record\attribute('Attachment', $noteAttachmentId, 'size'), 'global', record\attribute('Attachment', $noteAttachmentId, 'global'), 'name', record\attribute('Attachment', $noteAttachmentId, 'name'), 'sourceId', $noteAttachmentId, 'storage', record\attribute('Attachment', $noteAttachmentId, 'storage'), 'field', 'attachments', 'parentId', $noteForOpportunityId, 'parentType', 'Note' ) ); $j = $j + 1; ) ); $i = $i + 1; ) );
Leave a comment: