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
Krishan Gopal 27Krishan Gopal 27 

finding complexcity to right a proper if else condition

@RestResource(urlMapping = '/insertLineItems')
global class Insert_line_item {
    public String name  {get;set;}
    public String product_code {get;set;}
    public double quantity {get;set;}
    public double sale_price {get;set;}
    public class My1Exception extends Exception {} 
    public class My2Exception extends Exception {} 
    
    @httpPost
    global static void addItems()
    {
        Savepoint sp = Database.setSavepoint();
        String request = RestContext.request.requestBody.toString(); 
        RestResponse response = RestContext.response;
        ResponceHandler objResponse = new ResponceHandler();
        //  System.debug('request' + '' + request);
        Map<String,Object> body = (Map<String,Object>)JSON.deserializeUntyped(request);
        // System.debug('body' + '' + body);
        Map<String,Object> reqObject = (Map<String,Object>)body.get('req');
        //  System.debug('body' + '' + reqObject); 
        String actionpage = (String)reqObject.get('action');
        System.debug('actionpage' + '' + actionpage ); 
        Map<String,Object> customerdetail = (Map<String,Object>)reqObject.get('customer_details');
        System.debug('customerdetail ' + '' + customerdetail); 
        String email = (String)customerdetail.get('email');
        System.debug('email :' + email);
        List<Object> lineitems =(List<Object>)reqObject.get('line_items');
        // System.debug('line_items :' + lineitems);
        
        List<Id> responseid = new List<Id>();
        Set<String> mySet = new Set<String>();
        mySet.add(email);
        List<Insert_line_item> objPayload = (List<Insert_line_item>)JSON.deserialize(JSON.serialize(lineitems), List<Insert_line_item>.class);
        // System.debug('objPayload' + objPayload);
        Integer size = objPayload.size();
        Pricebook2 standardPrice = [ SELECT Id
                                    FROM Pricebook2
                                    WHERE isStandard = true
                                    LIMIT 1];
        System.debug('standardPriceBook' + standardPrice.Id);
        Integer counter = 0;
        List<Product2> oldproduct =[SELECT ProductCode, Id FROM Product2];        
        List<Contact> existingaccounts = [SELECT Id, FirstName, LastName, Email FROM Contact];
       
        //  Boolean flag = true;
        for (Contact cverfy : existingaccounts )
        {     
            if((email.equals(cverfy.email)))
            {
                 Integer iterate = 0;
                if(iterate < 1)
                {
                    System.debug('else:' +(!(email.equals(cverfy.email))) + cverfy.email);
                    System.debug('existing user ');
                    Contact con =[SELECT Id, AccountId, FirstName, LastName, Email FROM Contact where Email = :cverfy.email];
                    Opportunity opp = new opportunity();
                    opp.Name =(String)customerdetail.get('first_name');
                    opp.StageName = 'Prospecting';
                    opp.AccountId = con.AccountId;
                    opp.CloseDate = system.today().addDays(10);
                    opp.ContactId = con.id;
                    opp.Pricebook2Id = standardPrice.Id;
                    insert opp; 
                    for(Integer i=0;i<size;i++){
                        for(Product2 newpro : oldproduct )
                        {
                            if(newpro.ProductCode == objPayload[i].product_code)
                            {   
                                System.debug('existing product added : 67 ');
                                Product2 oldpro = [SELECT Id, ProductCode FROM Product2 where ProductCode = :newpro.ProductCode];
                                System.debug('oldpro' + oldpro);
                                PricebookEntry pentry = [SELECT Id, Product2Id, Pricebook2Id, Name FROM PricebookEntry where Product2Id = : oldpro.id ];                                
                                OpportunityLineItem oli = new OpportunityLineItem();
                                oli.Product2Id = oldpro.id;
                                oli.OpportunityId = opp.id;
                                oli.PricebookEntryId = pentry.Id;
                                oli.UnitPrice = objPayload[i].sale_price;
                                oli.Quantity = objPayload[i].quantity;
                                insert oli;
                                //  responseid.add(oli.id);    
                            } }
                        System.debug(' new product added : 80 ');
                        Product2 product = new Product2();
                        product.Name = objPayload[i].name;
                        product.productCode = objPayload[i].product_code;
                        product.isActive = true;
                        insert product;
                        
                        PricebookEntry standardPriceBookEntry = new PricebookEntry();
                        standardPriceBookEntry.Pricebook2Id = standardPrice.Id;
                        standardPriceBookEntry.Product2Id = product.Id;
                        standardPriceBookEntry.UnitPrice = objPayload[i].sale_price;
                        standardPriceBookEntry.UseStandardPrice = false;
                        standardPriceBookEntry.IsActive = true;
                        insert standardPriceBookEntry;
                        
                        OpportunityLineItem oli = new OpportunityLineItem();
                        oli.Product2Id = product.id;
                        oli.OpportunityId = opp.id;
                        oli.PricebookEntryId = standardPriceBookEntry.Id;
                        oli.UnitPrice = objPayload[i].sale_price;
                        oli.Quantity = objPayload[i].quantity;
                        insert oli;
                        responseid.add(oli.id);
                        
                    }
                }
            }
            else
                {
                    System.debug('email : '+(!(email == cverfy.email)) + cverfy.email );
                    System.debug('new contact created');
                    Account newacc = new Account();
                    newacc.name = (String)customerdetail.get('first_name') ;
                    newacc.Company_name__c  = (String)customerdetail.get('company') ;
                    insert newacc;
                    Contact newcon = new Contact();
                    newcon.AccountId=newacc.id;
                    newcon.Email  =     (String)customerdetail.get('email');
                    newcon.FirstName =  (String)customerdetail.get('first_name');
                    newcon.LastName =   (String)customerdetail.get('last_name');
                    insert newcon; 
                   // iterate++; 
                    Opportunity newopp = new Opportunity();
                    newopp.Name = (String)customerdetail.get('first_name');
                    newopp.accountid = newacc.id;
                    newopp.Pricebook2Id = standardPrice.Id;
                    newopp.CloseDate = system.today().addDays(10);
                    newopp.StageName  = 'Prospecting';
                    insert newopp;   
                    
                    for(Integer i=0;i<size;i++)
                    {
                        for(Product2 newpro : oldproduct )
                        {
                            if(newpro.ProductCode == objPayload[i].product_code)
                            {   
                                System.debug('existing product added :131' + newpro.ProductCode );
                                Product2 oldpro = [SELECT Id, ProductCode FROM Product2 where ProductCode = :objPayload[i].product_code];
                                System.debug('oldpro' + oldpro);
                                PricebookEntry pentry = [SELECT Id, Product2Id, Pricebook2Id, Name FROM PricebookEntry where Product2Id = : oldpro.id ];
                                
                                OpportunityLineItem oli = new OpportunityLineItem();
                                oli.Product2Id = oldpro.id;
                                oli.OpportunityId = newopp.id;
                                oli.PricebookEntryId = pentry.Id;
                                oli.UnitPrice = objPayload[i].sale_price;
                                oli.Quantity = objPayload[i].quantity;
                                insert oli;
                                responseid.add(oli.id);    
                            } }
                        System.debug('new product added :145');
                        Product2 product = new Product2();
                        product.Name = objPayload[i].name;
                        product.productCode = objPayload[i].product_code;
                        product.isActive = true;
                        insert product;
                        
                        PricebookEntry standardPriceBookEntry = new PricebookEntry();
                        standardPriceBookEntry.Pricebook2Id = standardPrice.Id;
                        standardPriceBookEntry.Product2Id = product.Id;
                        standardPriceBookEntry.UnitPrice = objPayload[i].sale_price;
                        standardPriceBookEntry.UseStandardPrice = false;
                        standardPriceBookEntry.IsActive = true;
                        insert standardPriceBookEntry;
                        
                        OpportunityLineItem oli = new OpportunityLineItem();
                        oli.Product2Id = product.id;
                        oli.OpportunityId = newopp.id;
                        oli.PricebookEntryId = standardPriceBookEntry.Id;
                        oli.UnitPrice = objPayload[i].sale_price;
                        oli.Quantity = objPayload[i].quantity;
                        insert oli;
                        responseid.add(oli.id);
                    }         
                }             
        }
    }
    global class ResponceHandler {
        global boolean success {get;set;}
        global String messages {get;set;}
        global object data {get;set;}
        
    } 
}