• ApexDev
  • NEWBIE
  • 60 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 28
    Questions
  • 11
    Replies
Hi! :) 

I am not able to find the right answer. I have a lot of Service Templates for Users from different countries. I want to restrict the choice using the screen flow, but I am not able to run 'Create Service Report' from flow to see the Service Report Preview. Anyone know how to trigger this action? 
Hi, 

All users are not able to send Quote document by Email button. There is an Insufficient Privileges Error message.

I checked: Document folder, validation rules and debug logs.

Do you have any idea what can be also checked? 

Admin do not have a problem to send Quote document by email.
Hi Experts! 

I need help to increase my test coverage for SingleEmailMessage. The test coverage is only 33%. Everything after for (Case cs : cases) is not coverage. What I am doing wrong? 

Email Class: 
 
public class EmailClass
{
    @InvocableMethod
    public static void sendEmail() {

        
 //Contact and Case lists
    List<Contact> contacts =[Select Id From Contact where Email!=null];
    
    List<Case> cases = new List<Case>();
    cases = [Select Id,ContactEmail, Letter_Sent__c, Letter_Sent_Date__c, Reminder_Sent__c, Reminder_Sent_Date__c, Follow_up_Date__c, Responsibility_Center__c, 
             Status, ContactId, Follow_Up_Date_in_Days__c, Actual_Follow_up_Date__c From Case 
             Where ContactEmail!=null AND subject='Periodic Service' AND account.company_code__c='US45' AND Actual_Follow_up_Date__c = true AND Status='Ready for Email' AND Letter_Sent__c = false AND Reminder_Sent__c = false AND ContactId IN : contacts];
   
    List<Case> cases2 = new List<Case>();    
    cases2 = [Select Id,ContactEmail, Letter_Sent__c, Letter_Sent_Date__c, Reminder_Sent__c, Reminder_Sent_Date__c, Follow_up_Date__c, Responsibility_Center__c, 
             Status, ContactId, Follow_Up_Date_in_Days__c, Actual_Follow_up_Date__c From Case 
             Where ContactEmail!=null AND Follow_Up_Date_in_Days__c = 0 AND subject='Periodic Service' AND account.company_code__c='US45' AND Actual_Follow_up_Date__c = true AND Status='Waiting for Customer' AND Letter_Sent__c = true AND Reminder_Sent__c = false AND ContactId IN : contacts];
    
        
        
        
 //ORG Wide Email Addresses lists   
        
    OrgWideEmailAddress AST = new OrgWideEmailAddress();
                AST = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress Where DisplayName='Austin']; 
        
    OrgWideEmailAddress ATL = new OrgWideEmailAddress();
                ATL = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress Where DisplayName='Atlanta'];
        

        
        
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for (Case cs : cases)
    {
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    
    List<String> toAddress = new List<String>();
    toAddress.add(cs.ContactEmail);
    email.setToAddresses(toAddress);
    
    email.setWhatId(cs.Id);
    email.setTargetObjectId(cs.ContactId);
    email.setTemplateId('00X7Z0000027tioUAA');
    
    email.setTreatTargetObjectAsRecipient(false);
    //email.setOrgWideEmailAddressId(owea.id);
    //List<String> ccAddress = new List<String>();
    //email.setCcAddresses(ccAddress);
    //email.setSaveAsActivity(true); 
    
    
    //First email reminder
    //Letter_Sent_Date__c and Follow_up_Date__c updated in Cases Processes for first reminder
    
    if (cs.Responsibility_Center__c =='AST' && cs.status=='Ready for Email' && cs.Letter_Sent__c == false && cs.Reminder_Sent__c == false)
    {
        cs.status = 'Waiting for Customer';
        cs.Letter_Sent__c = true;
        email.setOrgWideEmailAddressId(AST.id);
    }
    
    if (cs.Responsibility_Center__c =='ATL' && cs.status=='Ready for Email' && cs.Letter_Sent__c == false && cs.Reminder_Sent__c == false)
    {
        cs.status = 'Waiting for Customer';
        cs.Letter_Sent__c = true;
        email.setOrgWideEmailAddressId(ATL.id);
    } 
        
        else
    {
        system.debug('no mail was sent');
    }
    
    emails.add(email);}
   
    
   //Second email reminder
   
    for (Case cs2 : cases2)
{
    Messaging.SingleEmailMessage email2 = new Messaging.SingleEmailMessage();
    
    List<String> toAddress2 = new List<String>();
    toAddress2.add(cs2.ContactEmail);
    email2.setToAddresses(toAddress2);
    
    email2.setWhatId(cs2.Id);
    email2.setTargetObjectId(cs2.ContactId);
    email2.setTemplateId('00X7Z0000027tioUAA');
    
    email2.setTreatTargetObjectAsRecipient(false);
    cs2.Reminder_Sent__c = true;
   
    if (cs2.Responsibility_Center__c =='AST' && cs2.status=='Waiting for Customer' && cs2.Letter_Sent__c == true && cs2.Follow_Up_Date_in_Days__c == 0)
    {     
        cs2.Reminder_Sent__c = true;
        email2.setOrgWideEmailAddressId(AST.id);
    }
    
    if (cs2.Responsibility_Center__c =='ATL' && cs2.status=='Waiting for Customer' && cs2.Letter_Sent__c == true && cs2.Follow_Up_Date_in_Days__c == 0)
    {
        cs2.Reminder_Sent__c = true;
        email2.setOrgWideEmailAddressId(ATL.id);
    }
    
    else
    {
        system.debug('no mail was sent');
    }
    
    emails.add(email2);
    }
   else
    {
        system.debug('no mail was sent');
    }
    
 
    emails.add(email2);
} 

    Messaging.sendEmail(emails); 
    update cases;
    update cases2;    
  } 
}

