static object transfer from one user to another user


html:-

<template>

    <lightning-card title="Opportunity Transfer to Users Model 1">

        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 

        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

        <lightning-layout horizontal-align="center" vertical-align="end">

        <lightning-layout-item >

        <lightning-button label="Selected" variant="Brand" onclick={selectAllHandler}></lightning-button>&nbsp;

        </lightning-layout-item> &nbsp;

        <lightning-layout-item >

            <lightning-combobox label="Users" options={userop} values={value} onchange={userselect} style="color:rgb(230, 12, 12);width:100px;"></lightning-combobox>

        </lightning-layout-item>&nbsp;&nbsp;

        <lightning-layout-item >

        <lightning-button label="Transfer" variant="Brand" onclick={updateuserdata}></lightning-button> 

        </lightning-layout-item> 


    

</lightning-layout>

    

        <br/><br/>

        <br/>

        <br/>


       

            <div style="height: 300px;">

                <lightning-datatable

                        key-field="id"

                        data={chandu.data}

                        columns={columns}>

                </lightning-datatable>

            </div>

        

    </lightning-card>

</template>


<template>
    <lightning-card title="Opportunity Transfer to Users Model 1">
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        <lightning-layout horizontal-align="center" vertical-align="end">
        <lightning-layout-item >
        <lightning-button label="Selected" variant="Brand" onclick={selectAllHandler}></lightning-button>&nbsp;
        </lightning-layout-item> &nbsp;
        <lightning-layout-item >
            <lightning-combobox label="Users" options={userop} values={value} onchange={userselect} style="color:rgb(230, 12, 12);width:100px;"></lightning-combobox>
        </lightning-layout-item>&nbsp;&nbsp;
        <lightning-layout-item >
        <lightning-button label="Transfer" variant="Brand" onclick={updateuserdata}></lightning-button>
        </lightning-layout-item>

   
</lightning-layout>
   
        <br/><br/>
        <br/>
        <br/>

       
            <div style="height: 300px;">
                <lightning-datatable
                        key-field="id"
                        data={chandu.data}
                        columns={columns}>
                </lightning-datatable>
            </div>
       
    </lightning-card>
</template>

js:-

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

import getOpportunitys from '@salesforce/apex/guru_Apexclass.getOpportunitys';

import user1 from '@salesforce/apex/guru_Apexclass.user1';

import UpdatedOpptyOwnerId from '@salesforce/apex/guru_Apexclass.UpdatedOpptyOwnerId';



const columns = [

    { label: 'Name', fieldName: 'Name' },

    { label: 'Stage', fieldName: 'StageName', type: 'Picklist' },

    { label: 'Close Date', fieldName: 'CloseDate', type: 'Date' },

    { label: 'LeadSource', fieldName: 'LeadSource', type: 'Picklist' }

];


export default class Guru_component extends LightningElement 

{

    checkedBoxesIds=[];

value='';

userid1=''

    selectedIds=[];

    useroptions=[];

    selectednewoppids=[];

    @track columns = columns;

    

    @wire(getOpportunitys)chandu;


    @wire(user1)guru({data,error}){

   if(data)

   {    for(let i=0;i<=data.length;i++)

    {

        this.useroptions=[...this.useroptions,{label:data[i].Name,value:data[i].Id}];

        console.log('user options-->',this.useroptions);

    }if(error)

    {

        console.log(error);

    }

        

   }}

   updateuserdata(){

    UpdatedOpptyOwnerId({lstId:this.selectedIds,userId2:this.value}).then(result=>{

        this.result1=result;

        console.log('result-->',this.result1)

    }

    ).catch(error=>{

        console.log(error)

    });


   }


   

    get userop()

    {

        return this.useroptions;

    }

    selectHandler(){


    }

    userselect(event)

    {

    this.value=event.detail.value;

     alert(this.value);

    console.log('userid--->',this.value);

    }


    selectAllHandler(){

        

      

            var selectedRecords =  this.template.querySelector("lightning-datatable").getSelectedRows();


          console.log('selected records-->',selectedRecords)

            const ids = selectedRecords.map(obj => obj.Id);

             console.log('Idssss-->',ids);

             this.selectedIds=ids;


            

            }   

          }


