Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
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
4 answers
Loading
0/9000