Showing posts with label SFDX. Show all posts
Showing posts with label SFDX. Show all posts

Tuesday, March 17, 2020

how to clone a record using apex in salesforce

Salesforce sObject class has a method called clone() which creates a copy of the sObject record. This method has four Boolean type optional parameters.
clone(Boolean preserveId, Boolean isDeepClone, Boolean preserveReadonlyTimestamps, Boolean preserveAutonumber)

 preserveId:  Determines whether the ID of the original object is kept or cleared in the duplicate. If set to true, the ID is copied to the duplicate. The default is false, that is, the ID is cleared.

 isDeepClone:  Determines whether the method creates a full copy of the sObject field or just a reference:
* If set to true, the method creates a full copy of the sObject. All fields on the sObject are duplicated in memory, including relationship fields. Consequently, if you make changes to a field on the cloned sObject, the original sObject is not affected.
* If set to false, the method performs a shallow copy of the sObject fields. All copied relationship fields reference the original sObjects. Consequently, if you make changes to a relationship field on the cloned sObject, the corresponding field on the original sObject is also affected, and vice-versa. The default is false.

preserveReadonlyTimestamps: Determines whether the read-only timestamp fields are preserved or cleared in the duplicate. If set to true, the read-only fields CreatedById, CreatedDate, LastModifiedById, and LastModifiedDate are copied to the duplicate. The default is false, that is, the values are cleared.

preserveAutonumber: Determines whether auto-number fields of the original object are preserved or cleared in the duplicate. If set to true, auto number fields are copied to the cloned object. The default is false, that is, auto number fields are cleared.

Example
Account objAcc = new Account(Name = 'Test Acc', Industry = 'Banking', Phone = '1234567890');
insert objAcc;    
 
//Clone the record
Account accClone = objAcc.clone(false, false, false, false);
insert accClone;


Below are the few of the cloning methods:

isClone() - Returns true if an entity is cloned from something, even if the entity hasn’t been saved.

Example
trigger clonetest on sObject (before insert) {
    for (sObject obj : Trigger.new) {
        if (obj.isClone()) {
            System.debug( 'isClone: ====>  '+ obj.isClone());
        }
    }
}


getCloneSourceId() - Method gives the salesforce id of the source record from where the new record is cloned.

Example
 trigger clonetest on sObject (before insert) {
    for (sObject obj : Trigger.new) {
        if (obj.isClone()) {
            System.debug( ' isClone: ====>  '+ obj.getCloneSourceId());
        }
    }
}

NOTE: getCloneSourceId() Always return null in salesforce lightning. This is already reported as BUG in salesforce.


Saturday, October 12, 2019

Useful CLI commands for Salesforce DX

Salesforce DX CLI is a powerful command-line interface, a tool that simplifies development and builds automation when working with your Salesforce org.

Open a new terminal window and run the following command to get the commands list.
sfdx force:doc:commands:list


SFDX CLI Commands List