import { LightningElement,track,wire} from 'lwc';
import getOpportunitys from '@salesforce/apex/guru_Apexclass.getOpportunitys';
import user1 from '@salesforce/apex/guru_Apexclass.user1';
import UpdatedOpptyOwnerId from '@salesforce/apex/guru_Apexclass.UpdatedOpptyOwnerId';


const columns = [
    { label: 'Name', fieldName: 'Name' },
    { label: 'Stage', fieldName: 'StageName', type: 'Picklist' },
    { label: 'Close Date', fieldName: 'CloseDate', type: 'Date' },
    { label: 'LeadSource', fieldName: 'LeadSource', type: 'Picklist' }
];

export default class Guru_component extends LightningElement
{
    checkedBoxesIds=[];
value='';
userid1=''
    selectedIds=[];
    useroptions=[];
    selectednewoppids=[];
    @track columns = columns;
   
    @wire(getOpportunitys)chandu;

    @wire(user1)guru({data,error}){
   if(data)
   {    for(let i=0;i<=data.length;i++)
    {
        this.useroptions=[...this.useroptions,{label:data[i].Name,value:data[i].Id}];
        console.log('user options-->',this.useroptions);
    }if(error)
    {
        console.log(error);
    }
       
   }}
   updateuserdata(){
    UpdatedOpptyOwnerId({lstId:this.selectedIds,userId2:this.value}).then(result=>{
        this.result1=result;
        console.log('result-->',this.result1)
    }
    ).catch(error=>{
        console.log(error)
    });

   }

   
    get userop()
    {
        return this.useroptions;
    }
    selectHandler(){

    }
    userselect(event)
    {
    this.value=event.detail.value;
     alert(this.value);
    console.log('userid--->',this.value);
    }

    selectAllHandler(){
       
     
            var selectedRecords =  this.template.querySelector("lightning-datatable").getSelectedRows();

          console.log('selected records-->',selectedRecords)
            const ids = selectedRecords.map(obj => obj.Id);
             console.log('Idssss-->',ids);
             this.selectedIds=ids;

           
            }  
          }


Apex class:-

---------------

public with sharing class guru_Apexclass {

    @AuraEnabled(cacheable=true)

    public static List<Opportunity> getOpportunitys() {

        return [select Id,Name,StageName,CloseDate,LeadSource,ownerId from Opportunity where ownerId =:userinfo.getUserId() ];

    }//




    @AuraEnabled(cacheable=true)

    public static List<user> user1(){

        return [select id,Name from user];

    }



    @AuraEnabled

    public static string UpdatedOpptyOwnerId(List<Id> lstId,string userId2){


        List<Opportunity> lstOpp=[select Id,Name,ownerId from Opportunity where Id In:lstId];

        list<Opportunity> newlist=new list<Opportunity>();

        for(Opportunity opp:lstopp)

        {

            opp.ownerId=userId2;

            newlist.add(opp);

        }

        update newlist;

        return 'records updated successfully';

    }


    @AuraEnabled(cacheable=true)

    public static List<String> model2Method(){

        List<Schema.SObjectType> objectTypes = Schema.getGlobalDescribe().Values();

        List<String> objectNames = new List<String>();

        for(Schema.SObjectType objectType : objectTypes) {

            objectNames.add(objectType.getDescribe().getName());

        

        }

        return objectNames;

    }


    @AuraEnabled

    public static string transferrecords1(string objectName, string fromuser,string touser){


        if(objectName!= ' ')

        {


        

       String fieldName = 'OwnerId';

        //String objectName = 'Account';

       // String fieldValue = '0051e000002xVEnAAM';

      //  string queryString = '0055g00000FWLshAAH';

        String soqlQuery = 'SELECT Id, OwnerId FROM ' + objectName + ' WHERE OwnerId = \'' + fromuser + '\'';

        List<SObject> records = Database.query(soqlQuery);

        for (SObject record : records) {

            record.put(fieldName, touser);

        }

        update records;

        return 'record updates successfully';


    }else {

        return 'record error';

    }

}

    

}


 

Comments