Test Class:
 
@isTest(seeAllData=true)
public class EmailClassTest {
   @isTest
    public static void sendEmail(){
            
        //Insert Profile
      Profile cus = [SELECT Id FROM Profile WHERE Name='DGS - Customer Service'];   
        
        
        //Insert User
      User us = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = cus.Id, CompanyName = 'E3',
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com'); 
        
        
        //Insert Account
      Account acct = new Account(Name='TestClassAccount', Customer_Segment__c = 'Hospitals', Calibration_Intervals__c = 'Half year');
      insert acct;
        
        
        //Insert Contact
      Contact con = new Contact(LastName='JasonSendEmail', email='sendemail@toatlanta.com', AccountId=acct.Id);
      insert con;
        
        
        //Insert Responsibility Center
      Responsibility_Center__c rc = new Responsibility_Center__c(Name='e3 AST');
        
        
        //Insert Case
       Case cas = new Case(Status = 'Ready for Email', ContactId=con.id, subject='Periodic Service', AccountId = acct.id,
                          Case_Reason__c = 'Business related', Reason = 'General Enquiry', Responsibility_Center_lookup__c=rc.id, Reminder_Sent__c = false, Letter_Sent__c = false,
                          Follow_up_date__c = datetime.newInstance(2022, 10, 15, 12, 30, 0));
       insert cas; 
        //Insert 2nd Case
       Case cas2 = new Case(Follow_up_Date__c = date.today(), Status = 'Waiting for Customer', ContactId=con.id, subject='Periodic Service', AccountId = acct.id,
                          Case_Reason__c = 'Business related', Reason = 'General Enquiry', Responsibility_Center_lookup__c=rc.id, Reminder_Sent__c = false, Letter_Sent__c = true);
       insert cas2; 
         
        
       //Insert Org-wide
       OrgWideEmailAddress[] addresses = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress Where DisplayName='Austin'];      
            
    
       Test.startTest();
       System.assertEquals(0, Limits.getEmailInvocations(), 'No emails should be sent');

       EmailClass.sendEmail();

       System.assertEquals(1, Limits.getEmailInvocations(), 'Emails should be sent');
      
       Test.stopTest();
        
     
}
}

 
I have a custom lookup field on Work Order Line Item object. The fields has lookup to Product2. It is editable and visible on WorkOrderLineItem layout. 

On my LWC Datatable the field is not visible. The function when I click on the pencil as edit and save are working. Only the column is empty. When I add the standard fields - they are visible and also editable. The only problem is with custom field (ProductXX__c)

Can you help me with that?.
 
