function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
salesforce sfdxsalesforce sfdx 

Requirement to create Records depending on the Quantity in Flow

Custom Object: Product__c
Custom Fields: Name, Quantity__c, Address__c, EmiNumber__C.

If User Creates a record :

Name ='Samsung Pro Max';
Quantity=10;
Address='xxxx'.



After Save
Here i need to Create same product with ten Records.
Say...!

Name ='Samsung Pro Max';
Quantity =1;
Address='xxxx'.

Name ='Samsung Pro Max';
Quantity =1;
Address='xxxx'.

Name ='Samsung Pro Max';
Quantity =1;
Address='xxxx'.

------------->so on 10 times
including the first record  to be updated as 1
so that i can track each item seperately .
Can this action can be acheived using the flow.


Thanks 
Prathushya Reddy.




 
Vineeth ReddyVineeth Reddy
You can achieve this with a simple trigger on the Product__c object.

Trigger code below :
 
trigger ProductTrigger on Product__c (after insert) {

    // declare a list variable to store all the new products to be inserted
    List<Product__c> productsToInsert = new List<Product__c>();

    // loop through each product in the trigger context
    for(Product__c product : Trigger.new) {

        // loop 10 times to create a product if the product quantity is 10, 
        //if the product quantity is 20, this loop will run 20 times
        for(Integer i=0; i<product.Quantity__c; i++) {

            // create the data for the new product to be inserted
            Product__c newProduct = new Product__c();
            newProduct.Name = product.Name;
            newProduct.Quantity__c = 1;
            newProduct.Address = product.Address__c;

            productsToInsert.add(newProduct);
        }
    }

    // finally insert all the newly created products at once to database
    if(productsToInsert.size()>0)
        insert productsToInsert;
}

 
Vikram SFDCVikram SFDC
Hi Prathushya,
Try This below code if you are searching for Apex code.

Apex Class :- 
public class ProductClass {
    public static boolean firstTime = true;
    public static void ProductMethod(){
        list<product__c> Pl = new list<Product__c>();
        for(integer i=0; i<=10; i++){
            Product__c p = new Product__c();
            p.Name = 'Samsung Pro Max '+i;
            p.Quantity__c= 1;
            p.Address__c = 'xxxx';
            pl.add(p);
        }insert pl;
    }
}

Trigger Code :- 
trigger ProductTrig on Product__c (after insert){
    if(ProductClass.firstTime == true){
        ProductClass.firstTime = false;
        ProductClass.ProductMethod();
    }
}


Note :- Let me know if it works are not.