Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Nested Multifield- Inner multifield data is not populating

Avatar

Level 1

I am trying to create a nested multifield but the inner multifield data is not populating.

Attatching my sling model and sit-ly code.

Please help I am a fresher.site_ly.PNGmodel_part3.PNGmodel_part2.PNGModel_part1.PNG

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Sharing .content.xml file associated with my sample nested multifield component -

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    jcr:primaryType="nt:unstructured"
    jcr:title="Companies"
    sling:resourceType="cq/gui/components/authoring/dialog">
    <content
        jcr:primaryType="nt:unstructured"
        sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns">
        <items jcr:primaryType="nt:unstructured">
            <column
                jcr:primaryType="nt:unstructured"
                sling:resourceType="granite/ui/components/coral/foundation/container">
                <items jcr:primaryType="nt:unstructured">
                    <companies
                        jcr:primaryType="nt:unstructured"
                        sling:resourceType="granite/ui/components/coral/foundation/form/multifield"
                        composite="{Boolean}true"
                        fieldDescription="Click 'Add Field' to add a new company.">
                        <field
                            jcr:primaryType="nt:unstructured"
                            sling:resourceType="granite/ui/components/coral/foundation/container"
                            name="./companies">
                            <items jcr:primaryType="nt:unstructured">
                                <name
                                    jcr:primaryType="nt:unstructured"
                                    sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
                                    fieldLabel="Comp. Name"
                                    name="name"/>
                                <departments
                                    jcr:primaryType="nt:unstructured"
                                    sling:resourceType="granite/ui/components/coral/foundation/form/multifield"
                                    composite="{Boolean}true"
                                    fieldDescription="Click 'Add Field' to add a new department."
                                    fieldLabel="Departments">
                                    <field
                                        jcr:primaryType="nt:unstructured"
                                        sling:resourceType="granite/ui/components/coral/foundation/container"
                                        name="./departments">
                                        <items jcr:primaryType="nt:unstructured">
                                            <image
                                                jcr:primaryType="nt:unstructured"
                                                sling:resourceType="granite/ui/components/coral/foundation/form/pathfield"
                                                fieldLabel="Image"
                                                name="image"
                                                rootPath="/content/dam/we-retail/en/people"/>
                                        </items>
                                    </field>
                                </departments>
                                <activationDate
                                    granite:rel="schedule-move-datepicker"
                                    jcr:primaryType="nt:unstructured"
                                    sling:resourceType="granite/ui/components/coral/foundation/form/datepicker"
                                    displayedFormat="&#x9;&#xa;YYYY-MM-DD HH:mm"
                                    displayTimezoneMessage="{Boolean}true"
                                    emptyText="Select Date &amp; Time"
                                    minDate="today"
                                    name="scheduleDate"
                                    type="date"/>
                            </items>
                        </field>
                    </companies>
                </items>
            </column>
        </items>
    </content>
</jcr:root>

Sling Model -

package com.aem.demo.core.models;

import java.util.List;
import javax.inject.Inject;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;

