function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mitsvikmitsvik 

updaterecord on opportunity and send email imperatively using LWC

I am extremely new to LWC. InFACT just new to apex as well..but my first assignment i have to create a small component which takes a text value and is appended to opportunity record existing field and then sends an email to owners managers...i have the basic HTML, JAVASCRIPT AND APEX written,

However i was told not use wire and instead call the apex imperatively, So On click of submit button the apex methos will udpate the opportunity record and send an email and I have to reload the existing opporecord page with updated values.
Can someone help me correct my javascript handleclick funcation?
javascript
import { LightningElement, track, api } from 'lwc';
import updateCommentsNextStepsOnOpportunity from "@salesforce/apex/opportunityCommentController.updateCommentsNextStepsOnOpportunity";


export default class OpportunityCommentUpdate extends LightningElement {
  
    @api recordId;
    @track newComment = '';

    
	 trackComment(event){
          this.newComment = event.detail.value;
      }


	  handleClick() {
     
      updateCommentsNextStepsOnOpportunity(recordId, newComment)
        .then(result => {
          
          window.location = "opportunity/" + this.recordId;
        })
        .catch(error => {
          this.error = error;
        });
    }
  
     

	  handleCancel(event){
	 
    }
	}


APEX:
public without sharing class opportunityCommentController {
    

	
	 @AuraEnabled
	 public static void updateCommentsNextStepsOnOpportunity(Id recordId, String newComment) {
        
        
        
        Opportunity selectedOpp = [SELECT Id, Comments_Next_Steps__c, Description, OwnerId from opportunity where id =: recordId ];
        
        selectedOpp.Comments_Next_Steps__c=System.today().format()+' '+ newComment;
        string temp = selectedOpp.Description;
        
        if(temp==null && !string.isNotBlank(temp)){
            selectedOpp.Description = selectedOpp.Comments_Next_Steps__c;  
            
        }else if(temp!=null && string.isNotBlank(temp)){
            selectedOpp.Description =  selectedOpp.Comments_Next_Steps__c +'\n' +temp;
        }
        
        
        update selectedOpp;
		sendEmailToRVPandRD(selectedOpp);
    }
    
	
	
    public static void sendEmailToRVPandRD(Opportunity selectedOpp) {
        
        List<String> emailList = new List<String>();
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        
        User getRVPRDEmail = [SELECT Id, Reports_to_RVP__r.Email, Reports_to_RD__r.Email, Username FROM User where id =:selectedOpp.OwnerId];
        
        emailList.add(getRVPRDEmail.Reports_to_RVP__r.Email);
        emailList.add(getRVPRDEmail.Reports_to_RD__r.Email);
        Messaging.SingleEmailMessage sendEmailmsg = new Messaging.SingleEmailMessage();
        sendEmailmsg.toaddresses= emailList;
        string body='';
        body += '<html></br> </br>' + 'A comment has been added to the opportunity record , '
            + '<a href="'+URL.getSalesforceBaseUrl().toExternalForm()
            +'/'+ selectedOpp.id +'">' + '</a>';
        body +=  '</body></html>';
        
        sendEmailmsg.setHtmlBody(body);
        string Subject = 'A comment has been added to the opportunity record '+''+ selectedOpp.Name;
        sendEmailmsg.setSubject(Subject);
        
        mails.add(sendEmailmsg);
        
  
    Messaging.sendEmail(mails);
    
}
}


html
template>
    <lightning-card title="Add Comments">
        <div class="slds-m-around_medium">
          

                <lightning-textarea name="input1" label="Enter new comment" onchange={trackComment}></lightning-textarea>
                <lightning-button variant="neutral"
                                  label="Cancel"
                                  title="Cancel"
                                  type="text"
                                  onclick={handleCancel}
                                  class="slds-m-right_small"></lightning-button>

                <lightning-button variant="brand"
                                  label="Submit"
                                  title="Submit"
                                  onclick={handleClick}
                                  type="submit"></lightning-button>
           
        </div>
    </lightning-card>
</template>

 
Maharajan CMaharajan C
Hi,

Your javascript needs the some changes: 

Use the below code in your Javascript then it will work:

/* eslint-disable no-console */
import { LightningElement, track, api } from 'lwc';
import updateCommentsNextStepsOnOpportunity from "@salesforce/apex/opportunityCommentController.updateCommentsNextStepsOnOpportunity";
export default class UpdateOppComment_Forum extends LightningElement {
    @api recordId;
    @track newComment = '';
    
    trackComment(event){
          this.newComment = event.detail.value;
          console.log(' newComment ==> ' + this.newComment );
    }
    handleClick() {
        console.log( '== handleClick ==' );
        updateCommentsNextStepsOnOpportunity({
            recordId : this.recordId,
            newComment : this.newComment
        })

          .then(result => {
            console.log(' result==> ' + result);
            //window.location = "Opportunity/" + this.recordId + '/view';
            window.location.reload()
          })
          .catch(error => {
            this.error = error;
            console.log(' error ==> ' + this.error);
          });
    }
    handleCancel(event){
        console.log('handleCancel ==> ' + event);
    }   
}




Apex Class: 

In the below soql line your are missing the Opportunity Name to Query . Class will throw error if Name is not in included in SOQL because you referred the Name in Email Subject.


 Opportunity selectedOpp = [SELECT Id, Name, Comments_Next_Steps__c, Description, OwnerId from opportunity where id =: recordId ];



Thanks,
Maharajan.C