Skip to main content
2 réponses
  1. 9 juin 2021, 13:32
    Hi Piyush,

    You can take references from below code.

    ----Apex class

    public class InsertContact_Controller14 {

        

        @AuraEnabled

        public static boolean contactInsert(string conObj ,string accountId){

            try{

                

                contact con = new contact();

                con.LastName=conObj;

                con.AccountId=accountId;

                

                insert con;

                system.debug('con-->'+con);

                return true;

            }

            catch(Exception e){

                system.debug('Message-->'+e.getMessage());

                system.debug('Line No-->'+e.getLineNumber());

                

                return false;

            }

        }

        @AuraEnabled

        public static List<Account> AccountFetch(){

            

            system.debug('Account Find');

            List<Account> accList = [SELECT id ,Name FROM Account limit 100];

            return accList ;

        }  

    }

    -------html

    <template>

        <lightning-card title="Create Account With Contact" icon-name="standard:contact">

          

                    <div class="form-group">

                        <lightning-input label="Enter Last Name" name="accName" required="required" type="text"  onchange={handleNameChange}></lightning-input>

                    </div>

                    <div class="slds-m-around_medium">

                        <select class="slds-select" onchange={AccountIdFetch}>

                        <option>Select Account</option>

                        <template for:each ={acc} for:item="item">

                        <option key={item.Id} value={item.Id}>{item.Name}</option>

                        </template>

                        </select>

                        </div>

                    

                <div>

                        <lightning-button label="submit" onclick={forSubmit}></lightning-button>

                    </div>

                 

        </lightning-card>

        

    </template>

    ------.js

    import { LightningElement, track } from 'lwc';

    import fetchData from '@salesforce/apex/InsertContact_Controller14.contactInsert';

    import fetchAccount from'@salesforce/apex/InsertContact_Controller14.AccountFetch';

    export default class Question8 extends LightningElement {

    @track acc;

    @track lName;

    @track accId;

     message;

    handleNameChange(event) {

        this.lName=event.target.value;

        console.log('Name',this.lName);

    }

        connectedCallback(){

            fetchAccount()

            .then(result => {

            this.acc = result;

            

            console.log(JSON.stringify(result));

            console.log("result",this.acc);

            })

           

        }    

        AccountIdFetch(event){

            this.accId=event.target.value;

            console.log('accId',accId);

        }

        forSubmit(event){

            fetchData({conObj :this.lName ,accountId : this.accId })

            .then(result => {

            this.message = result;

            

            console.log(JSON.stringify(result));

            console.log("result",this.message);

            })

            .catch(error => {

                this.error = error.message;

            });

        }

        

    }

    If you find your Solution then mark this as the best answer. 

    Thank you!

    Regards 

    Suraj Tripathi
0/9000