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
EldonEldon 

Creating Order and Order Products from community

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
Best Answer chosen by Eldon
Matt SmelserMatt Smelser
Eldon-
Currently it looks like Orders is not a supported object in the Napili template. http://resources.docs.salesforce.com/202/18/en-us/sfdc/pdf/community_templates.pdf and see page 5 for supported objects for Napili.

All Answers

Matt SmelserMatt Smelser
Eldon-
Currently it looks like Orders is not a supported object in the Napili template. http://resources.docs.salesforce.com/202/18/en-us/sfdc/pdf/community_templates.pdf and see page 5 for supported objects for Napili.
This was selected as the best answer
EldonEldon
Thank you for responding Matt. So now im trying to embed my lightning component in a visual force page. There are no other way to do it right?
Matt SmelserMatt Smelser
Eldon-
Sorry but those objects are not supported by Napili template. You could look at using another method to create these Orders outside of this lightning component such as process builder, but the communtiy users will not have access to these orders.
Sujata GusainSujata Gusain
Eldon-
can you please help me with the same problem, how did you solve it?