import { LightningElement, track, wire, api } from 'lwc';
import fetchWolis from '@salesforce/apex/AccountDataController.fetchWolis';
import WORK_ORDER_LINE_ITEM_OBJECT from '@salesforce/schema/WorkOrderLineItem';
import STATUS_FIELD from '@salesforce/schema/WorkOrderLineItem.Status';
import { updateRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { refreshApex } from '@salesforce/apex';
import { getPicklistValues, getObjectInfo } from 'lightning/uiObjectInfoApi';
 
const columns = [
   
     {
        label: 'Device',
        fieldName: 'AssetId',
        type: 'lookupColumn',
        typeAttributes: {
            object: 'WorkOrderLineItem',
            fieldName: 'AssetId',
            value: { fieldName: 'AssetId' },
            context: { fieldName: 'Id' },
            name: 'Asset',
            fields: ['Asset.Name'],
            target: '_self'
        },
        editable: false,
    }, 
    {
        label: 'Serial Number', fieldName: 'Serial_No__c', editable: false
    },
    {
        label: 'Status', fieldName: 'Status', type: 'picklistColumn', editable: false, typeAttributes: {
            placeholder: 'Choose Status', options: { fieldName: 'pickListOptions' }, 
            value: { fieldName: 'Status' }, // default value for picklist,
            context: { fieldName: 'Id' } // binding account Id with context variable to be returned back
        }
    },
    { label: 'Product', fieldName: 'PricebookEntryId', editable: false },

    {
        label: 'Product',
        fieldName: 'ProductXX__c',
        type: 'lookupProduct',
        typeAttributes: {
            object: 'WorkOrderLineItem',
            fieldName: 'ProductXX__c',
            value: { fieldName: 'ProductXX__c' },
            context: { fieldName: 'Id' },
            name: 'Product2',
            fields: ['Product2.Name'],
            target: '_self'
        },
        editable: false,
    },
    { label: 'Qauantity', fieldName: 'Quantity', editable: true},
    { label: 'List Price', fieldName: 'ListPrice', type: 'currency', editable: false},

    { label: 'Discount %', fieldName: 'Discount', type: 'percent-fixed', editable:true,
            cellAttributes: {
    alignment: 'left'
    },
},
  /*  { label: 'Total Price', fieldName: 'TotalPrice', type: 'currency', editable: false },
    {
        label: 'Line Item Number',
        fieldName: 'nameUrl',
        type: 'url',
        typeAttributes: {label: { fieldName: 'LineItemNumber' }, 
        target: '_blank'},
        sortable: true
    }  */
]
 
export default class CustomDatatableDemo extends LightningElement {
    columns = columns;          //picklist
    showSpinner = false;
    @track data = [];
    @track accountData;
    @track draftValues = [];
    lastSavedData = [];
    @track pickListOptions;
 

    @api recordId;             //related record



    @wire(getObjectInfo, { objectApiName: WORK_ORDER_LINE_ITEM_OBJECT })
    objectInfo;
 
    //fetch picklist options
    @wire(getPicklistValues, {
        recordTypeId: '0120X000000gLE8QAM',
        fieldApiName: STATUS_FIELD
    })
 
    wirePickList({ error, data }) {
        if (data) {
            this.pickListOptions = data.values;
        } else if (error) {
            console.log(error);
        }
    }
 
    //here I pass picklist option so that this wire method call after above method
    @wire(fetchWolis, { woid: '$recordId', pickList: '$pickListOptions' })
    accountData(result) {
        this.accountData = result;
        
        if (result.data) {
            let baseUrl = 'https://'+'demant--andzeladev.lightning.force.com'+'/';
            this.data = JSON.parse(JSON.stringify(result.data));
            console.log(this.data);
 
            this.data.forEach(ele => {
                ele.pickListOptions = this.pickListOptions;
            //    ele.nameUrl = baseUrl+ele.Id;
            //    ele.assetUrl = baseUrl+ele.AssetId;
            //    ele.serialUrl = baseUrl+ele.AssetId;
                ele.accountLink = ele.AssetId != undefined ? '/' + ele.AssetId : '';
                ele.accountName = ele.AssetId != undefined ? ele.Asset.Name : '';
                ele.productLink = ele.ProductXX__c != undefined ? '/' + ele.Product2Id : '';
                ele.productName = ele.ProductXX__c != undefined ? ele.Product2.Name : '';
            })
 
            this.lastSavedData = JSON.parse(JSON.stringify(this.data));
 
        } else if (result.error) {
            this.data = undefined;
        }
    };
 
    updateDataValues(updateItem) {
        let copyData = JSON.parse(JSON.stringify(this.data));
 
        copyData.forEach(item => {
            if (item.Id === updateItem.Id) {
                for (let field in updateItem) {
                    item[field] = updateItem[field];
                }
            }
        });
 
        //write changes back to original data
        this.data = [...copyData];
    }
 
    updateDraftValues(updateItem) {
        let draftValueChanged = false;
        let copyDraftValues = [...this.draftValues];
        //store changed value to do operations
        //on save. This will enable inline editing &
        //show standard cancel & save button
        copyDraftValues.forEach(item => {
            if (item.Id === updateItem.Id) {
                for (let field in updateItem) {
                    item[field] = updateItem[field];
                }
                draftValueChanged = true;
            }
        });
 
        if (draftValueChanged) {
            this.draftValues = [...copyDraftValues];
        } else {
            this.draftValues = [...copyDraftValues, updateItem];
        }
    }
 
    //listener handler to get the context and data
    //updates datatable
    picklistChanged(event) {
        event.stopPropagation();
        let dataRecieved = event.detail.data;
        let updatedItem = { Id: dataRecieved.context, Status: dataRecieved.value };
        console.log(updatedItem);
        this.updateDraftValues(updatedItem);
        this.updateDataValues(updatedItem);
    }

    lookupChanged(event) {
        console.log(event.detail.data);
        event.stopPropagation();
        let dataRecieved = event.detail.data;
        let accountIdVal = dataRecieved.value != undefined ? dataRecieved.value : null;
        let updatedItem = { Id: dataRecieved.context, AssetId: accountIdVal  };
        console.log(updatedItem);
        this.updateDraftValues(updatedItem);
        this.updateDataValues(updatedItem);
        
    }

    lookupproductchanged(event) {
        console.log(event.detail.data);
        event.stopPropagation();
        let dataRecieved = event.detail.data;
        let productIdVal = dataRecieved.value != undefined ? dataRecieved.value : null;
        let updatedItem = { Id: dataRecieved.context, ProductXX__c: productIdVal  };
        console.log(updatedItem);
        this.updateDraftValues(updatedItem);
        this.updateDataValues(updatedItem);
    }
 
    //handler to handle cell changes & update values in draft values
    handleCellChange(event) {
        this.updateDraftValues(event.detail.draftValues[0]);
    }
 
    handleSave(event) {
        this.showSpinner = true;
        this.saveDraftValues = this.draftValues;
 
        const recordInputs = this.saveDraftValues.slice().map(draft => {
            const fields = Object.assign({}, draft);
            return { fields };
        });
 
        // Updateing the records using the UiRecordAPi
        const promises = recordInputs.map(recordInput => updateRecord(recordInput));
        Promise.all(promises).then(res => {
            this.showToast('Success', 'Records Updated Successfully!', 'success', 'dismissable');
            this.draftValues = [];
            return this.refresh();
        }).catch(error => {
            console.log(error);
            this.showToast('Error', 'An Error Occured!!', 'error', 'dismissable');
        }).finally(() => {
            this.draftValues = [];
            this.showSpinner = false;
        });
    }
 
    handleCancel(event) {
        //remove draftValues & revert data changes
        this.data = JSON.parse(JSON.stringify(this.lastSavedData));
        this.draftValues = [];
    }
 
    showToast(title, message, variant, mode) {
        const evt = new ShowToastEvent({
            title: title,
            message: message,
            variant: variant,
            mode: mode
        });
        this.dispatchEvent(evt);
    }
 
    // This function is used to refresh the table once data updated
    async refresh() {
        await refreshApex(this.accountData);
    }
}

 
  • September 24, 2022
  • Like
  • 0
I try to build my own LWC Datatable component on Work Order object - which will show related Work Order Line Items records. The table is empty. How to retrieve the records into my LWC?
Here is my code:
woliDatatableDemo.html
<template>
    <lightning-card title="NEW Work Order Line Item" icon-name="custom:custom63">
            <div style="width: auto;">
                    <lightning-datatable
                        key-field="Id"
                        data={data}
                        columns={columns}
                        ></lightning-datatable>
                     </div>
        </lightning-card>
    </template>

woliDatatableDemo.js
import { LightningElement, api, track, wire } from "lwc";
import getWOLIs from '@salesforce/apex/woliControllerDatatable.getWOLIs';

const columns = [
    {
        label: 'Device', fieldName: 'AssetId'
    },

    { label: 'Serial No.', fieldName: 'Serial_No__c'},

    { label: 'Product', fieldName: 'PricebookEntryId'},

    { label: 'Quantity', fieldName: 'Quantity'},

    { label: 'List Price', fieldName: 'ListPrice'},

    { label: 'Discount', fieldName: 'Discount'},

    { label: 'Total Price', fieldName: 'TotalPrice'},
    
];

export default class WoliDatatableDemo extends LightningElement {

    @api recordId;
    @track data = [];
    @track columns = columns;
    
  
    @wire(getWOLIs, { woid: "$recordId" })
    
    wiredRecordsMethod({ data, error }) {
    if (data) {
       let result = JSON.parse(JSON.stringify(data));
        this.data = result.map(function(item) {
          return item;
        })
      this.error = undefined;
    } else if (error) {
      this.error = error;
      this.data = undefined;
    }
}
}

woliControllerDatatable.cls
public with sharing class woliControllerDatatable {

    @AuraEnabled(cacheable=true)
    public static List<WorkOrderLineItem> getWOLIs(String woId) {
        return [
            SELECT Quantity, PricebookEntryId, Serial_No__c, TotalPrice, WorkOrder.WorkOrderNumber, Work_Order_No__c,
                     Duration, ListPrice, Discount, WorkOrderId, Parent_WOLI__c, AssetId
            FROM WorkOrderLineItem
            WHERE Parent_WOLI__c = false AND WorkOrderId = :woId
            WITH SECURITY_ENFORCED
        ];}
}

 
  • September 14, 2022
  • Like
  • 0
Hi! I am super newbie in LWC, and I need to write Datatable Component with lookup field. I don't know how to achive this. My component looks fine, but there is no clickable lookup. Do you have any example code how to add lookup field in Data Table component?

Thank you!
  • September 14, 2022
  • Like
  • 0
Hi Developers :) Can somebody look at my code, and describe me why the refresh Apex is not working after deleting record? 
 
