0

I have a method (not a wire, because its not possible), that are supposed to retrieve new data every time I click a button. But it only give me the same data that I got when i clicked the button the first time.

But when I refresh the whole page and click the button, then I get the new data that I want when I click the button. But when I click again, I get the same data as I got after I clicked the button the first time after refresh.

I have removed (cacheable=true), so I have no idea how to do this. Does anyone have any idea? Even thought this.serviceAppointment.Id value changes, I still get the same data as the first time method is called.

Problem: It makes a new thread every time I click the button because it wont get the new thread Id after the first time

Apex:

public static String getThreadFreelanceId(String serviceAppointmentId) {
    String Id='';
    ServiceAppointment serviceAppointment=[SELECT HOT_WorkOrderLineItem__r.WorkOrderId FROM ServiceAppointment WHERE Id=:serviceAppointmentId];
    try{
        Thread__c thread = [SELECT Id FROM Thread__c WHERE CRM_Related_Object__c=:serviceAppointment.HOT_WorkOrderLineItem__r.WorkOrderId LIMIT 1];
        if (thread.Id != null) {
            Id=thread.Id;
            return Id;
        }

        return Id;
    }catch(Exception E){

    }
    return Id;
}

Method that is called when button click:

goToThread() {
    getThreadFreelanceId({ serviceAppointmentId: this.serviceAppointment.Id }).then((result) => {
        if (result != '') {
            this.freelanceThreadId = result;
        } else {
            createThread({ recordId: this.serviceAppointment.Id, accountId: this.serviceAppointment.accountId })
            .then((result) => {
                this.freelanceThreadId = result;
            })
            .catch((error) => {
            });
        }
    });
}
2
  • can you share the code? Commented Feb 13, 2023 at 8:00
  • @AntonKutishevsky code added :)
    – Rasmus
    Commented Feb 13, 2023 at 8:06

1 Answer 1

0

I think this may be related to nested promise chaining and the freelanceThreadId variable assignment. Try using async await. Here is a working example using accounts and opportunities. If the account already has an opportunity, the user is redirected to the opportunity page on click. If not, an opportunity is first created and the user is redirected to the new opportunity after successfull creation.

Apex controller:

public without sharing class  OpportunityController {
  @AuraEnabled
  public static Id fetchOpportunityByAccountId(string id){
    return [Select Id FROM Opportunity WHERE AccountId = :id LIMIT 1]?.Id;
  }

  @AuraEnabled
  public static Id createOpportunityForAccount(string accountId){
    Opportunity opp = new Opportunity(
      name = 'Test Opp',
      accountId = accountId,
      StageName = 'Closed Won',
      CloseDate = Date.today()
    );
    insert opp;
    return opp.Id;
  }
}

LWC

HTML:

<template>
  <lightning-button label="Test Button" onclick={handleClick}></lightning-button>
   <span>OpportunityId: {opportunityId}</span> 
</template>

JS:

import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import fetchOpportunity from '@salesforce/apex/OpportunityController.fetchOpportunityByAccountId';
import createOpportunity from '@salesforce/apex/OpportunityController.createOpportunityForAccount';

export default class CreateOrFetchButton extends NavigationMixin(LightningElement) {
  @api recordId;
  opportunityId = ""

  async handleClick(){
    try {
      const result = await fetchOpportunity({ id: this.recordId })
      if(result){
        this.opportunityId = result;
        this.navigateToOpportunity();
      } 

      const newOpp = await createOpportunity({ accountId: this.recordId })
      if(newOpp){
        this.opportunityId = newOpp;
        this.navigateToOpportunity();
      }
      
    } catch (e) {
      console.error("error", e);
    }
  }

  navigateToOpportunity(){
    this[NavigationMixin.Navigate]({
      type: 'standard__recordPage',
      attributes: {
        recordId: this.opportunityId,
        actionName: 'view',
      },
      
    });
  }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .