0

I am kind of scratching my head in order to update two object fields in datatable. The table is displaying field from accountcontactrelation and contact (parent).

public with sharing class PrController {
   

    @AuraEnabled(cacheable=true)
        public static List<AccountContactRelation> getAccountRelationsList(String prId) {
            list<Program__c> listPr =[Select Account_Id__c from Pr__c where Id =:prId limit 1];
         
        
            List<AccountContactRelation> acntContRelation=[SELECT contact.Id,contact.Name,Relationship__c,Contact.Mobilephone,Contact.Email,contact.Birthdate,contact.MailingAddress,contact.SSN__c  from AccountContactRelation where AccountId=: listProgram[0].Account_Id__c] ;
         //  List<AccountContactRelation> acntContRelation=[SELECT EndDate from AccountContactRelation where AccountId=: listPr[0].Account_Id__c] ;
          //  List<Account> acntContRelation=[select name from account where id =: listProgram[0].Account_Id__c];
           return acntContRelation;
        }
    @AuraEnabled
        public static void updateContactList(string contactDraft){
            system.debug('contactDraft' + contactDraft);
           // List <AccountContactRelation> acntcontlist = (List<AccountContactRelation> ) JSON.deserialize(contactDraft, List <AccountContactRelation>.class);
            
           // if(updatecontlist.size()>0)
        // update updatecontlist;

        }

HTML template:

        <template>
    <lightning-card title="Related Contacts" icon-name="custom:custom63">

        <div class="slds-var-m-around_medium">
            <template if:true={contact.data}>
                <lightning-datatable
                    key-field="Id"
                    data={contact.data}
                    columns={columns}
                    onsave={handleSave}
                    draft-values={draftValues}>
                </lightning-datatable>
            </template>
            <template if:true={contact.error}>
                <!-- handle Apex error -->
            </template>
        </div>
    </lightning-card>
</template>

JS controller:


import { LightningElement, wire, track ,api} from 'lwc';
import getRelationList from '@salesforce/apex/PrController.getAccountRelationsList';
import  updateRelatedCont  from '@salesforce/apex/PrController.updateContactList';
import { refreshApex } from '@salesforce/apex';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import ADDRESS_FIELD from '@salesforce/schema/Contact.MailingAddress';
import Mobile_FIELD from '@salesforce/schema/Contact.MobilePhone';
import  HomePhone_FIELD from '@salesforce/schema/Contact.HomePhone';
import Email_FIELD from '@salesforce/schema/Contact.Email';
import ID_FIELD from '@salesforce/schema/Contact.Id';
import RELATIONSHIP_FIELD from '@salesforce/schema/AccountContactRelation.Relationship__c';


const COLS = [
    {label: 'Id', fieldname:'Id',editable:false},
    {label: 'Name', fieldname:'Name',editable:false},
    {label:'BirthDate',fieldName:'BirthDate', editable:false},
    { label: 'Address', fieldName: 'MailingAddress', editable: true },
    { label: 'Relationship', fieldName: 'Relationship__c', editable: true  },
    { label: 'Mobile', fieldName: 'MobilePhone', editable: true},
    { label: 'HomePhone', fieldName: 'HomePhone', editable: true},
    { label: 'Email', fieldName: 'Email', editable: true},   
];

export default class DatatableUpdateExample extends LightningElement {

    @track error;
    @api recordId; 
    @track columns = COLS;
    @track draftValues = [];

    @wire(getRelationList,{programId:'$recordId'})
    contact;

   

    handleSave(event) {
        console.log('draft' + JSON.stringify(event.detail.draftValues));
        updateRelatedCont( {contactDraft: JSON.stringify(event.detail.draftValues)}).then(result => {
            this.dispatchEvent(
              new ShowToastEvent({
                  title: 'Success',
                   message: result + ' price records have been added or updated.',
                   variant: 'success'   
                })
                 
            );  //End of dispatchEvent
  
              this.draftValues = [];
              refreshApex(this.contact);
     
           }).catch(error => {
            // Handle error
          });
    }

}

1.There will be three or more recordrow and 2 or more fields in any rows can be changed. In other words, JSON will be dynamic 2. fields will be updated across two objects

Example JSON passed in updateRelatedCont method as follows

[{"MobilePhone":"1254545","HomePhone":"34234234","Id":"07k1F00000ESWx3QAH"},{"MobilePhone":"234234234","Email":"[email protected]",r"Relationship" :"Friend","Id":"07k1F00000ESWzcQAH"}]

In above example, Relationship,Id is in accountcontactrelation object rest are all on contact object. How would I be updating in apex controller?

1 Answer 1

0

I suppose you could use the generic sobject type to set values dynamically. This might be useful Setting field values generic SObject

You must log in to answer this question.

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