import { LightningElement, wire, track, api } from 'lwc';
import getWOLIs from '@salesforce/apex/WOLIController.getWOLIs';
import { refreshApex } from '@salesforce/apex';
import { updateRecord } from 'lightning/uiRecordApi';
import delSelectedWoli from '@salesforce/apex/WOLIController.deleteWolis';

import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import LineItemNumber_FIELD from '@salesforce/schema/WorkOrderLineItem.LineItemNumber';
import Work_to_do__c_FIELD from '@salesforce/schema/WorkOrderLineItem.Work_to_do__c';
import Status_FIELD from '@salesforce/schema/WorkOrderLineItem.Status';
import ListPrice_FIELD from '@salesforce/schema/WorkOrderLineItem.ListPrice';
import AssetId_FIELD from '@salesforce/schema/WorkOrderLineItem.AssetId';
import Discount_FIELD from '@salesforce/schema/WorkOrderLineItem.Discount';
import Duration_FIELD from '@salesforce/schema/WorkOrderLineItem.Duration';
import ID_FIELD from '@salesforce/schema/WorkOrderLineItem.Id';

const actions = [
     
     
    { label: 'Delete', name: 'delete'}
];

const COLS = [
    {
        label: 'Line Item Number',
        fieldName: LineItemNumber_FIELD.fieldApiName,
        editable: false
    },
    {
        label: 'Work to do',
        fieldName: Work_to_do__c_FIELD.fieldApiName,
        editable: true
    },
    {
        label: 'Device',
        fieldName: AssetId_FIELD.fieldApiName,
        editable: true
    },

    { label: 'Status', fieldName: Status_FIELD.fieldApiName, editable: false },
    {
        label: 'List Price',
        fieldName: ListPrice_FIELD.fieldApiName,
        type: 'double',
        editable: true
    },
    {
        label: 'Discount',
        fieldName: Discount_FIELD.fieldApiName,
        type: 'percent',
        editable: true
    },
    {
        label: 'Duration',
        fieldName: Duration_FIELD.fieldApiName,
        type: 'double',
        editable: true
    },
    {
        type: 'action',
        typeAttributes: {
            rowActions: actions,
            menuAlignment: 'right'
        }}
];
export default class WoliNewLWC extends LightningElement {
    @api recordId;
    columns = COLS;
    draftValues = [];


