1

I have created a lightning datatable and it displays all the contact roles of a grant in the grant contract object. enter image description here

Here I get the details of all the contacts. Now if the contact is primary then I have to get the primary contact placed against the name accordingly if the checkbox is checked. Lookup relation is given to grant from grant contract and grant contract role. Code:

import { LightningElement,api,track,wire} from 'lwc';

import getDataTable from '@salesforce/apex/ComponentTable.getDataTable';

export default class ComponentData extends LightningElement {

 

@api recordId;

@track columns = [ 
    {

    label: 'Role',

    fieldName: 'Role',

    type: 'String',

    sortable: true

},
    {

   label: 'Name'+'/'+'Title',

    fieldName: 'Name',

    type: 'text',
    initialWidth: 180,
    wrapText: true


   // sortable: true

},

/*{

    label: 'Title',

    fieldName: 'Title',

    type: 'text',
    wrapText: true
   // sortable: true

},*/
/*{

    label: 'Primary Contact',
 
     fieldName: 'IsPrimary',
 
     type: 'checkbox',
     wrapText: true,
     initialWidth: 150
 
     //sortable: true
 
 },*/
 {

    label: 'Primary Contact',
 
     fieldName: 'primaryContact',
 
     type: 'text',
     wrapText: true,
     initialWidth: 150
 
     //sortable: true
 
 },
{

    label: 'Email',
 
     fieldName: 'Email',
 
     type: 'text',
     wrapText: true,
     initialWidth: 150
 
     //sortable: true
 
 },
{
    label:'Phone',

    fieldName: 'Phone',

    type: 'Number',
    wrapText: true,
    initialWidth: 80
   // sortable:true
}



];

 

@track error;

@track datatable;




@wire(getDataTable,{recordId: '$recordId'})

wiredAccounts({ error,data }) {

            if (data) {
               
                    var tempOppList = [];  
                    for (var i = 0; i < data.length; i++) {  
                     let tempRecord = Object.assign({}, data[i]); //cloning object  
                    // tempRecord.recordLink = "/" + tempRecord.Product2Id; // Link to product
                     //tempRecord.qliLink = "/" + tempRecord.Id;    // Link to QLI
                     //tempRecord.Contact = tempRecord.Contact.Name;  // Lookup name from product
                     tempRecord.Email = tempRecord.Contact.Email;
                     //tempRecord.Name = tempRecord.Contact.Name; 
                     if(tempRecord.Contact.Title != null) {
                        tempRecord.Name = tempRecord.Contact.Name + ' / ' + tempRecord.Contact.Title;
                     }
                     else{
                        tempRecord.Name = tempRecord.Contact.Name;
                        
                     }
                     tempRecord.Phone=tempRecord.Contact.Phone; 
                     if(tempRecord.isPrimary=='true'){
                        tempRecord.PrimaryContact=tempRecord.PrimaryContact
                     }
                     // Lookup name from product
                     tempOppList.push(tempRecord);  
                    } 
                    data = tempOppList;
                   // console.data('datawbhb@==>'+this.data);
                   this.datatable = data;
                }
                
                

                
         if (error) {

            this.error = error;

        }
    };
                
                

              

}

Apex class:

public with sharing class ComponentTable {
    @AuraEnabled(cacheable=true)
    public static List<OpportunityContactRole> getDataTable(String recordId) {
        System.debug('recordID>>'+recordId);
        //recordId = '0060R00000HxXuEQAV';
Grant_Contract__c  gc = [Select Id,Name,Grant_Name__c from Grant_Contract__c where Id =:recordId limit 1];          
system.debug('opp iD@@==>'+gc.Grant_Name__c); 
return[
SELECT id,IsPrimary,Contact.name,Contact.Email,Role,Contact.Title,
Contact.Phone FROM OpportunityContactRole 
WHERE OpportunityId=: gc.Grant_Name__c 
ORDER BY createddate ASC];
    }
}

1 Answer 1

1

You should simplify your way of writing code making the most of Javascript native functions

Modify this way:

@wire(getDataTable,{recordId: '$recordId'})

wiredAccounts({ error,data }) {

            if (data) {
               
                    /*var tempOppList = [];  
                    for (var i = 0; i < data.length; i++) {  
                     let tempRecord = Object.assign({}, data[i]); //cloning object  
                    // tempRecord.recordLink = "/" + tempRecord.Product2Id; // Link to product
                     //tempRecord.qliLink = "/" + tempRecord.Id;    // Link to QLI
                     //tempRecord.Contact = tempRecord.Contact.Name;  // Lookup name from product
                     tempRecord.Email = tempRecord.Contact.Email;
                     //tempRecord.Name = tempRecord.Contact.Name; 
                     if(tempRecord.Contact.Title != null) {
                        tempRecord.Name = tempRecord.Contact.Name + ' / ' + tempRecord.Contact.Title;
                     }
                     else{
                        tempRecord.Name = tempRecord.Contact.Name;
                        //tempRecord.PrimaryContact=tempRecord.PrimaryContact.Name
                     }
                     tempRecord.Phone=tempRecord.Contact.Phone; // Lookup name from product
                     tempOppList.push(tempRecord);  
                    } 
                    data = tempOppList;*/
                   // console.data('datawbhb@==>'+this.data);
                   this.datatable = data.map(e=>({
                        ...e,
                        Email : e.Contact?.Email,
                        Name:e.Contact?.Title != null?(
                            e.Contact?.Name + ' / ' + e.Contact?.Title
                        ): e.Contact?.Name,
                        Phone:e.Contact?.Phone,
                        primaryContact:e.IsPrimary?e.Contact?.Name:''
                    }));
                }
                
                

                
         if (error) {

            this.error = error;

        }
    };
                
                

              

}

You must log in to answer this question.

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