• Taher Kundawala 19
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
I'm working on Step 9 at the moment and my code passed the Step 7 check, but I noticed something very odd.  Here is the problem:

1. I can add an intial review and the code does as requested and shows the review 
Add Review  Show Boat

Behind the scenes a record is added to the BoatReview__C object
User-added image

2. The problem occurs on adding a second review.  No matter if I change boats or add a review to the existing boat, all subsequent inserts actually update the original review.
Add second review 

Behind the scenes the Insert has actually done an Update on the original record
User-added image

If I change boats and add a review record, the original boat review is updated and the Id and Boat Id stay the same.  It is as though I am not Committing the transaction.

Here is the code:
AddBoatReview.cmp
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes">
	<aura:attribute name="boat" type="Boat__c" access="public"/>
    <aura:attribute name="boatReview" type="BoatReview__c" access="private"
                    default="{'sobjectType':'BoatReview__c', 'Name':'', 'Comment__c':''}"/>    
	<aura:attribute name="boatReviewRecord" type="Object" access="public"/>
    <aura:attribute name="recordError" type="String" access="private"/>  

    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
	<aura:registerEvent name="BoatReviewAdded" type="c:BoatReviewAdded"/>    
    
	<force:recordData aura:id="service"
    	layoutType="FULL"  
		fields="Id, Name, Comment__c, Boat__c"
		targetRecord="{!v.boatReviewRecord}"
		targetFields="{!v.boatReview}"
		targetError="{!v.recordError}"
        recordUpdated="{!c.onRecordUpdated}" />    
    
    <lightning:layout multipleRows="true">
		<lightning:layoutItem size="12" padding="around-small">
			<lightning:input name="title" label="Title" value="{!v.boatReview.Name}"/>
		</lightning:layoutItem>
		<lightning:layoutItem size="12" padding="around-small">
			<label class="slds-form-element__label" for="input-id-01">Comment</label>
			<lightning:inputRichText value="{!v.boatReview.Comment__c}" disabledCategories="FORMAT_FONT"/>
		</lightning:layoutItem>
        <lightning:layoutItem size="12" class="slds-align--absolute-center">
			<lightning:button iconName="utility:save" label="Submit" onclick="{!c.onSave}"/>
		</lightning:layoutItem>
	</lightning:layout> 
    
</aura:component>

AddBoatReviewController.js
({  
    doInit : function(component, event, helper) {       
		helper.onInit(component, event);
    },
   
    onSave : function(component, event, helper) {
//		component.set("v.boatReview.Boat__c",component.get("v.boat.Id"));
			
        component.find("service").saveRecord(function(saveResult) {
           console.log("Add Review status" + saveResult.state)
        		if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") { 
        			var resultsToast = $A.get("e.force:showToast");
                    if(resultsToast != undefined) {    
                        resultsToast.setParams({
                        "title": "Saved",
                        "message": "Boat Review Created"
                        });
						resultsToast.fire(); 
                    } else {
                        alert('Boat Review Created');
                    }
				} else if (saveResult.state === "INCOMPLETE") {
                	console.log("User is offline, device doesn't support drafts.");
        		} else if (saveResult.state === "ERROR") {
            		console.log('Problem saving contact, error: ' +JSON.stringify(saveResult.error));
            	} else {
            		console.log('Unknown problem, state: ' + saveResult.state + ',error: ' + JSON.stringify(saveResult.error));
            	}                
			}
        ); 
		var BoatReviewAdded=component.getEvent("BoatReviewAdded");
        BoatReviewAdded.setParams ({"BoatId" : "ADDED"});
		BoatReviewAdded.fire();
		
        helper.onInit(component,event);   
	},        
    onRecordUpdated : function(component, event, helper) {
        var eventParams = event.getParams();
        if(eventParams.changeType === "CHANGED") {
            var changedFields = eventParams.changedFields;
            var saveResultsToast = $A.get("e.force:showToast");
                if(saveResultsToast!='undefined')
                {
                    saveResultsToast.setParams({
                        "title": "Saved",
                        "message": "Boat Review Saved"
                    });
                    saveResultsToast.fire(); 
                }
                else
                {
                    alert('Boat Review Saved');
                }
        }
    }
})

AddBoatReviewHelper.js
({
	onInit : function(component,event) {
        var boat=component.get("v.boat"); 
//        console.log("We have got the AddReview INIT OK" + boat.Id);  
        component.find("service").getNewRecord("BoatReview__c", // sObject type (entityApiName)
            	null, // recordTypeId
            	false, // skip cache?
            $A.getCallback(function() {
           		var rec = component.get("v.boatReview");
                   var error = component.get("v.recordError");                 
				if(error || (rec === null)) {
//            		console.log("Error initializing record template: " + error);
         		} else {
//                    console.log("We have initialised the AddReview OK" + boat.Id);                 
					component.set("v.boatReview.Boat__c",boat.Id);
         		}
			})
		);   
    }
})

I can complete the SuperBadge becuase the code passed the checker, but be good to know what I got wrong......thanks