• Vinay Ramu 16
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hi All,

I was working on aura component specialist challenge however, faced problem in setting component attribute value in helper JS. Need help on how I can set Boat__c[] type attribute in BoatSearchResults component.
BoatSearchResults.cmp
<aura:component controller="BoatSearchResults" >
    <aura:attribute name="boats" type="Boat__c[]"/>
    <aura:attribute name="boatTypeId" type="Id" />
    <aura:attribute name="selectedBoatId" type="String" />
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:handler name="boatselect" event="c:BoatSelect" action="{!c.onBoatSelect}" />
    <aura:handler name="boatselected" event="c:BoatSelected" action="{!c.onBoatSelected}" />
    
    <aura:method name="search" action="{!c.doSearch}" description="Takes boatTypeId as input parameter"> 
        <aura:attribute name="boatTypeId" type="Id" /> 
    </aura:method>
    <lightning:layout horizontalAlign="space" multipleRows="true">
        <lightning:layoutItem size="9" padding="around-small">
            <aura:iteration items="{!boats}" var="boat">
                <c:BoatTile boat="{!boat}" selected="{!boat.Id == v.selectedBoatId ? true : false }"/>
            </aura:iteration>
        </lightning:layoutItem>
    </lightning:layout>
</aura:component>

BoatSearchResultsController.js
({
    doInit : function(component, event, helper) {
        
    },
    doSearch : function(component, event, helper) {
        //debugger;
        var boatTypeId = component.get("v.boatTypeId");
        console.log('In boat search results 2 : ',boatTypeId);
        helper.onSearch(component, boatTypeId);
        console.log("Inside boat search results controller");
    },
    onBoatSelect : function(component, event, helper) {
        var boatId = event.getParam("boatId");
        console.log("onBoatSelect in BoatSearchResults.", boatId);
        component.set("v.selectedBoatId", boatId);
    }
})

BoatSearchResultsHelper.js
({
    onSearch : function(component, boatTypeId){
        var action = component.get( "c.getBoats" );
        var returnBoats;
        console.log("In helper of boat search results and value is ",boatTypeId);
        action.setParams({
            boatTypeId : boatTypeId
        });
        action.setCallback(this, function( response ){       
            //console.log(response.getReturnValue());
            returnBoats = response.getReturnValue()
            component.set("v.boats",returnBoats);
            //debugger;
            this.returnBoats = returnBoats;
            console.log("length is ",returnBoats.length);
        });
        $A.enqueueAction(action);
    }
})

returnBoats has value however, component.set("v.boats",returnBoats) statement is not setting any value to boats attribute as a result the aura component iteration is not executing tiles component.

Thanks,
Vinay
Hi, I am facing error - Ensure that you implement the Queueable interface in the AnnouncementQueueable class. 

I am getting proper response....and implemented trigger, Product2Helper and AnnouncementQueable class as per the trailhead 



Follwoing is my code -
product2Trigger --->
trigger product2Trigger on Product2 (after update) {
   Product2Helper.AfterUpdate((List<Product2>)trigger.new,(List<Product2>)trigger.old);
   
}

Product2Helper Class ---->
public class Product2Helper {

    /**
     * @name COLLABORATION_GROUP
     * @description List of CollaborationGroup used in both business and test logic
    **/
    static List<CollaborationGroup> COLLABORATION_GROUP = [
        SELECT Id
        FROM CollaborationGroup
        WHERE Name = :Constants.INVENTORY_ANNOUNCEMENTS 
        OR Name = :('TEST'+Constants.INVENTORY_ANNOUNCEMENTS)
        LIMIT 1
    ];