@Model(
	    adaptables = {Resource.class},
	    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public interface CompaniesModel {
	@Inject
	  List<Company> getCompanies(); // the name `getCompanies` corresponds to the multifield name="./companies"
	  /**
	   * Company model
	   * has a name and a list of departments
	   */
	  @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
	  interface Company {
	    @inject
	    String getName();
	    @inject
	    List<Department> getDepartments(); // the name `getDepartments` corresponds to the multifield name="./departments"
	  }
	  /**
	   * Department model
	   * has a name and a manager
	   */
	  @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
	  interface Department {
	    @inject
	    String getImage();
	    
	  }

}

 

Authoring entries -

DEBAL_DAS_0-1674998081919.pngDEBAL_DAS_1-1674998092649.png

Content displays on page -

DEBAL_DAS_2-1674998121570.png

Please review and hope these will help.

View solution in original post

4 Replies

Avatar

Level 6

@vanshgg  Can you please share the nested multifield dialog node structure details.

Avatar

Community Advisor

Hi @vanshgg 

Hope you are doing well!

 

I see you have used custom code in Post construct method to populate your multifield here. I think you should first change your approach to get data from multifield which is very straight forward. Here is a very good post:

https://aemhints.com/2020/10/24/coral-nested-multifield-aem65/

If you follow this, you dont have to populate data yourself and would be able to make use of sling models concept to the fullest. Let Sling model populate data for you and it will have less chance of any code error.

 

Also, the way you have implemeted it, does not make sense as, each list item in Multilist should have its own NestedList and you have created single Nested List only.

 

Hope it helps!

Thanks

Nupur

Avatar

Correct answer by
Employee Advisor

Sharing .content.xml file associated with my sample nested multifield component -

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
    jcr:primaryType="nt:unstructured"
    jcr:title="Companies"
    sling:resourceType="cq/gui/components/authoring/dialog">
    <content
        jcr:primaryType="nt:unstructured"
        sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns">
        <items jcr:primaryType="nt:unstructured">
            <column
                jcr:primaryType="nt:unstructured"
                sling:resourceType="granite/ui/components/coral/foundation/container">
                <items jcr:primaryType="nt:unstructured">
                    <companies
                        jcr:primaryType="nt:unstructured"
                        sling:resourceType="granite/ui/components/coral/foundation/form/multifield"
                        composite="{Boolean}true"
                        fieldDescription="Click 'Add Field' to add a new company.">
                        <field
                            jcr:primaryType="nt:unstructured"
                            sling:resourceType="granite/ui/components/coral/foundation/container"
                            name="./companies">
                            <items jcr:primaryType="nt:unstructured">
                                <name
                                    jcr:primaryType="nt:unstructured"
                                    sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
                                    fieldLabel="Comp. Name"
                                    name="name"/>
                                <departments
                                    jcr:primaryType="nt:unstructured"
                                    sling:resourceType="granite/ui/components/coral/foundation/form/multifield"
                                    composite="{Boolean}true"
                                    fieldDescription="Click 'Add Field' to add a new department."
                                    fieldLabel="Departments">
                                    <field
                                        jcr:primaryType="nt:unstructured"
                                        sling:resourceType="granite/ui/components/coral/foundation/container"
                                        name="./departments">
                                        <items jcr:primaryType="nt:unstructured">
                                            <image
                                                jcr:primaryType="nt:unstructured"
                                                sling:resourceType="granite/ui/components/coral/foundation/form/pathfield"
                                                fieldLabel="Image"
                                                name="image"
                                                rootPath="/content/dam/we-retail/en/people"/>
                                        </items>
                                    </field>
                                </departments>
                                <activationDate
                                    granite:rel="schedule-move-datepicker"
                                    jcr:primaryType="nt:unstructured"
                                    sling:resourceType="granite/ui/components/coral/foundation/form/datepicker"
                                    displayedFormat="&#x9;&#xa;YYYY-MM-DD HH:mm"
                                    displayTimezoneMessage="{Boolean}true"
                                    emptyText="Select Date &amp; Time"
                                    minDate="today"
                                    name="scheduleDate"
                                    type="date"/>
                            </items>
                        </field>
                    </companies>
                </items>
            </column>
        </items>
    </content>
</jcr:root>

Sling Model -

package com.aem.demo.core.models;

import java.util.List;
import javax.inject.Inject;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;

@Model(
	    adaptables = {Resource.class},
	    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public interface CompaniesModel {
	@Inject
	  List<Company> getCompanies(); // the name `getCompanies` corresponds to the multifield name="./companies"
	  /**
	   * Company model
	   * has a name and a list of departments
	   */
	  @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
	  interface Company {
	    @inject
	    String getName();
	    @inject
	    List<Department> getDepartments(); // the name `getDepartments` corresponds to the multifield name="./departments"
	  }
	  /**
	   * Department model
	   * has a name and a manager
	   */
	  @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
	  interface Department {
	    @inject
	    String getImage();
	    
	  }

}

 

Authoring entries -

DEBAL_DAS_0-1674998081919.pngDEBAL_DAS_1-1674998092649.png

Content displays on page -

DEBAL_DAS_2-1674998121570.png

Please review and hope these will help.