+ Start a Discussion
Sarah BurtonSarah Burton 

JavaScript button not working

Hi There

I am trying to create a button which will sit in the Opportunity related list which allows you to update the field 'POP Request' on all the product lines you use in the Multi Picklist. I am getting the following message: 

Error Message: <span class="errorStyle">Error: The Name field is required.</span>

This is the first time I have tried this so any help is much appreciated! 

Thanks, Sarah 


{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
var p = new sforce.SObject('OpportunityLineItem');
p.id = "{OpportunityLineItem.Id}";
p.POP_Request__c = TRUE;
result = sforce.connection.update([p]);
location.reload(true);

User-added image

 
Ryan GreeneRyan Greene
Your code looks right, although I don't do much Javascript writing anymore.
However your ajax version is WAY outdated. Use version 43, that's the most recent.
{!REQUIRESCRIPT("/soap/ajax/43.0/connection.js")}
Also, javascript in Salesforce seems to be getting slowly phased out. You may want to look into other options for what you are trying to accomplish.
Good luck!
Ryan GreeneRyan Greene
I did notice a few things in the Javascript. If I remember correctly it is very finickey about qoutes and single quotes. Try this instead:
var p = new sforce.SObject("OpportunityLineItem");
p.id = '{!OpportunityLineItem.Id}';

Also needed that ! before the OppLineItem
Sarah BurtonSarah Burton
Hi Ryan, thank you so much for your assistance, it's really appreciated. 
I tried the following an didnt work unfortuantely. This is all totally new to me so I am winging it if I'm honest! 

No problem about Java being phased out... this is a quick fix until the team get on lightning :) 

{!REQUIRESCRIPT("/soap/ajax/43.0/connection.js")}
var p = new sforce.SObject("OpportunityLineItem");
p.id = '{!OpportunityLineItem.Id}';
p.Pop_Request_Picklist__c = "Yes";
result = sforce.connection.update([p]);
location.reload(true);

User-added image
Shruti SShruti S
Could you give this a shot - 
{!REQUIRESCRIPT("/soap/ajax/43.0/connection.js")}

var oli = new sforce.SObject( "OpportunityLineItem" );
oli.Id = "{!OpportunityLineItem.Id}";
oli.Pop_Request_Picklist__c = "Yes";

var result = sforce.connection.update( [ oli ] );

if( result[0].getBoolean( "success" ) ) {
    location.reload( true );
}
else {
    alert( "Failed to Update! Error: " + result[0] );
}
Sarah BurtonSarah Burton
Thank you Shruti for you help! 

When I clicked the button it said: 
Failed to Update! Error: {errors:{message:'Id not specified in an update call', statusCode:'MISSING_ARGUMENT', }, id:null, success:'false', }
Shruti SShruti S
Could you please tell me on which object have you created this button?
Sarah BurtonSarah Burton
Hi Shruti - It is on the Opportunity Product 
Have I gone about this in the wrong way?

User-added image
 
Shruti SShruti S
Please try the below code : 
{!REQUIRESCRIPT("/soap/ajax/43.0/connection.js")}

//Returns an Array of Salesforce IDs
//[ 'XXXXXXXXXXXXXXXXXX', 'YYYYYYYYYYYYYYYYYY' ..... ]
var selectedRecordIds = {!GETRECORDIDS($ObjectType.OpportunityLineItem)};

if( selectedRecordIds.length > 0 ) {
    var olis = [];

    selectedRecordIds.forEach(
        function( selRecordId ) {
            var oli = new sforce.SObject( "OpportunityLineItem" );

            oli.Id = selRecordId;
            oli.Pop_Request_Picklist__c = "Yes";

            olis.push( oli );
        }
    );  

    var result = sforce.connection.update( olis );

    if( result[0].getBoolean( "success" ) ) {
        location.reload( true );
    }
    else {
        alert( "Failed to Update! Error: " + result[0] );
    }
}
Ryan GreeneRyan Greene
Pull out "var result = " or "result = " from your original post Sarah.

Just leave it as "sforce.connection.update([p]);" OR [oli] which ever variable youre using for OppLineItem

I have a similar Javascript button I no longer use and it just had "sforce.connection.update([L]);" to update my Leads
Ryan GreeneRyan Greene
You can also try window.location.reload();