    // non-reactive variables
    selectedRecords = [];
    refreshTable;
    error;

    // retrieving the data using wire service 


    @wire(getWOLIs, { woId: '$recordId' })
    wolis;
    

    handleRowActions(event) {
        let actionName = event.detail.action.name;

        window.console.log('actionName ====> ' + actionName);

        let row = event.detail.row;

        window.console.log('row ====> ' + row);
        // eslint-disable-next-line default-case
        switch (actionName) {
            case 'delete':
                this.deleteWolis(row);
                //break;
        }
    }

    async handleSave(event) {
        // Convert datatable draft values into record objects
        const records = event.detail.draftValues.slice().map((draftValue) => {
            const fields = Object.assign({}, draftValue);
            return { fields };
        });

        // Clear all datatable draft values
        this.draftValues = [];

        try {
            // Update all records in parallel thanks to the UI API
            const recordUpdatePromises = records.map((record) =>
                updateRecord(record)
            );
            await Promise.all(recordUpdatePromises);

            // Report success with a toast
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Success',
                    message: 'Work Order Line Item updated',
                    variant: 'success'
                })
            );

            // Display fresh data in the datatable
            await refreshApex(this.wolis);
        } catch (error) {
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error updating or reloading work order line items',
                    message: error.body.message,
                    variant: 'error'
                })
            );
        }
    }

    deleteWolis(currentRow) {
        let currentRecord = [];
        currentRecord.push(currentRow.Id);
        this.showLoadingSpinner = true;

        // calling apex class method to delete the selected contact
        delSelectedWoli({lstWolIds: currentRecord})
        .then(result => {
            window.console.log('result ====> ' + result);
            this.showLoadingSpinner = false;

            // showing success message
            this.dispatchEvent(new ShowToastEvent({
                title: 'Success!',
                message: currentRow.LineItemNumber +' Work Order Line Item deleted.',
                variant: 'success'
            }),);

            // refreshing table data using refresh apex
             return refreshApex(this.refreshTable);

        })
        .catch(error => {
            window.console.log('Error ====> '+error);
            this.dispatchEvent(new ShowToastEvent({
                title: 'Error!!', 
                message: error.message, 
                variant: 'error'
            }),);
        });
    }
    handleSuccess() {
        return refreshApex(this.refreshTable);
    }
}

 
  • September 13, 2022
  • Like
  • 0
Hi Developers Is it possible to populate lookup field on Case with ID of custom object using Apex Class?

Criteria if: Account.RC__c (picklist) == Responsibility_Center__c.Name (custom object)
Set the Responsibility_Center__c ID on Case

I tried to find some examples to modify the code, but unfortunatelly I don't think that I am doing it right. And I am newbie in Apex, so it is hard to write without any examples :(
public with sharing class CaseTriggerService {
    public static void findRC(List<Responsibility_Center__c> records)
    {
        Set<String> RcIds = new Set<String>();
        for (Responsibility_Center__c record : records) RcIds.add(record.Name);

        Map<String, Case> cases = new Map<String, Case>();
        for (Case cs : [
            SELECT Id, Responsibility_Center__c, Account.RC__c FROM Case
            WHERE Responsibility_Center__c IN : RcIds
        ]) cases.put(cs.Responsibility_Center__c, cs);

        for (Responsibility_Center__c record : records)
            if (cases.containsKey(record.Responsibility_Center__c))
                record.Name = cases.get(record.Responsibility_Center__c).Id;
    }
}
  • September 07, 2022
  • Like
  • 0
Hello! :) 

I need to create report of active records related to Account. I need extensions to show the SOQL query, because in other way I am able to see all records active and inactive. Do you have any code example that I can use, and how to show it with standard controller? :( 
  • September 07, 2022
  • Like
  • 0
Hello everyone! :) 

It is possible to add Cost Price and List Price (fields from PricebookEntry object) on Product Selection filters?

I checked Field Set on Product, and there is no option to choose Cost Price and List Price - because of one Product record can have multiple Pricebooks.

I also checked Search Filter and there are only default fields as Product Code, Product Name, Product Family, and Product Description. Can we add custom field to Product Search and how to make it? :) 

Thank you for your advise! :) 

User-added image
Hi! :) 

Everytime when I try to edit/change PricebookEntry field on Work Order Line Item by adding different product I get the error message.

I checked and the field is standard, with lookup to Price Book Entry. The editing is enable, and field accesible is also set as editing for each profile. 

Do you have any ideas?:) 

User-added image
Hi Guys! 

I am newbie in Apex, but I am trying to write some code. The purpose was simple: I have PricebookEntry object on Salesforce, and 2 lookup fields: 
- Pricebook2Id
- Price_Book_2__c
I want to fill Price_Book_2__c with the same value as Pricebook2Id on each record.