    /**
     * @name afterUpdate
     * @description called by product2 Trigger on After Update
     * @param List<Product2> newList
     * @param List<Product2> oldList
    **/
    public static void AfterUpdate(List<Product2> prodList, List<Product2> prodOldList){
        //ToDo: Declare a List of Product2 records named needsAnnouncement

        //ToDo: Declare a Map of Strings to Inventory_Setting__mdt records

        //ToDo: Loop through a query of Inventory_Setting__mdt records and populate the Map with Name as the key

        //ToDo: Loop through the Products in newList
        // Use the corresponding Inventory Setting record to determine the correct Low Quantity Alert
        // If the Product's Quantity Remaining has been changed to less than the Low Quantity Alert
        //      add it to the needsAnnouncement list

        //ToDo: Pass records to the postAlerts method
        List<Product2> needsAnnouncement = new List<Product2>();
        Map<String,Inventory_Setting__mdt> mapInventory = new Map<String,Inventory_Setting__mdt>();
        for(Inventory_Setting__mdt inv : [select id, DeveloperName, Low_Quantity_Alert__c from Inventory_Setting__mdt]){
            mapInventory.put(inv.DeveloperName,inv);
        }
        
        for(integer i=0;i<prodList.size();i++)
        {
            if(mapInventory.get(prodList[i].Family).Low_Quantity_Alert__c > prodList[i].Quantity_Remaining__c && 
                mapInventory.get(prodList[i].Family).Low_Quantity_Alert__c <= prodOldList[i].Quantity_Remaining__c)
            {
                needsAnnouncement.add(prodList[i]);
            }
        }
        PostAlerts(needsAnnouncement);
    }

    /**
     * @name postAlerts
     * @description called by product2 Trigger on After Update
     * @param List<Product2> productList
    **/
    public static void PostAlerts(List<Product2> productList){
        List<ConnectApi.AnnouncementInput> toPost = new List<ConnectApi.AnnouncementInput>();
        for ( Product2 p : productList ){
            // ToDo: Construct a new AnnouncementInput for the Chatter Group so that it:
            // expires in a day
            // does not notify users via email.
            // and has a text body that includes the name of the product followed by the INVENTORY_LEVEL_LOW constant
            ConnectApi.MessageBodyInput msgBody = new ConnectApi.MessageBodyInput();
            ConnectApi.AnnouncementInput tempPost = new ConnectApi.AnnouncementInput();
            ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
            textSegment.text = p.Name +'  '+ Constants.INVENTORY_LEVEL_LOW;
            msgBody.messageSegments = new List<ConnectApi.MessageSegmentInput>();
            msgBody.messageSegments.add(textSegment);
            
            tempPost.body = msgBody;
            tempPost.expirationDate = DateTime.Now().AddDays(1);
            tempPost.parentId = COLLABORATION_GROUP[0].id;
            tempPost.sendEmails = false;
            toPost.add(tempPost);
         }
    
        // ToDo: Create and enqueue an instance of the announcementQueuable class with the list of Products
        AnnouncementQueueable annQue = new AnnouncementQueueable(toPost);
        //annQue.toPost = toPost;
        system.enqueueJob(annQue);
    }
}

AnnouncementQueueable Class ---->
/**
 * @name AnnouncementQueueable
 * @description This class posts Chatter Announcements
**/
public class AnnouncementQueueable implements Queueable{

    public List<ConnectApi.AnnouncementInput> toPost;
    
    public AnnouncementQueueable(List<ConnectApi.AnnouncementInput> annList)
    {
        toPost = annList;
    }

    //ToDo: Modify this class to implement the Queueable interface and call the postAnnouncements method
    public void execute(System.QueueableContext context){
        
        PostAnnouncements(toPost);
    }

    /**
     * @name postAnnouncements
     * @description This method is provided for you to facilitate the Super Badge
    **/
    public static void PostAnnouncements(List<ConnectApi.AnnouncementInput> announcements){
        while ( announcements.size() > 0 ){
            if ( Limits.getDMLStatements() < Limits.getLimitDMLStatements() && !test.isRunningTest() ){
                ConnectApi.AnnouncementInput a = announcements.remove(0);
                ConnectApi.Announcements.postAnnouncement('Internal', a);
            } else {
                break;
            }
        }
        if ( announcements.size() > 0 && !test.isRunningTest() ){
            AnnouncementQueueable q = new AnnouncementQueueable(announcements);
            //q.toPost = announcements;
            system.enqueueJob(q);
            //ToDo: Enqueue the above instance of announcementQueueable
        }
    }

}

Any help would be really appreciated.