Important Update: Community URLs redirect issues are partially resolved. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Object JavaScript Copy Previous Text

ShaunMartinLaw2
Advocate

Hi all,
Hoping some of you Archer JavaScript  Hero's can point me in the right direction here.

We have a Questionnaire that runs Quarterly. Sometimes the answers don't change too much from Quarter to Quarter. To assist the submitter we were first asked to present the previous Quarter's answers on layout to help with the latest submission. Now we've been asked to assist further & enable a copy / paste function in one click (I know!!).

I implemented the below simple function, which almost works.

<script type='text/javascript'>
function copyPrevAnswer(whereToCopy, textToCopy) {
whereToCopy.innerHTML = textToCopy.innerHTML;
}
</script>

When you click in to edit the copy however, it disappears for some reason.


Adding an additional:
whereToCopy.focus();

...before (and/or after) setting the innerHTML, helps a little, but seems to be inconsistent, long clicks, seem to work ok... weird!

Anyone know what's happening any advice?


2 ACCEPTED SOLUTIONS

Accepted Solutions

@ShaunMartinLaw2 Archer's HTML is a little complex how fields and associated data is laid out.  Here's POC to copy just one text are field to another upon clicking a button to get you started.

<div class="workflow-app-buttons" >
     <a title="Copy Field" class="tb-btn-link-left" id="btnCopyField" href="javascript&colon;void(0);">
          <div class="tb-btn" data-icon="&#xE08d" data-icon-pos="left">&nbsp;Copy Field</div>
     </a>
</div>

<script>
     $('#btnCopyField').click(function(){
          copyField("Text Area", "Text Area Two");
     });

     function copyField(from, to) {
          var fromFieldId = lookupFieldId(from);
          var toFieldId = lookupFieldId(to);
          if (fromFieldId && toFieldId) {
               setTextNumericField(toFieldId, $('input[id$="' + fromFieldId + 'c"]').val());
          }
     }

     function setTextNumericField(fld,text) {

          if($CM._fields[fld].type == 1 && $CM._fields[fld].displayControl == 1) {
               setTimeout( function(){
                    $('input[id$="'+ fld +'c"]').focus({preventScroll: true}).val(text).blur();
               }, 20);
          } else {
               $('input[id$="'+ fld +'c"]').val(text);
               $('div[id$="'+ fld +'c_text"]').html(text);
          }

          //Check to see if the field is read-only
          if($('input[id$="'+ fld +'c"]').parent().parent().find('.readOnly')) $('input[id$="'+ fld +'c"]').parent().parent().find('.readOnly').html(text);

     }

     function lookupFieldId(fldName){
          var goFindId = null;

          try{
               $('.FieldLabel').each(function(){
                    if(($(this).text().indexOf(fldName + ':') != -1) && ($(this).text().indexOf(fldName + ':') == 0)){
                         goFindId = $(this).find("span")[0].id;
                         return false;
                    }
               });
          } catch (err) {console.log(err)}
          try {if (!goFindId) goFindId = $('.SectionLabel:findField("' + fldName + '")')[0].id;} catch (err) {}
          try {if (!goFindId) goFindId = $('.SubSectionLabel:findField("' + fldName + '")')[0].id;} catch (err) {}
          try {if (!goFindId) goFindId = $('.SubSectionLabelCollapsible:findField("' + fldName + '")')[0].id;} catch (err) {}
          try {if (!goFindId) goFindId = $('.SubSectionLabelCollapsibleToolTip:findField("' + fldName + '")')[0].id;} catch (err) {}
          return goFindId ? $LM._layoutItems[goFindId.replace( /^\D+/g, '')].fieldId : 0;
     }

     $.expr[':'].findField = function(a, i, m) {
          return $(a).text().replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_').match("^" + m[3].replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_') + "$");
     };
</script>

 Advisory Consultant

View solution in original post

ShaunMartinLaw2
Advocate

Still no joy, I've alerted lots of different variations on this, bit nothing returned unfortunately.
When I hardcode the element & grab the innerHTML, as the second param it works. 