When I opened and run batch in Anonymouse Window I get error: 
Line: 3, Column: 25
Variable does not exist: Pricebook2Id

Here is the Apex class: 

global class UpdatePriceBookCustomField implements Database.batchable<sObject>{
    global final String query;
    global Database.QueryLocator start (Database.BatchableContext BC) {
        return Database.getQueryLocator(query);}
    
    global void execute(Database.BatchableContext BC,List<sObject> scope){
        List<PricebookEntry>pbeToUpdate = new List<PricebookEntry>();
        
    for (sObject s : scope){PricebookEntry a = (PricebookEntry)s;
                            if(a.Price_Book_2__c==null){
                                a.Price_Book_2__c=a.Pricebook2Id;
                                pbeToUpdate.add(a);
                            }
                            }
    update pbeToUpdate;
        
    }
    global void finish(Database.BatchableContext BC) {
    }
}


Here is the batch: 
UpdatePriceBookCustomField PBE = new UpdatePriceBookCustomField();
PBE.query='Select Id, Pricebook2Id, Price_Book_2__c from PricebookEntry';
PBE.Price_Book_2__c=PBE.Pricebook2Id;
ID batchprocessid = Database.executeBatch(PBE);

Anyone has any idea why it don't want to run? I don't know why Apex is not seeing the Pricebook2Id...
Hi Everyone! :)   On PricebookEntry object, I have a 2 lookup fields: 
 - Pricebook2Id (standard field filled by Integration)
 - Price_Book_2__c (custom field needed for Related list) 
 
I want to fill the Price_Book_2__c using the value in Pricebook2Id.  I am trying to write the class, it will be my first apex class, I invoke it using Flow and later Process Builder, but the field is still empty 
 


public class PriceBookField
{
    @InvocableMethod
    public static void PriceBookField(List<Id> PBIds){
        List<PricebookEntry> XYZ = [Select Id, Pricebook2Id, Price_Book_2__c from PricebookEntry where ID in: PBIds];
        for(PricebookEntry PB : XYZ){
            PB.Price_Book_2__c = PB.Pricebook2Id;
        }
        update XYZ;
    } 
}
Hi Everyone! :) 

On PricebookEntry object, I have a 2 lookup fields: 
- Pricebook2Id (standard field filled by Integration)
- Price_Book_2__c (custom field needed for Related list)
I want to fill the Price_Book_2__c using the value in Pricebook2Id.

I am trying to write some class, it will be my first apex class, and still I get errors...

I want to run it from Flow.

Here is code:
public class PriceBookField{
@InvocableVariable(label='Get Price Book records')
    public PriceBookEntry p;
    @InvocableMethod
    public static void populateField(List<PricebookEntry>records){
        records[0].Price_Book_2__c = records[0].Pricebook2Id;
    update records;
}
}


Can you help me?
Hi Everyone! :) 

Email-to-case doesn't work with Lightning Email Templates. I am using Flow and Email Alerts to send the emails to customers. Under Email tab on Case I can see only emails with Classic Template. The same situation with response from customer's email box. I can see only responses for Classic Templates, but not for Lightning.

Do you have any idea? :) 
Hi Experts!

We have 3 standard object in Salesforce: Account, Pricebook2 and PricebookEntry. 

Account has lookup relation to Pricebook2, and Pricebook2 has relation to PricebookEntry.

Is any possibility to create list on Account object, which will show the PricebookEntry records from right Pricebook2?

User-added image
Hi Experts! :) 

On Account object and Pricebook2 object I created 2 text fields - Customer_Price_Group__c. When both values in a Pricebook2 object record match the values of field in Account object record, respectively I would like fill the Price_Book__c field on Account record to populate with the name of Pricebook2 record.

Do you have any example of Apex trigger, that I can exercise and modify for my purpose?

Thank you! :) 
Hi everyone! :) 
 
I have a problem, that Assistant in Sales app stopped working for users with different profile than Administator. Do you know how to set up it correctly? They have permission as Standard Einstein Activity Capture or Sales Cloud Einstein.
 
What they see: 
User-added image

What I see:
User-added image
Hi everyone! :) 

I have a question regarding to automation testing: I want to write some code to make regression tests. 

Did someone try to do this? Which language will be the best? It is good idea to test it with webdriver help? 
Hi everyone! :) 

I have 2 standard object on Salesforce: Case and Asset. On Case object I created 2 custom checkbox fields: 
- In_House_Service__c
- Field_Service__c
Depends of Preferred_Service_Location__c on Asset, the right checkbox should be true on Case:
- if In-house service on Asset - the In_House_Service__c should be true on Case
- if Field Service on Asset - the Field Service__c should be true on Case

I am super newbie in Apex, I don't know how to write the right code. Maybe do you have any examples, that I can build or base on it?

I will be grateful! Thank you!
Hi Experts! 

I need help to increase my test coverage for SingleEmailMessage. The test coverage is only 33%. Everything after for (Case cs : cases) is not coverage. What I am doing wrong? 

Email Class: 
 