=== Commands
  force:alias:list                    // list username aliases for the Salesforce CLI
  force:alias:set                     // set username aliases for the Salesforce CLI
  force:apex:class:create             // create an Apex class
  force:apex:execute                  // execute anonymous Apex code
  force:apex:log:get                  // fetch the last debug log
  force:apex:log:list                 // list debug logs
  force:apex:log:tail                 // start debug logging and display logs
  force:apex:test:report              // display test results
  force:apex:test:run                 // invoke Apex tests
  force:apex:trigger:create           // create an Apex trigger
  force:auth:jwt:grant                // authorize an org using the JWT flow
  force:auth:list                     // list auth connection information
  force:auth:logout                   // log out from authorized orgs
  force:auth:sfdxurl:store            // authorize an org using an SFDX auth URL
  force:auth:web:login                // authorize an org using the web login flow
  force:config:get                    // get config var values for given names
  force:config:list                   // list config vars for the Salesforce CLI
  force:config:set                    // set config vars for the Salesforce CLI
  force:data:bulk:delete              // bulk delete records from a csv file
  force:data:bulk:status              // view the status of a bulk data load job or batch
  force:data:bulk:upsert              // bulk upsert records from a CSV file
  force:data:record:create            // create a record
  force:data:record:delete            // delete a record
  force:data:record:get               // view a record
  force:data:record:update            // update a record
  force:data:soql:query               // execute a SOQL query
  force:data:tree:export              // export data from an org into sObject tree format for force:data:tree:import consumption
  force:data:tree:import              // import data into an org using SObject Tree Save API
  force:doc:commands:display          // display help for force commands
  force:doc:commands:list             // list the force commands
  force:lightning:app:create          // create a Lightning app
  force:lightning:component:create    // create a bundle for an Aura component or a Lightning web component
  force:lightning:event:create        // create a Lightning event
  force:lightning:interface:create    // create a Lightning interface
  force:lightning:lint                // analyze (lint) Aura component code
  force:lightning:lwc:start           // Develop Lightning Web Component modules and see live changes without publishing your components to an org.
  force:lightning:test:create         // create a Lightning test
  force:lightning:test:install        // install Lightning Testing Service unmanaged package in your org
  force:lightning:test:run            // invoke Aura component tests
  force:limits:api:display            // display current org’s limits
  force:mdapi:convert                 // convert metadata from the Metadata API format into the source format
  force:mdapi:deploy                  // deploy metadata to an org using Metadata API
  force:mdapi:deploy:cancel           // cancel a metadata deployment
  force:mdapi:deploy:report           // check the status of a metadata deployment
  force:mdapi:describemetadata        // display the metadata types enabled for your org
  force:mdapi:listmetadata            // display properties of metadata components of a specified type
  force:mdapi:retrieve                // retrieve metadata from an org using Metadata API
  force:mdapi:retrieve:report         // check the status of a metadata retrieval
  force:org:clone                     // clone a sandbox org
  force:org:create                    // create a scratch or sandbox org
  force:org:delete                    // mark a scratch org for deletion
  force:org:display                   // get org description
  force:org:list                      // list all orgs you’ve created or authenticated to
  force:org:open                      // open an org in your browser
  force:org:shape:create              // create a snapshot of org edition, features, and licenses
  force:org:shape:delete              // delete all org shapes for a target org
  force:org:shape:list                // list all org shapes you’ve created
  force:org:snapshot:create           // snapshot a scratch org
  force:org:snapshot:delete           // delete a scratch org snapshot
  force:org:snapshot:get              // get details about a scratch org snapshot
  force:org:snapshot:list             // list scratch org snapshots
  force:org:status                    // report sandbox org creation status and headlessly authenticate to org
  force:package1:version:create       // create a first-generation package version in the release org
  force:package1:version:create:get   // retrieve the status of a package version creation request
  force:package1:version:display      // display details about a first-generation package version
  force:package1:version:list         // list package versions for the specified first-generation package or for the org
  force:package:create                // create a package
  force:package:hammertest:list       // list the statuses of running and completed ISV Hammer tests
  force:package:hammertest:report     // display the status or results of a ISV Hammer test
  force:package:hammertest:run        // run ISV Hammer
  force:package:install               // install a package in the target org
  force:package:install:report        // retrieve the status of a package installation request
  force:package:installed:list        // list the org’s installed packages
  force:package:list                  // list all packages in the Dev Hub org
  force:package:uninstall             // uninstall a second-generation package from the target org
  force:package:uninstall:report      // retrieve status of package uninstall request
  force:package:update                // update package details
  force:package:version:create        // create a package version
  force:package:version:create:list   // list package version creation requests
  force:package:version:create:report // retrieve details about a package version creation request
  force:package:version:list          // list all package versions in the Dev Hub org
  force:package:version:promote       // promote a package version to released
  force:package:version:report        // retrieve details about a package version in the Dev Hub org
  force:package:version:update        // update a package version
  force:project:create                // create a Salesforce DX project
  force:project:upgrade               // update project config files to the latest format
  force:schema:sobject:describe       // describe an object
  force:schema:sobject:list           // list all objects of a specified category
  force:source:convert                // convert source into Metadata API format
  force:source:delete                 // delete source from your project and from a non-source-tracked org
  force:source:deploy                 // deploy source to an org
  force:source:deploy:cancel          // cancel a source deployment
  force:source:deploy:report          // check the status of a metadata deployment
  force:source:diff                   // diff a local source file against the version in your org
  force:source:open                   // edit a Lightning Page with Lightning App Builder
  force:source:pull                   // pull source from the scratch org to the project
  force:source:push                   // push source to a scratch org from the project
  force:source:retrieve               // retrieve source from an org
  force:source:status                 // list local changes and/or changes in a scratch org
  force:user:create                   // create a user for a scratch org
  force:user:display                  // displays information about a user of a scratch org
  force:user:list                     // lists all users of a scratch org
  force:user:password:generate        // generate a password for scratch org users
  force:user:permset:assign           // assign a permission set to one or more users of an org
  force:visualforce:component:create  // create a Visualforce component
  force:visualforce:page:create       // create a Visualforce page

Tuesday, February 19, 2019

Salesforce DX(SFDX) setup for non scratch org(Developer/Sandbox)

This post explains how to set up Salesforce DX(SFDX) for non-scratch org(Developer/Sandbox)

Step 1: install salesforce CLI and VS Code

1. Download and Install Salesforce CLI
2. Download vs code From this link and install.

Note: Once installed the Salesforce CLI you can verify using below command
sfdx --version
3. Install salesforcedx plugin using below command
sfdx plugins:install salesforcedx

Verify installed plugins using below command
sfdx plugins

Installing Salesforce Extensions in vs code
1. Open vs code install Salesforce extension pack

Step 2: Create a Project

Once complete the Step1, open the command palette
To open the command palette, press Cmd+Shift+P (macOS) or Ctrl+Shift+P (Windows or Linux).


Enter project Name and Select Folder
Once create the Project, configure the below files
1. sfdx-project.json
2. package.xml
sfdx-project.json
 if you are connecting to sandbox change the sfdcloginurl to test.salesforce.com
check below screenshot

package.xml
go to manifest folder ---> package.xml
add the components which data you want to retrieve in the package.xml
check below screenshot

Step 3: Authorize an Org

once complete the step2, open command palette then select SFDX: Authorize an Org

this command opens your default browser then login to your developer/sandbox org
once logged in to your org you may close your browser.

Step 4: Retrieve the source from org

once complete the step3, go to your package.xml, right click on the package.xml
select retrieve source in manifest from Org.
check screenshot
this command retrieves the source from your org, it is taken some time based on data in your Org.

Step 5: Deploy or retrieve the code from the server

Once you retrieve the source form your org, to open any file in the project folder press ctrl+p
it opens search palette.
check screenshot

if you want to save the code into your org, right click on that file, select SFDX: Deploy to org
if you want to refresh the file from server select SFDX: Retrieve the source from org
check below screenshot

That's it.
Let us know if you have any queries.