setTextNumericField(toFieldId, setTextNumericField(toFieldId, document.getElementById('master_DefaultContent_rts_s5590_f19369c').innerHTML);

 

View solution in original post

8 REPLIES 8

DavidPetty
Archer Employee
Archer Employee

@ShaunMartinLaw2 it's hard to tell what's going on with just snippets of code.  Can you post the complete custom object?

 Advisory Consultant

ShaunMartinLaw2
Advocate

Thanks David. 

Here's how I am calling the above function, by providing a link to click, which calls the function above on click:

<div class="copy-text" id="btnUIDcopyPrevRiskOutlookJust">
<a title="Copy from previous answer" class="SystemURL" href="javascript&colon;void(0);" onclick="copyPrevAnswer(document.getElementById('master_DefaultContent_rts_s5590_f19375c_text'), document.getElementById('master_DefaultContent_rts_s5590_f20161c'))">
&lArr; Copy Previous</a></div>

whereToCopy = Editable Text Area - though shows up as Div when inspect.
TextToCopy = Uneditable textArea - though again copying Div content when inspect.

Both seem ok. Not sure why it clears & why focus() kind of helps.

 

ShaunMartinLaw2
Advocate

Any insights @DavidPetty ? What am I missing? We've had a few in our team wrecking our brains, it feels simple.

@ShaunMartinLaw2 Archer's HTML is a little complex how fields and associated data is laid out.  Here's POC to copy just one text are field to another upon clicking a button to get you started.

<div class="workflow-app-buttons" >
     <a title="Copy Field" class="tb-btn-link-left" id="btnCopyField" href="javascript&colon;void(0);">
          <div class="tb-btn" data-icon="&#xE08d" data-icon-pos="left">&nbsp;Copy Field</div>
     </a>
</div>

<script>
     $('#btnCopyField').click(function(){
          copyField("Text Area", "Text Area Two");
     });

     function copyField(from, to) {
          var fromFieldId = lookupFieldId(from);
          var toFieldId = lookupFieldId(to);
          if (fromFieldId && toFieldId) {
               setTextNumericField(toFieldId, $('input[id$="' + fromFieldId + 'c"]').val());
          }
     }

     function setTextNumericField(fld,text) {

          if($CM._fields[fld].type == 1 && $CM._fields[fld].displayControl == 1) {
               setTimeout( function(){
                    $('input[id$="'+ fld +'c"]').focus({preventScroll: true}).val(text).blur();
               }, 20);
          } else {
               $('input[id$="'+ fld +'c"]').val(text);
               $('div[id$="'+ fld +'c_text"]').html(text);
          }

          //Check to see if the field is read-only
          if($('input[id$="'+ fld +'c"]').parent().parent().find('.readOnly')) $('input[id$="'+ fld +'c"]').parent().parent().find('.readOnly').html(text);

     }

     function lookupFieldId(fldName){
          var goFindId = null;

          try{
               $('.FieldLabel').each(function(){
                    if(($(this).text().indexOf(fldName + ':') != -1) && ($(this).text().indexOf(fldName + ':') == 0)){
                         goFindId = $(this).find("span")[0].id;
                         return false;
                    }
               });
          } catch (err) {console.log(err)}
          try {if (!goFindId) goFindId = $('.SectionLabel:findField("' + fldName + '")')[0].id;} catch (err) {}
          try {if (!goFindId) goFindId = $('.SubSectionLabel:findField("' + fldName + '")')[0].id;} catch (err) {}
          try {if (!goFindId) goFindId = $('.SubSectionLabelCollapsible:findField("' + fldName + '")')[0].id;} catch (err) {}
          try {if (!goFindId) goFindId = $('.SubSectionLabelCollapsibleToolTip:findField("' + fldName + '")')[0].id;} catch (err) {}
          return goFindId ? $LM._layoutItems[goFindId.replace( /^\D+/g, '')].fieldId : 0;
     }

     $.expr[':'].findField = function(a, i, m) {
          return $(a).text().replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_').match("^" + m[3].replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_') + "$");
     };
</script>

 Advisory Consultant

ShaunMartinLaw2
Advocate

This is very tidy David, thanks so much.
I got it partially working, copying from an editable field, however, my use case is from a read only non editable field, so failing.
Using an alert, the second param seems to be where the issue lies below, as it returns undefined:

 setTextNumericField(toFieldId, $('input[id$="' + fromFieldId + 'c"]').val());

 

Anytime : D

Try,  

setTextNumericField(toFieldId, $('[id$="' + fromFieldId + 'c"]').text().trim());

 Advisory Consultant

ShaunMartinLaw2
Advocate

Still no joy, I've alerted lots of different variations on this, bit nothing returned unfortunately.
When I hardcode the element & grab the innerHTML, as the second param it works. 

setTextNumericField(toFieldId, setTextNumericField(toFieldId, document.getElementById('master_DefaultContent_rts_s5590_f19369c').innerHTML);

 

Try changing the text() to html() and see if that works.

 Advisory Consultant