public class EmailClass
{
    @InvocableMethod
    public static void sendEmail() {

        
 //Contact and Case lists
    List<Contact> contacts =[Select Id From Contact where Email!=null];
    
    List<Case> cases = new List<Case>();
    cases = [Select Id,ContactEmail, Letter_Sent__c, Letter_Sent_Date__c, Reminder_Sent__c, Reminder_Sent_Date__c, Follow_up_Date__c, Responsibility_Center__c, 
             Status, ContactId, Follow_Up_Date_in_Days__c, Actual_Follow_up_Date__c From Case 
             Where ContactEmail!=null AND subject='Periodic Service' AND account.company_code__c='US45' AND Actual_Follow_up_Date__c = true AND Status='Ready for Email' AND Letter_Sent__c = false AND Reminder_Sent__c = false AND ContactId IN : contacts];
   
    List<Case> cases2 = new List<Case>();    
    cases2 = [Select Id,ContactEmail, Letter_Sent__c, Letter_Sent_Date__c, Reminder_Sent__c, Reminder_Sent_Date__c, Follow_up_Date__c, Responsibility_Center__c, 
             Status, ContactId, Follow_Up_Date_in_Days__c, Actual_Follow_up_Date__c From Case 
             Where ContactEmail!=null AND Follow_Up_Date_in_Days__c = 0 AND subject='Periodic Service' AND account.company_code__c='US45' AND Actual_Follow_up_Date__c = true AND Status='Waiting for Customer' AND Letter_Sent__c = true AND Reminder_Sent__c = false AND ContactId IN : contacts];
    
        
        
        
 //ORG Wide Email Addresses lists   
        
    OrgWideEmailAddress AST = new OrgWideEmailAddress();
                AST = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress Where DisplayName='Austin']; 
        
    OrgWideEmailAddress ATL = new OrgWideEmailAddress();
                ATL = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress Where DisplayName='Atlanta'];
        

        
        
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for (Case cs : cases)
    {
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    
    List<String> toAddress = new List<String>();
    toAddress.add(cs.ContactEmail);
    email.setToAddresses(toAddress);
    
    email.setWhatId(cs.Id);
    email.setTargetObjectId(cs.ContactId);
    email.setTemplateId('00X7Z0000027tioUAA');
    
    email.setTreatTargetObjectAsRecipient(false);
    //email.setOrgWideEmailAddressId(owea.id);
    //List<String> ccAddress = new List<String>();
    //email.setCcAddresses(ccAddress);
    //email.setSaveAsActivity(true); 
    
    
    //First email reminder
    //Letter_Sent_Date__c and Follow_up_Date__c updated in Cases Processes for first reminder
    
    if (cs.Responsibility_Center__c =='AST' && cs.status=='Ready for Email' && cs.Letter_Sent__c == false && cs.Reminder_Sent__c == false)
    {
        cs.status = 'Waiting for Customer';
        cs.Letter_Sent__c = true;
        email.setOrgWideEmailAddressId(AST.id);
    }
    
    if (cs.Responsibility_Center__c =='ATL' && cs.status=='Ready for Email' && cs.Letter_Sent__c == false && cs.Reminder_Sent__c == false)
    {
        cs.status = 'Waiting for Customer';
        cs.Letter_Sent__c = true;
        email.setOrgWideEmailAddressId(ATL.id);
    } 
        
        else
    {
        system.debug('no mail was sent');
    }
    
    emails.add(email);}
   
    
   //Second email reminder
   
    for (Case cs2 : cases2)
{
    Messaging.SingleEmailMessage email2 = new Messaging.SingleEmailMessage();
    
    List<String> toAddress2 = new List<String>();
    toAddress2.add(cs2.ContactEmail);
    email2.setToAddresses(toAddress2);
    
    email2.setWhatId(cs2.Id);
    email2.setTargetObjectId(cs2.ContactId);
    email2.setTemplateId('00X7Z0000027tioUAA');
    
    email2.setTreatTargetObjectAsRecipient(false);
    cs2.Reminder_Sent__c = true;
   
    if (cs2.Responsibility_Center__c =='AST' && cs2.status=='Waiting for Customer' && cs2.Letter_Sent__c == true && cs2.Follow_Up_Date_in_Days__c == 0)
    {     
        cs2.Reminder_Sent__c = true;
        email2.setOrgWideEmailAddressId(AST.id);
    }
    
    if (cs2.Responsibility_Center__c =='ATL' && cs2.status=='Waiting for Customer' && cs2.Letter_Sent__c == true && cs2.Follow_Up_Date_in_Days__c == 0)
    {
        cs2.Reminder_Sent__c = true;
        email2.setOrgWideEmailAddressId(ATL.id);
    }
    
    else
    {
        system.debug('no mail was sent');
    }
    
    emails.add(email2);
    }
   else
    {
        system.debug('no mail was sent');
    }
    
 
    emails.add(email2);
} 

    Messaging.sendEmail(emails); 
    update cases;
    update cases2;    
  } 
}

Test Class:
 
