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
SFDCIronManSFDCIronMan 

Create Button on opportunity name 'Sync Order' ,Create new class just to sync that particular order.

help me create apex class from this Batch Class 
this Batch Class is for handling bulk record I want to create this code for handling single record with Apex Class With Opportunity ID


/* Batch class to Synch Opportunies to AccountRight
 * Makes API calls to accountRight
 * 1. Checks if Opportunity Account exists in AccountRight
 * 2. If not creates an Account(Card Type) inAccount right.
 * 3. Create Sales Order in Account Right.
 * 4. Update Account and Oppotunity with error/success messages.
 * @changes: 2018-10-06 - CI - Add error handling in accountMap update
 */ 

global class SynchOrderToAccountRightBatch implements Database.Batchable < sObject >, Database.Stateful, Database.AllowsCallouts {
    
    //collections to work with
    Map<id, OpportunityLineItem> oppLineMap = new Map<id, OpportunityLineItem>();
    //Map<id, Opportunity> oppMap = new Map<id, Opportunity>();
    Map<id, Account> accountMap = new Map<id, Account>();
    List<User> salesUser = new List<User>();
    String gstTaxCode;
    
    Map<String,MyobRegionUID__c> regionUIDMap;
    
    //Lineitems does not work when batch runs, so have to query line items in execute method
    String sqlStr = 'SELECT id, Ownerid , CloseDate, AccountId, Freight_exc__c, Account.Name, Owner.Name,owner.MYOB_Employee_UID__c, Opportunity_Number__c, Purchase_Order__c, MYOB_Order_Number__c, Region__c, AFQ_Company__c, StageName, owner.firstname,owner.lastname ' + 
                                        //', (SELECT Quantity, Discount, UnitPrice, Description, TotalPrice, PricebookEntry.Name, PricebookEntry.Product2.Name, PricebookEntry.Product2.Family, PricebookEntry.Product2.ProductCode FROM OpportunityLineItems) '+
                                        ',  (select id,External_Comments__c,IsSyncing from Quotes ) ' +                                                     
                                        'from Opportunity where MYOB_Order_Number__c = Null and AFQ_Company__c = \'Australia\' and MYOB_Synch_Status__c != \'Success\' and closedate >= 2017-05-01 and StageName = \'Closed Won\'';
    //we are picking opportunities closed from 1st May 2017
    // Start Method
    global Database.QueryLocator start(Database.BatchableContext BC) 
    {
        AccountRightAPI apiAccRight = new AccountRightAPI();
        gstTaxCode = apiAccRight.taxCode;
        regionUIDMap = apiAccRight.regionUIDMap;
        return Database.getQueryLocator(sqlStr);
        //return null;
    }

    // Execute Logic
    global void execute(Database.BatchableContext BC, List <SObject> scope) {
        //Contrainer for opportunities to work with
        Map<id, Opportunity> oppMap = new Map<id, Opportunity>();
        system.debug('entry point product execute method ' + scope);
        AccountRightAPI apiAccRight = new AccountRightAPI();
        // exit if there are no records returns from API return
        if(scope == null)
            return;
        List<Opportunity> oppList = (List<Opportunity>)scope;
        Set<id> accntId = new Set<id>();
        //loop through the scope and prepare data to work with
        //Consider Opportunity which are closed won, AFQ is Australia and Myob order is NULL
        for(Opportunity thisOpp : oppList){
            if(thisOpp.StageName.equalsIgnoreCase('Closed Won') 
                && thisOpp.AFQ_Company__c.equalsIgnoreCase('Australia')
                && thisOpp.MYOB_Order_Number__c == NULL){
                 oppMap.put(thisOpp.Id, thisOpp); 
                 system.debug('thisOpp ' +thisOpp);   
                 system.debug('thisOpp ' +thisOpp.OpportunityLineitems);
                 accntId.add(thisOpp.AccountId);   
            }
        }
        // get opportunity line items
        for(OpportunityLineItem thisLine :[Select OpportunityId, ProductCode, PricebookEntry.Product2.MYOB_Product_UID__c,
                                             Product2.Name, Quantity, UnitPrice, 
                                             Description, Discount 
                                             from OpportunityLineItem 
                                             where OpportunityId in:oppMap.keySet()]) {
            oppLineMap.put(thisLine.Id, thisLine);
        }

        
        //get the accounts associated with Opportunities, 
        //we need to verify if these accounts exist in AccountRight
        //If does not exist in AccountRight - Create and in AccountRight and update in Salesforce
       // Map<Id, Account> accListToCreatedInMyob = new Map<Id, Account> ();
        for(Account thisAcc : [Select Id, Name, MYOB_Card_ID__c, MYOB_Account__c, BillingCity, BillingStreet, Invoice_Email__c,
                                    BillingState, BillingPostalCode, BillingCountry, Phone, Fax, Company_Email__c, 
                                    ShippingStreet, ShippingState, ShippingPostalCode, ShippingCountry, ShippingCity,MYOB_Account_UID__c, 
                                    Parent.MYOB_Account_UID__c,ParentId, Parent.MYOB_Card_ID__c, Parent.Name, Parent.BillingCity, Parent.MYOB_Account__c, 
                                    Parent.BillingStreet, Parent.BillingState, Parent.BillingPostalCode, Parent.BillingCountry, 
                                    Parent.Phone, Parent.Fax, Parent.Invoice_Email__c, Parent.ShippingStreet,Parent.ShippingState, 
                                    Parent.ShippingPostalCode, shippingAddress, billingAddress, Parent.shippingAddress, Parent.billingAddress, 
                                    Parent.ShippingCountry, Parent.ShippingCity 
                                    from Account
                                    where Id IN: accntId]) {
            accountMap.put(thisAcc.Id, thisAcc);
        }
        
        system.debug('accountMap  ' + accountMap);
        try{
            //loop through opps and synch to Myob one at a time
            for(Opportunity thisOpp : oppMap.values()){
                try{
                    synchOppToMyob(thisOpp, accountMap.get(thisOpp.AccountId),apiAccRight);
                }catch(Exception exp){
                    system.debug('in ctach  block exception ' + exp.getMessage());
                    thisOpp.Myob_Synch_Status__c = 'Failed'; thisOpp.Myob_Synch_Message__c = exp.getMessage();
                    continue;
                }
            }      

            //create Invoices for the synched Orders/opportunitie
            List<Invoice__c> invList = new List<Invoice__c>();
            for(Opportunity thisOpp : oppMap.values()){
                if(thisOpp.MYOB_Synch_Status__c.equalsIgnorecase('Success')){
                    //make api call to get order values to be populated to invoice
                    AccountRightSaleAPI api = new AccountRightSaleAPI('/Sale/Order');
                    List<AccountRightAPI.SaleOrder> orderRes = api.find(new AccountRightAPI.FilterOperator('Number', 'eq', thisOpp.Opportunity_Number__c));
                    AccountRightAPI.SaleOrder thisAcOrder = orderRes.get(0);//there should be only one order in Account right
                    Invoice__c inv = new Invoice__c();
                    inv.Name = thisOpp.Opportunity_Number__c; inv.Opportunity__c = thisOpp.Id; inv.Account__c = thisOpp.AccountId; inv.Subtotal__c = thisAcOrder.Subtotal;
                    inv.Freight__c = thisAcOrder.Freight; inv.Total_Amount__c = thisAcOrder.TotalAmount; inv.Date__c = getDate(thisAcOrder.DateString); inv.Status__c = 'Order'; inv.Balance_Due__c = thisAcOrder.BalanceDueAmount;
                    invList.add(inv);
                }
            }
            
            //update MYOB flag for the created account
            if(oppMap.size() > 0){
                update oppMap.values();
            }

            List<Account> failedAccountUpdatesList = new List<Account>();
            if(accountMap.size() > 0){
                List<Database.SaveResult> results = Database.update(accountMap.values(), false);
                for (Database.SaveResult sr : results) {
                    if (!sr.isSuccess()) {
                        for(Database.Error err : sr.getErrors()) {
                            Account newAcc = new Account();
                            newAcc.Id = sr.getId(); newAcc.MYOB_Sync_Message__c = err.getMessage();
                            failedAccountUpdatesList.add(newAcc);
                        }
                    }
                }
            }

            if(!failedAccountUpdatesList.isEmpty()){
                update failedAccountUpdatesList;
            }

            //insert invoices
            if(invList.size() > 0)
                insert invList;
            
            //update user
            if(salesUser.size() > 0)
                update salesUser;
        }catch(Exception exp){
            system.debug('exception while inserting records ' + exp.getMessage());
        }
    }