• Sujata Gusain
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi,

Im trying to create an Order and related order products in apex using lightning.
So basically i want to call a server method that creates order and order products when i click a checkout button.
I am able to do it in my lightning app. But when i put my component in a community(Napili template), and login as a community user , order and order products are not getting created when i call the same server method and my community page is getting refreshed instead. 
the server method is as follows,


@auraenabled
    public static string CheckOut(){
        
       
        String userID;
        userID = UserInfo.getUserId();
        list<user> u=[select id,Name from user where id=:userID];
        string nam= u[0].Name +'-Automated';
        list<Opportunity> Oppid=[select id from opportunity where name=:nam];
        system.debug('Oppid'+Oppid[0].id);
        string OID;
        OID=Oppid[0].id;
        
        list<OpportunityLineItem> OppListItems=[select OpportunityId,Quantity,UnitPrice,PriceBookEntryId,Product2Id,InMyCart__c,Wishlisted__c 
                                         from OpportunityLineItem where InMyCart__c=:true AND OpportunityId=:OID];
             
        if(OppListItems.size()>0){
            Order NewOrder = new Order(
               
                AccountId='0012800000ZKzhH',
                Status='Draft',
                EffectiveDate=System.today(),
               
                Pricebook2Id ='01s28000006q8A8',
                OpportunityId=Oppid[0].id
                
            );
            
            insert NewOrder;
             
            list <OrderItem> OrderitemList=new list<OrderItem>();
            
            For(integer i=0;i<OppListItems.size();i++){
                
                if(OppListItems[i].InMyCart__c==true){
                    OppListItems[i].InMyCart__c=false;
                    OrderItem OI=new OrderItem(
                        OrderId=NewOrder.Id,
                        Quantity=OppListItems[i].Quantity,
                        
                        PriceBookEntryId=OppListItems[i].PriceBookEntryId,
                        UnitPrice=OppListItems[i].UnitPrice
                        // Product2Id=OppLI[i].Product2Id
                        
                    );
                    OrderitemList.add(OI);
                }
            }
            system.debug('OrderitemList'+OrderitemList);
            insert OrderitemList;
            system.debug('OppLI'+OppListItems);
            update OppListItems;
            
            list<OpportunityLineItem> OppLIToDelete=[select id,name from OpportunityLineItem where InMyCart__c=:false AND WishListed__c=:false];
            delete OppLIToDelete;
            
            
        }   
        return OID;
    }



OppLIToDelete  is getting deleted . But order and order products are not getting created. Any help would be appreciated.

Thankyou
  • August 18, 2016
  • Like
  • 0