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

cancel
Showing results for 
Search instead for 
Did you mean: 

Add New Subform Record on Questionnaire - Custom Object

LiaKonieczny
Contributor III

I wanted to create an "Add New" custom object button for end users to add new comment subform records on a questionnaire (as opposed to using the sticky note method).

 

I created the custom object using the following script (found via the community):


<div style="text-align:center;">
<a class="ContentURL" href="javascript:$('#master_DefaultContent_rts_s6959_ss23141_Add_New')[0].click();">
<img src="../BackgroundImageGenerator.axd?className=StyledButton&classProperties=caption:+++++++++++++++++++++++Ask+a+Question++++++++++++++++++++++;iconSetStyle:VistaRound;baseColor:%23BFDDF5;disabled:False" />
</a>
</div>


However, it is failing to work because of the following section of the script (I believe):

('#master_DefaultContent_rts_s6959_ss23141_Add_New')

 

After inspecting the element of the "Add New" link associated with the subform field, it looks like that one line is unique to each questionnaire record. What can I do to make the button function as intended? Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions

IIya, I've seen on a rare occasion that the section id (s1234) increments when the application is saved in the application builder and don't know why it's happening.

 

I did create a workaround for Lia that would get the correct section id first beforehand.

<div style="text-align:center;">
     <img id="btnAskAQuestion" src="../BackgroundImageGenerator.axd?className=StyledButton&classProperties=caption:+++++++++++++++++++++++Ask+a+Question++++++++++++++++++++++;iconSetStyle:VistaRound;baseColor:%23BFDDF5;disabled:False" />
</div>

<script type="text/javascript">
     var fldId = 21358;

     $('#btnAskAQuestion').click(function(){
          var subfromAddNewId = $('#master_DefaultContent_rts_s' + $LM._layoutItems[$CM._fields[fldId].layoutId].sectionId + '_ss' + fldId + '_Add_New');
          if(subfromAddNewId) subfromAddNewId[0].click();
      });
</script>

 Advisory Consultant

View solution in original post

10 REPLIES 10

DavidPetty
Archer Employee
Archer Employee

Lia are you looking for just one button to create a new sub-form record regardless of the question it's tied to?

 Advisory Consultant

Hey David, yes - I created a new subform (essentially a copy of the questionnaire comments form with more fields) and just want new records to be created upon button click, the associated question is not necessary. That line of code changes from record to record, however, so I don't think I have it right.

Hmm, the id of the Add New element wouldn't change from record to record being it's a field and it's in the same spot on the layout.

 

If you open the browser's developer tools and select the console, see if you get any errors being reported when you open the record and then when clicking on the button.

 Advisory Consultant

jsol5
Advocate II

Looking at a couple of other posts from David Petty

Button to Mimic "Add New" for a Sub-Form 

Button to Mimic "Add New" for a Sub-Form (v6.1) 

 

One of them looks sort of like what you have, except you have an index of 0.  

href="javascript:$('#master_DefaultContent_rts_s6959_ss23141_Add_New')[0].click();"

Maybe try removing the [0]

The example from the thread was like this - href="javascript:$('#master_DefaultContent_rts_ts1970_s1720_Add_New').click();">  

 

In one of the other posts, it was done slightly different where the href="#" and then there is an added onclick event.

<a href="#" onclick="$('#master_DefaultContent_rts_s17687_Add_New').click()"

Jason,

 

Taking out the [0] results in an [object Object] error. I also tried this:

 

<div style="text-align:center;">
<a href="#" onclick="$('#master_DefaultContent_rts_6955_ss23141_Add_New').click();">
<img src="../BackgroundImageGenerator.axd?className=StyledButton&classProperties=caption:++++++++++++++++++++++++++Ask+a+Question++++++++++++++++++++++++++;iconSetStyle:VistaRound;baseColor:%23BFDDF5;disabled:False" />
</a>
</div>

 

which results in the button appearing to be clickable, but no dice. No errors appear on the developers console either. However, when I added [0] to the above..

 

<div style="text-align:center;"> 
<a href="#" onclick="$('#master_DefaultContent_rts_6955_ss23141_Add_New')[0].click();"> 
<img src="../BackgroundImageGenerator.axd?className=StyledButton&classProperties=caption:++++++++++++++++++++++++++Ask+a+Question++++++++++++++++++++++++++;iconSetStyle:VistaRound;baseColor:%23BFDDF5;disabled:False" /> 
</a> 
</div> 

I receive the following error message: 

SCRIPT5007: Unable to get property 'click' of undefined or null reference

David, please see my comments to Jason below. 

You cannot access the A element to click method without using DOM. Thus [0].click(); is needed.

But if it does not work, try vanilla code:

document.getElementById('master_DefaultContent_rts_6955_ss23141_Add_New').click();

IIya, I've seen on a rare occasion that the section id (s1234) increments when the application is saved in the application builder and don't know why it's happening.

 

I did create a workaround for Lia that would get the correct section id first beforehand.

<div style="text-align:center;">
     <img id="btnAskAQuestion" src="../BackgroundImageGenerator.axd?className=StyledButton&classProperties=caption:+++++++++++++++++++++++Ask+a+Question++++++++++++++++++++++;iconSetStyle:VistaRound;baseColor:%23BFDDF5;disabled:False" />
</div>

<script type="text/javascript">
     var fldId = 21358;

     $('#btnAskAQuestion').click(function(){
          var subfromAddNewId = $('#master_DefaultContent_rts_s' + $LM._layoutItems[$CM._fields[fldId].layoutId].sectionId + '_ss' + fldId + '_Add_New');
          if(subfromAddNewId) subfromAddNewId[0].click();
      });
</script>

 Advisory Consultant

Great!