buli
2023-04-20 f0bccccbb88d93ac05010c17d4b2e0cb22a2ce9a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { createRecord } from 'lightning/uiRecordApi';
import { reduceErrors } from 'c/ldsUtils';
import CONTACT_OBJECT from '@salesforce/schema/Contact';
import CONTACT_FIRST_NAME_FIELD from '@salesforce/schema/Contact.FirstName';
import CONTACT_LAST_NAME_FIELD from '@salesforce/schema/Contact.LastName';
import OPPORTUNITY_OBJECT from '@salesforce/schema/Opportunity';
import OPPORTUNITY_NAME_FIELD from '@salesforce/schema/Opportunity.Name';
import OPPORTUNITY_STAGENAME_FIELD from '@salesforce/schema/Opportunity.StageName';
import OPPORTUNITY_CLOSEDATE_FIELD from '@salesforce/schema/Opportunity.CloseDate';
 
export default class CreateMixedRecordsWireFunctions extends LightningElement {
    contactFirstName = 'Yan';
    contactLastName = 'Khang';
    opportunityName = 'Possible deal';
 
    handleContactFirstNameInputChange(event) {
        this.contactFirstName = event.target.value;
    }
 
    handleContactLastNameInputChange(event) {
        this.contactLastName = event.target.value;
    }
 
    handleOpportunityNameInputChange(event) {
        this.opportunityName = event.target.value;
    }
 
    handleButtonClick() {
        this.createContact();
        this.createOpportunity();
    }
 
    createContact() {
        // Note: In this example we generate the record input structure from scratch for simplicity.
        // Consider to use the generateRecordInputForCreate() function instead.
        // The function will create the record input for you, including only fields that are createable.
        // Check https://developer.salesforce.com/docs/component-library/documentation/en/50.0/lwc/reference_generate_record_input_update
        const recordInput = {
            apiName: CONTACT_OBJECT.objectApiName,
            fields: {
                [CONTACT_FIRST_NAME_FIELD.fieldApiName]: this.contactFirstName,
                [CONTACT_LAST_NAME_FIELD.fieldApiName]: this.contactLastName
            }
        };
 
        createRecord(recordInput)
            .then((result) => this.handleSuccess(result.id, 'Contact'))
            .catch((error) => this.handleErrors(error));
    }
 
    createOpportunity() {
        // Note: In this example we generate the record input structure from scratch for simplicity.
        // Consider to use the generateRecordInputForCreate() function instead.
        // The function will create the record input for you, including only fields that are createable.
        // Check https://developer.salesforce.com/docs/component-library/documentation/en/50.0/lwc/reference_generate_record_input_update
        const recordInput = {
            apiName: OPPORTUNITY_OBJECT.objectApiName,
            fields: {
                [OPPORTUNITY_NAME_FIELD.fieldApiName]: this.opportunityName,
                [OPPORTUNITY_STAGENAME_FIELD.fieldApiName]: 'Prospecting',
                [OPPORTUNITY_CLOSEDATE_FIELD.fieldApiName]: new Date(2025, 1, 1)
            }
        };
 
        createRecord(recordInput)
            .then((result) => this.handleSuccess(result.id, 'Opportunity'))
            .catch((error) => this.handleErrors(error));
    }
 
    handleSuccess(recordId, object) {
        const evt = new ShowToastEvent({
            title: 'Success',
            message: `${object} created with Id: ${recordId}`,
            variant: 'success'
        });
        this.dispatchEvent(evt);
    }
 
    handleErrors(error) {
        const evt = new ShowToastEvent({
            title: 'Error',
            message: `Error creating records: ${reduceErrors(error).join(
                ', '
            )}`,
            variant: 'error'
        });
        this.dispatchEvent(evt);
    }
}