0

apex class

public with sharing class BringEmployeesAble {
    
    public List<Project_skill__c> contactList {get;set;}
    public BringEmployeesAble(){
        this.contactList = new List<Project_skill__c>();

    }

    public static String project{get;set;}

    String Employee;
    @AuraEnabled(cacheable=true)
    public static list<Employee__c> getRecordsofprojectname(string searchKey){
                
        list<Project_skill__c> lsy = [SELECT Id, Project__c, Name 
                                        FROM Project_skill__c 
                                        WHERE Id =: searchKey];
        string k = lsy[0].Name;
        project = lsy[0].Project__c;
        List<string> p = new List<String>();
        p.add(k);
        system.debug('///////////dummy code starts here        '+project );

        list<Employee__c> lsxy = [SELECT Id, Name, Name__c, (SELECT Id, Name
                                        FROM Employee_Skills__r WHERE Name IN:p)
                                    FROM Employee__c 
                                    WHERE Employee_Status__c ='Unassigned'];

        system.debug('///////////dummy code ends here        '+p );
        Set<Id> parentIdSet = new Set<Id>(); // Create a set to hold the Ids

        for (Employee_Skill__c obj : [SELECT Employee__c FROM Employee_Skill__c WHERE Name IN:p]) {
            parentIdSet.add(obj.Employee__c); 
            // Loop through the child objects, getting those whose status is "Completed"
        }

        //for (Employee__c obj : [SELECT Name FROM Parent_Object__c WHERE Id IN :parentIdSet]) {
            // Do something... // Query the parent based on Ids stored in the Set<Id>
        //}

        list<Employee__c> j = [SELECT Id, Name, Name__c FROM Employee__c WHERE Id IN :parentIdSet];

        system.debug('///////////irfan        '+j);

        return j;
    }

    @AuraEnabled(cacheable=true)
    public static list<string> giveproject(string searchKey){
        list<Project_skill__c> lsy = [SELECT Id, Project__c, Name 
                    FROM Project_skill__c 
                    WHERE Id =: searchKey];
        string k = lsy[0].Project__c;

        // project = lsy[0].Project__c;
        List<string> proj = new List<String>();
        proj.add(k);

        return proj;
    }

    @AuraEnabled
    public static void createPAs(Project_Assignment__c searchKey){
        System.debug('/////////////////      oliv     '+searchKey);
    
        List <Project_Assignment__c> lv = new List <Project_Assignment__c>();
        try {
            Project_Assignment__c varnew = new Project_Assignment__c();
            varnew.Project__c = searchKey.Project__c;
            varnew.Employee__c = searchKey.Employee__c;
            lv.add(varnew);
            insert lv;

            System.debug('/////////////////      donedude');
        } catch (Exception e) {
            System.debug('/////////////////      erdude     ');  
        }
        
        Project_Assignment__c l = searchKey;
        //return l;
    }
}

Js code

import { LightningElement ,track,api,wire } from 'lwc';
//import NAME_FIELD from '@salesforce/schema/Account.Name';
import getRecordsofproassigns from '@salesforce/apex/BringEmployeesAble.getRecordsofprojectname';

import createpas from '@salesforce/apex/BringEmployeesAble.createPAs';
import giveproject from '@salesforce/apex/BringEmployeesAble.giveproject';

//import ACCOUNT_OBJECT from '@salesforce/schema/Project_Assignment__c';
import ENAME_FIELD from '@salesforce/schema/Project_Assignment__c.Employee__c';
import PROJECT_FIELD from '@salesforce/schema/Project_Assignment__c.Project__c';
//import TECHSKILL_FIELD from '@salesforce/schema/Project_Assignment__c.Project_Tech_skill__c';

let i=0;
export default class ToCreatePAs extends LightningElement {
    @track searchKey = '';
    @api recordId;
    @track error;   //this holds errors

    @track items = []; //this holds the array for records with value & label

    @track value = '';  //this displays selected value of combo box

    @track pname = PROJECT_FIELD;
    @track ename = ENAME_FIELD;

    @track pvar;

    rec = {
      Project__c : this.pname,
      Employee__c : this.ename
    }
    