@isTest(seeAllData=true)
public class EmailClassTest {
   @isTest
    public static void sendEmail(){
            
        //Insert Profile
      Profile cus = [SELECT Id FROM Profile WHERE Name='DGS - Customer Service'];   
        
        
        //Insert User
      User us = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = cus.Id, CompanyName = 'E3',
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com'); 
        
        
        //Insert Account
      Account acct = new Account(Name='TestClassAccount', Customer_Segment__c = 'Hospitals', Calibration_Intervals__c = 'Half year');
      insert acct;
        
        
        //Insert Contact
      Contact con = new Contact(LastName='JasonSendEmail', email='sendemail@toatlanta.com', AccountId=acct.Id);
      insert con;
        
        
        //Insert Responsibility Center
      Responsibility_Center__c rc = new Responsibility_Center__c(Name='e3 AST');
        
        
        //Insert Case
       Case cas = new Case(Status = 'Ready for Email', ContactId=con.id, subject='Periodic Service', AccountId = acct.id,
                          Case_Reason__c = 'Business related', Reason = 'General Enquiry', Responsibility_Center_lookup__c=rc.id, Reminder_Sent__c = false, Letter_Sent__c = false,
                          Follow_up_date__c = datetime.newInstance(2022, 10, 15, 12, 30, 0));
       insert cas; 
        //Insert 2nd Case
       Case cas2 = new Case(Follow_up_Date__c = date.today(), Status = 'Waiting for Customer', ContactId=con.id, subject='Periodic Service', AccountId = acct.id,
                          Case_Reason__c = 'Business related', Reason = 'General Enquiry', Responsibility_Center_lookup__c=rc.id, Reminder_Sent__c = false, Letter_Sent__c = true);
       insert cas2; 
         
        
       //Insert Org-wide
       OrgWideEmailAddress[] addresses = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress Where DisplayName='Austin'];      
            
    
       Test.startTest();
       System.assertEquals(0, Limits.getEmailInvocations(), 'No emails should be sent');

       EmailClass.sendEmail();

       System.assertEquals(1, Limits.getEmailInvocations(), 'Emails should be sent');
      
       Test.stopTest();
        
     
}
}

 
Hi Everyone! :) 

On PricebookEntry object, I have a 2 lookup fields: 
- Pricebook2Id (standard field filled by Integration)
- Price_Book_2__c (custom field needed for Related list)
I want to fill the Price_Book_2__c using the value in Pricebook2Id.

I am trying to write some class, it will be my first apex class, and still I get errors...

I want to run it from Flow.

Here is code:
public class PriceBookField{
@InvocableVariable(label='Get Price Book records')
    public PriceBookEntry p;
    @InvocableMethod
    public static void populateField(List<PricebookEntry>records){
        records[0].Price_Book_2__c = records[0].Pricebook2Id;
    update records;
}
}


Can you help me?
Hi Everyone! :) 

Email-to-case doesn't work with Lightning Email Templates. I am using Flow and Email Alerts to send the emails to customers. Under Email tab on Case I can see only emails with Classic Template. The same situation with response from customer's email box. I can see only responses for Classic Templates, but not for Lightning.

Do you have any idea? :) 
Hi Experts!

We have 3 standard object in Salesforce: Account, Pricebook2 and PricebookEntry. 

Account has lookup relation to Pricebook2, and Pricebook2 has relation to PricebookEntry.

Is any possibility to create list on Account object, which will show the PricebookEntry records from right Pricebook2?

User-added image
Hi everyone! :) 
 
I have a problem, that Assistant in Sales app stopped working for users with different profile than Administator. Do you know how to set up it correctly? They have permission as Standard Einstein Activity Capture or Sales Cloud Einstein.
 
What they see: 
User-added image

What I see:
User-added image
Hi everyone! :) 

I have 2 standard object on Salesforce: Case and Asset. On Case object I created 2 custom checkbox fields: 
- In_House_Service__c
- Field_Service__c
Depends of Preferred_Service_Location__c on Asset, the right checkbox should be true on Case:
- if In-house service on Asset - the In_House_Service__c should be true on Case
- if Field Service on Asset - the Field Service__c should be true on Case

I am super newbie in Apex, I don't know how to write the right code. Maybe do you have any examples, that I can build or base on it?

I will be grateful! Thank you!
Hi! :) 

Is there any way to save emails on Case record which were sent using Flow?

Now they are saved when User send the email directly from Case. I want to make automatization which will send email and save email on Case record. Thanks to that user can see what was sent to Customer. 
Hi :) I try to write Apex Trigger for Work Order object, which prevent user to Completed the Work Order if Service_Type__c field is empty. 
Can someone check my code? I get the issue that is unexpected token '<'. Thank you! 

trigger ValidateWorkOrderLineItem on WorkOrder (before update) {
  Map<String, WorkOrder> mapWoToId = new Map<String,WorkOrder>();
  for(WorkOrder w : Trigger.New) {
    if(w.Status =='Completed'){
      mapWoToId.put(w.Id, w);
    }
  }
  
  List<WorkOrderLineItem> woLineItemList = [select woli.Status, woli.workOrderId
                        from WorkOrderLineItem woli 
                        where woli.WorkOrderId IN :mapWoToId.keySet() and woli.Status != 'Completed' and woli.WOLI__c == 'Parent' and woli.Parent_WOLI__c == TRUE and woli.Service_type__c == '']; 

  for(WorkOrderLineItem woli : woLineItemList) {
      WorkOrder parentWO = mapWoToId.get(woli.workOrderId);
      parentWO.addError('Please fill the Service Type on each Parent Work Order Line Item');
  } 
}
Hello everyone! :) 

I know that can be a stupid question, but how to create history list based on status path on Work Order records?
I want to know who and when change the record status.
User-added image