Formula to change the document's parent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Russ
    Senior Member
    • Feb 2022
    • 423

    Formula to change the document's parent

    Hey guys, very interesting and tricky (for me), I will spend hours trying to dig, maybe someone can help me with a formula?

    If lead has 50 documents attached, now it is an opportunity, how to link all these documents with an opportunity?

    Any easy way? Probably formula?

    Since it is one to many relation, would be nice to link to an opportunity that is attached to the lead


    Thanks
  • dreginald
    Member
    • Sep 2018
    • 89

    #2
    Create Relationship One to Many> Lead to Documents
    In Leads create an Integer field " Count of documents" and write formula entity\countRelated(LINK, FILTER) to get the count from Documents and workflow in Document to update the formula in Leads on creation.

    Write a Workflow
    Tgt Entity> Leads
    Trigger> On Updation
    Trigger condition > Count of Documents is changed and Count of Documents greater than equal to 50
    Action> Create related record> Opportunity and map the fields you require in the opportunity (Create Additional One to Many relationship between Opportunity and Documents)
    Link the documents in the leads to Opportunity by writing this formula in the workflo
    documentsIds=lead.documentsIds
    Last edited by dreginald; 11-01-2023, 05:05 AM.

    Comment

    • rabii
      Active Community Member
      • Jun 2016
      • 1250

      #3
      Originally posted by Russ
      Hey guys, very interesting and tricky (for me), I will spend hours trying to dig, maybe someone can help me with a formula?

      If lead has 50 documents attached, now it is an opportunity, how to link all these documents with an opportunity?

      Any easy way? Probably formula?

      Since it is one to many relation, would be nice to link to an opportunity that is attached to the lead


      Thanks
      > If lead has 50 documents attached, now it is an opportunity, how to link all these documents with an opportunity? DO YOU MEAN WHEN A LEAD IS CONVERTED TO AN OPPORTUNITY ?

      If yes then the code below should work for you, it will loop through all documents linked to the current lead (once the lead is converted and has documents and been converted to opportunity) you can change the condition of if statement. But this code will link any existing documents linked to the lead and will map them to the opportunity.

      PHP Code:
      if (
          status == 'Converted' &&
          createdOpportunityId &&
          entity\countRelated('documents') > 0
      ) {
          $count = entity\countRelated('documents');
          
          $documentsIds = record\findRelatedMany('Lead', id, 'documents', $count, 'createdAt', 'desc');
          
          $i = 0;
          
          while ($i < array\length($documentsIds)) {
              
              record\relate('Opportunity', createdOpportunityId, 'documents', array\at($documentsIds, $i));
              
              $i = $i + 1;
          }
      }

      I hope this is what you are trying to achieve. You can try this on one lead to confirm it is the desired function you want and then apply this for future lead conversion. If you want to apply this to all existing lead you can select all the leads you need and apply mass action recalculate formula.
      Rabii
      Web Dev

      Comment

      • Russ
        Senior Member
        • Feb 2022
        • 423

        #4
        thanks, will check!!

        Comment

        • Russ
          Senior Member
          • Feb 2022
          • 423

          #5
          Originally posted by rabii

          &gt; If lead has 50 documents attached, now it is an opportunity, how to link all these documents with an opportunity? DO YOU MEAN WHEN A LEAD IS CONVERTED TO AN OPPORTUNITY ?

          If yes then the code below should work for you, it will loop through all documents linked to the current lead (once the lead is converted and has documents and been converted to opportunity) you can change the condition of if statement. But this code will link any existing documents linked to the lead and will map them to the opportunity.

          PHP Code:
          if (
          status == 'Converted' &&
          createdOpportunityId &&
          entity\countRelated('documents') > 0
          ) {
          $count = entity\countRelated('documents');
          
          $documentsIds = record\findRelatedMany('Lead', id, 'documents', $count, 'createdAt', 'desc');
          
          $i = 0;
          
          while ($i < array\length($documentsIds)) {
          
          record\relate('Opportunity', createdOpportunityId, 'documents', array\at($documentsIds, $i));
          
          $i = $i + 1;
          }
          }

          I hope this is what you are trying to achieve. You can try this on one lead to confirm it is the desired function you want and then apply this for future lead conversion. If you want to apply this to all existing lead you can select all the leads you need and apply mass action recalculate formula.



          It worked!

          Thanks a lot!!!!

          Comment


          • rabii
            rabii commented
            Editing a comment
            you are welcome
        Working...