    @wire(giveproject, { searchKey: '$searchKey' })
    project({ error, data }) {
        if (data) {
            for(i = 0; i < data.length; i++) {
                console.log('id=' + data[i].Id);
                //this.items = [...this.items ,{value: data[i].Id , label: data[i].Name__c}];
                this.pvar = data[0];                                   
            }
            this.error = undefined;
        } else if (error) {
            this.error = error;
        }
    };    

    @wire(getRecordsofproassigns, { searchKey: '$searchKey' })
    Employees({ error, data }) {
        if (data) {
            for(i = 0; i < data.length; i++) {
                console.log('id=' + data[i].Id);
                this.items = [...this.items ,{value: data[i].Id , label: data[i].Name__c}];                                   
            }
            this.error = undefined;
        } else if (error) {
            this.error = error;
        }
    }

    get statusOptions() {
        console.log(this.items);
        return this.items;
    }

    connectedCallback(){
        this.searchKey = this.recordId;
        //this.rec.Project__c = this.pvar;
    }

    renderedCallback() {
        this.rec.Project__c = this.pvar;    
    }

    //selectedLabel='';
    // handleChange(event) {
        // Get the string of the "value" attribute on the selected option
        //const selectedOption = event.detail.value;
        //console.log('from irfan  selectedOption=' + selectedOption);
        //this.selectedLabel = event.target.options.find(opt => opt.value === event.detail.value).label;
    
        //This is for event propagation
      
        //const filterChangeEvent = new CustomEvent('filterchange', { detail: { selectedOption },});
    
        // Fire the custom event
        //this.dispatchEvent(filterChangeEvent);
    //}

    searchKey = '';
    contacts;
    error;

    handleKeyChange(event) {
        this.searchKey = event.target.value;
        this.rec.Employee__c = event.target.value;
        console.log('from irfan  selectedOption=' + this.searchKey);
    }

    handleSearch() {
        createpas({ searchKey: this.rec })
            .then((result) => {
                this.contacts = result;
                this.error = undefined;
            })
            .catch((error) => {
                this.error = error;
                this.contacts = undefined;
            });
    }
}

html

<template>
    <lightning-card title="List of Employees with this Project Skill " icon-name="custom:custom63">
        <lightning-combobox
            name="contacts"
            label="Employees"
            placeholder="Choose Employee"
            value={value}
            onchange={handleKeyChange}
            options={statusOptions}>
        </lightning-combobox>
        <br/>
        <lightning-button variant="brand" 
            label="Create Assignment with the Above Employee" 
            onclick={handleSearch} class="slds-m-left_x-small">
        </lightning-button>

    </lightning-card>

    <!--lightning-record-form object-api-name="Project_Assignment__c" record-id={strRecordId}
        columns="2" mode="edit" fields={arrayFields}></lightning-record-form-->

    <!--lightning-record-form
        object-api-name="Project_Assignment__c"
        fields={myFields}
        onsuccess={handleAccountCreated}>
    </lightning-record-form-->       
</template>

I have a two distinct master object Project__c and Employee__c connected to the detail object Project_Assignment__c , (master-details relationship). I created apex class to fetch the project details.

Basically, I placed the lwc component on the project_skill__c which is other detail of project__c.

I have to match project skill and to employee skill (each employee has one skill, it is detail side of Employee object), and finally create the Project assignment which obviously has project and employee fields mandatory filled.

I tried to insert this but it is giving me

EXCEPTION_THROWN [79]|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Duplicate actives is not Allowed : []

But no custom validation rules exists in any of the master details object.

-- Error in dev console I am getting is error i am getting

I am getting the project and employee ids in apex class getting the project and employee ids in apex cls

2
  • 1
    apart from standard validation rule, error can also be thrown from apex using .adderror() method, i would advise you to check debug logs. you will find issue. May 21, 2022 at 14:44
  • Yes true, later I recognised it with that error message and remembered I gave it in trigger. So i solved it, Now it's working fine
    – Irfan
    May 23, 2022 at 9:03

1 Answer 1

0

Thank you I figured it out, it was error showing from Trigger which I created on Project assignment object. If any guys face such error, Kindly check your validation rule, Triggers and apex classes for this kind of custom error message which you receive(in my case 'Duplicate actives not allowed' as this message I gave to add error in one of my trigger).

Thank you all for reading.

You must log in to answer this question.

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