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
JuanTalJuanTal 

I can't update a custom field with a custom button

I have this but doesn't work

any ideas ?

 

 

{!REQUIRESCRIPT("/soap/ajax/8.0/connection.js")};

 

 

 

var caseObj = new sforce.SObject("Factura__c"); 
caseObj.Name = "{!Factura__c.Name}"; 
caseObj.Detalle__c = "005A0000001Sb6m";
var result = sforce.connection.update([caseObj]); 
window.location.href=window.location.href;
b-Forceb-Force

There are two issue,

While updating you are not specifying recordId,

Anather is code without result handling [As per best practice]

 

here is updated code for you

 

{!REQUIRESCRIPT("/soap/ajax/8.0/connection.js")};
try
{
var caseObj = new sforce.SObject("Factura__c");
caseObj.Id="YOUR_RECORD_ID";
caseObj.Name = "{!Factura__c.Name}";
caseObj.Detalle__c = "005A0000001Sb6m";
var result = sforce.connection.update([caseObj]);
if (result[0].success=='false')
{
alert(result[0].errors.message);
}
else
{
location.reload(true);
}
}catch(err)
{
alert(err);
}

 

 Please update your code accordingly,

Let us know if you face any more issue, If it works for you mark as accepted

 

Thanks,

Bala

 

JuanTalJuanTal

 

thanks for that but I had found the solution ...
I have another problem, I know how to update fields, but now I want to insert newfields.

Could you help me with that?

i have this
     
      
        {!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")};
        var movPresupuesto = new sforce.SObject("Movimiento_Presupuestal__c");
        var movPreQuery = "SELECT id,detalle__c FROM factura__c WHERE name ='" + idFactura + "'"; 
        var result = sforce.connection.query(movPreQuery);
        var resultpre = sforce.connection.query("SELECT id FROM presupuesto__c WHERE name ='"+cuenta+"'");
       if(result.size = 1){
           var total = '{!Factura__c.Total__c}';
           total = total.replace(/,/,"");
           movPresupuesto.Id = 'a0CA0000007BCAfMAO';
           movPresupuesto.Cargo__c = parseFloat(total.substr(1, total.length));
           movPresupuesto.Cuenta__c = '{!Factura__c.Cuenta__c}';
           var resultadoInsert = sforce.connection.create([movPresupuesto]);
           alert(resultadoInsert);
           
       }
the button is in the Factura__c custom object and i wanna insert new record in the Movimiento_Presupuestal__c custom object

this is the error for the insert message (create) :

{errors:{message:''a0CA0000007BCAf' is not a valid Salesforce ID for the type Movimiento Presupuestal', statusCode:'INVALID_ID_FIELD', }, id:null, success:'false', }

 

b-Forceb-Force

If you are creating new record of object Movimiento_Presupuestal__c

 

then you should not write like movPresupuesto.Id = 'a0CA0000007BCAfMAO';

remove this line,

 

We cant specify id while inserting new record

 

It will work,

 

One kind suggestion, handle insert result for error, surround code with try catch

 

Cheers,

Bala