0

I haven't changed the script or suddenly the button throws an error that is not defined or shows up in debug logs. The basic function of the button is to grab 'Items used' from a case and create a new order. Any help is appreciated.

/* Loop through items in case and after creating the order header loop through the items to add to the order lines */
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
var idItemsArray = {!GETRECORDIDS($ObjectType.Items_Used__c)};
var orderID=null;

function CreateOrder()
{
    /*Create order based*/
    try{
        var ord = new sforce.SObject("SCRB_SalesOrder__c");
        ord.AccountID__c = "{!Case.AccountId}";
        ord.Case__c = "{!Case.Id}";
        ord.RecordTypeId = "01236000000OECr"; /* Service Record Type */
        ord.Order_Type__c = "Service";

        /* Get billing and shipping if an account is set */
        if ({!NOT(ISBLANK(Case.AccountId))}) {

            /* Set default billing address */
            billingQuery = "Select BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry FROM Account WHERE Id = '"+"{!Case.AccountId}"+"' Limit 1";
            result = sforce.connection.query(billingQuery);
            billing = result.getArray("records");

            ord.BillingStreet__c = billing[0].BillingStreet;
            ord.BillingCity__c = billing[0].BillingCity;
            ord.BillingState__c = billing[0].BillingState;
            ord.BillingPostalCode__c = billing[0].BillingPostalCode;
            ord.BillingCountry__c = billing[0].BillingCountry;

            /* Set default shipping address based on the most recent order entered */
            shippingQuery = "Select Shipping_Location__c from SCRB_SalesOrder__c where AccountID__c = "+"'{!Case.AccountId}' and Shipping_Location__c <> null order by Order_Date__c desc limit 1";
            result = sforce.connection.query(shippingQuery);
            shipping = result.getArray("records");

            if(shipping.length > 0) {
                ord.Shipping_Location__c = shipping[0].Shipping_Location__c;
            }
        }

        /* Set FS Account If OEM/VAR/Direct/Indirect */
        ibQuery = "Select Owner__c, Owner__r.Type from Installbase__c where Id = '"+ "{!Case.Install_baseId__c }"+ "' and Owner__c != null limit 1";

        result = sforce.connection.query(ibQuery);
        install_base = result.getArray("records");

        if( install_base.length > 0 ) {
            if( (install_base.length > 0 && install_base[0].Owner__c != "{!Case.AccountId}" && (install_base[0].Owner__r.Type == "OEM" || install_base[0].Owner__r.Type == "VAR" || install_base[0].Owner__r.Type == "Other" || install_base[0].Owner__r.Type == "Direct" || install_base[0].Owner__r.Type == "Indirect") ) ) {
                ord.FS_Bill_to_Account__c = install_base[0].Owner__c;
            }
        }

        result = sforce.connection.create([ord]);
        if(result[0].getBoolean("success")) {
            orderID = result[0].id;
            return 0;
        }
        else {
            alert("Problem creating order. Contact your system administrator. ERROR: "+result);
            return -1;
        }
    }
    catch(e){
        alert("Problem creating order - you may not have permissions. Contact your system administrator. ERROR: "+e+"--"+result);
        return -1;
    }
}

/* Now Loop through the Items Used and add to the order items */
function CreateOrderItems()
{
    try{
        itemUsed = sforce.connection.retrieve('Item_Name__c,Quantity__c,Serial_Lot_Number__c,Unit_Sales_Price__c','Items_Used__c',idItemsArray );
        /*Run through selected IDs and assign newly create contract*/
        for (var n = 0; n < itemUsed.length; n++) {
            var ordItem = new sforce.SObject("SCRB_SalesOrderLineItem__c");
            ordItem.OrderName__c = orderID;
            ordItem.Name = n + 1;
            ordItem.Line_Num__c = n + 1;
            ordItem.ItemID__c = itemUsed[n].Item_Name__c;
            ordItem.Quantity__c = itemUsed[n].Quantity__c;
            ordItem.Unit_SalesPrice__c = itemUsed[n].Unit_Sales_Price__c;
            ordItem.Serial_Number__c = itemUsed[n].Serial_Lot_Number__c;
            var temp_date = new Date();
            ordItem.Date_Promised__c = temp_date;
            ordItem.Description__c = "Used in service - do not ship"
            result_ordItem = sforce.connection.create([ordItem]);
            if(result[0].getBoolean("failure")) {
                alert("Problem creating order item(s). Contact your system administrator. ERROR: "+result);
                return -1;
            }
        }
        return 0;
    }
    catch(e){
        alert("Problem creating order items - you may not have permissions. Contact your system administrator. ERROR: "+e+"--"+result);
        return -1;
    }
}

/* Main Execution Section */
if (idItemsArray[0] == null) {
    alert("Please select at least one item.");
}
else {
    error = CreateOrder();
    if(error != -1) {
        error = CreateOrderItems();
        if(error != -1) {
            /* Redirect user to newly created order */
            alert("An order has been created with the items used. Please enter shipping and other other information on the next screen.");
            newURL = "{!$Site.CurrentSiteUrl}"+"/"+orderID+"/e?retURL=/"+orderID;
            window.parent.location.replace(newURL);
        }
    }
}
2
  • Welcome to Salesforce Stack Exchange (SFSE)! Please edit your question to add the exact (i.e. copied/pasted) error text, or exactly which one of your code-defined errors or alerts you see.
    – Moonpie
    Commented Jun 14, 2022 at 22:00
  • 1
    @Moonpie Summer 22 happened. 😅
    – sfdcfox
    Commented Jun 15, 2022 at 2:41

1 Answer 1

1
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}

This API version is no longer supported as of Summer 22. We have been on notice for years that this was coming. You will need to bump up the API version to at least 21.0. Note that this only kicking the can down the street; next year for Summer 23, you'll have to bump up to at least version 31.0. In addition, don't keep putting this off: your buttons will eventually stop working entirely as Classic mode is sunset in favor of Lightning. You'll want to Convert JavaScript Buttons and Links into Lightning Experience Alternatives. Make sure you do it now before you're forced to do so.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .