• Krishan Gopal 27
  • NEWBIE
  • 40 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 11
    Questions
  • 6
    Replies
@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;}
        
    } 
}
[ having this kind of error when adding PricebookEntry ]
STANDARD_PRICE_NOT_DEFINED, Before creating a custom price, create a standard price

// code
@RestResource(urlMapping = '/addLineItems')
global class Product_Line_items {
    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 {} 
    
    @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); 
        List<Object> lineitems =(List<Object>)reqObject.get('line_items');
        // System.debug('line_items :' + lineitems);
        List<Add_Lineitems_into_opportunity> objPayload = (List<Add_Lineitems_into_opportunity>)JSON.deserialize(JSON.serialize(lineitems), List<Add_Lineitems_into_opportunity>.class);
        // System.debug('objPayload' + objPayload);
        Integer size = objPayload.size();
       // Pricebook2 standardPriceBook = [SELECT Id FROM Pricebook2 WHERE isStandard = true LIMIT 1];
        Integer counter = 0;
        List<PricebookEntry> pricelist = new  List<PricebookEntry>(); 
        List<OpportunityLineItem> additems = new List<OpportunityLineItem>();   
        List<Product2> newproducts = new List<Product2>();
        List<Product2> oldproduct =[SELECT ProductCode, Id FROM Product2];
        //System.debug(' oldproduct :' +  oldproduct);
        List<Product2> prolist = new List<Product2>();
        
        for(Integer i=0;i<size;i++){
            for(Product2 newpro : oldproduct )
            {
                if(newpro.ProductCode == objPayload[i].product_code)
                {
                    throw new My1Exception('this product has been exist:' + objPayload[i].name );  
                    
                } 
            }
            Product2 product = new Product2();
            product.Name = objPayload[i].name;
            product.productCode = objPayload[i].product_code;
            product.isActive = true;
            prolist.add(product); 
            insert product;
            
            
         Opportunity op = [SELECT Id, Name from Opportunity WHERE Id='0065g00000EXBnb'];   // 0065g00000EXBnb    
         op.Pricebook2Id = '01s5g00000Qt85J';  //01s5g00000Qt85J
          update op;
            
            PricebookEntry standardPrice = new PricebookEntry();
            standardPrice.Pricebook2Id = '01s5g00000Qt85J';
            standardPrice.Product2Id = product.id;
            standardPrice.UnitPrice = objPayload[i].sale_price;
            standardPrice.IsActive = false;
            standardPrice.UseStandardPrice= false;
             System.debug('pricelist' + pricelist);            
            insert standardPrice;
            System.debug('standardPrice' + standardPrice.id);            
         
            
            /* OpportunityLineItem oli = new OpportunityLineItem();
            oli.Product2Id = product.id;
            oli.OpportunityId = op.id;
            oli.PricebookEntryId = standardPrice.Id;
            oli.UnitPrice = objPayload[i].sale_price;
            oli.Quantity = objPayload[i].quantity;
            //oli.TotalPrice = objPayload[i].sale_price * objPayload[i].quantity; 
            insert oli;
            additems.add(oli);
System.debug('oli' + oli);*/
        }
       // insert pricelist;
       // insert additems;
      //  object lineitemsres = additems;
       // objResponse.data = lineitemsres;
    }
    global class ResponceHandler {
        global boolean success {get;set;}
        global String messages {get;set;}
        global object data {get;set;}
        
    } 
}
error generate at the time of paymentIf anyone help me out ??
tx for time and trouble
@RestResource(urlMapping = '/addLineItems')
global class Product_Line_items {
    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 {} 
    
    @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); 
        List<Object> lineitems =(List<Object>)reqObject.get('line_items');
        // System.debug('line_items :' + lineitems);
        List<Add_Lineitems_into_opportunity> objPayload = (List<Add_Lineitems_into_opportunity>)JSON.deserialize(JSON.serialize(lineitems), List<Add_Lineitems_into_opportunity>.class);
        // System.debug('objPayload' + objPayload);
        Integer size = objPayload.size();
        Opportunity op = [SELECT Id, Name from Opportunity WHERE Id='0065g00000EXBnb'];        
        op.Pricebook2Id = '01s5g00000LI8S8';  
        Integer counter = 0;
        
        
        List<PricebookEntry> pricelist = new  List<PricebookEntry>(); 
        
        List<OpportunityLineItem> additems = new List<OpportunityLineItem>();   
        List<Product2> newproducts = new List<Product2>();
        
        Map<Id,Product2> oldproduct =new Map<Id,Product2>([SELECT ProductCode, Id FROM Product2]);
        //System.debug(' oldproduct :' +  oldproduct);
        Set<id> productids =  (new Map<Id,Product2>(oldproduct)).keySet();
        
        for(Integer i=0; i<size; i++){
            for (Id keyid : oldproduct.keyset()) { 
                Product2 newproduct  =oldproduct.get(keyid) ;
                if( objPayload[i].product_code   newproduct.ProductCode){ 
                    System.debug('if');
                    product2 p = new product2();
                    p.Name = objPayload[i].name;
                    p.productCode = objPayload[i].product_code;
                    p.isActive = true;
                    newproducts.add(p);
                    System.debug(' newproduct :' +  p.id);
                    System.debug(' newproductsize :' +  newproducts.size());   
                
                }else
                {
                     System.debug('else' + newproduct.ProductCode );
                    throw new My1Exception('this product has been exist:' + objPayload[i].name );  
                }  
            } 
            }
        
        
    }
            // insert newproducts;
            // insert pricelist;
            global class ResponceHandler {
                global boolean success {get;set;}
                global String messages {get;set;}
                global object data {get;set;}
                
            } 
        }
How can i merge Json  opportunity Sobject  in one?


"((Contact:{Id=0035g00000ABOJmAAP, FirstName=jhg, LastName=Milapdeep Singh}, Contact:{Id=0035g00000FKkR4AAL, FirstName=jatin, LastName=kaushal}), 

(Opportunity:{Id=0065g000008xnpoAAA, Name=dummy, CloseDate=2021-09-16 00:00:00, IsWon=true}, Opportunity:{Id=0065g000008xqWVAAY, Name=testing, CloseDate=2021-09-17 00:00:00, IsWon=true}))"
trying to insert   7 days older apportunity into accounts( not geeting the apex logic)  insert in account : kd

// apex
public class AccountHandler {
 public static void last7days(List <Account> oldList)
    { 
        List<Opportunity> OpportunityList = new List<Opportunity>();
        Map<Id, Opportunity> opptyMap = new Map<Id, Opportunity>([select id, stageName, AccountId, createdDate from Opportunity where AccountId IN: oldList]);
        DateTime Opportunity7Days = system.now()-7;
        for(Opportunity opp: opptyMap.values()){
            if(opp.stageName =='closed won' && opp.createdDate < Opportunity7Days ){
                OpportunityList.add(opp);
                if(OpportunityList != null )
                {
                    insert OpportunityList; 
                }
            }
            
        }  
    }

}


// trigger 

trigger closedWon on Account (after insert, After update) {
    AccountHandler.last7days(Trigger.old);
}

User-added image
getting an error :
System.AssertException: Assertion Failed: Expected: false, Actual: trueUser-added imageUser-added image
After inserting a new candidate record, candidate may insert wrong date in the field “Application Date”. Write a trigger so that every time a new candidate record is created in a system, an “Application Date” field would have a value as system generated CreatedDate field of that record only and not using any Date functions.


User-added image
whats problem with logic and how can i implement without predefined function 
thank you!
facing trouble to one object to to another in trigger to solve that query
 
  • If Candidate is applying for a job whose status is Not Active(deactivate job). He should get an error, “This Job is not active. You can not apply for this job.

in this queston candidate object have a lookup field to select job  and according to job object  field active status . candidate shold able to select .

how to write query to check from old job data to a new candidate User-added imageUser-added image
After inserting a new candidate record, candidate may insert wrong date in the field “Application Date”. Write a trigger so that every time a new candidate record is created in a system, an “Application Date” field would have a value as system generated CreatedDate field of that record only and not using any Date functions.
User-added image
               can't understand typecasting 
               thank you!
getting an error :
System.AssertException: Assertion Failed: Expected: false, Actual: trueUser-added imageUser-added image
After inserting a new candidate record, candidate may insert wrong date in the field “Application Date”. Write a trigger so that every time a new candidate record is created in a system, an “Application Date” field would have a value as system generated CreatedDate field of that record only and not using any Date functions.
User-added image
               can't understand typecasting 
               thank you!