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
Vanessa BarrosVanessa Barros 

javascript button

hi! can you help me?

i have a javascript button that i made to modify the owner id if the Business_unit__c field is not null.

 

Doesnt work.. :(

 

this is the code

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

var records = {!GETRECORDIDS($ObjectType.Case)};
var newRecords = [];

for (var n=0; n<records.length; n++) {
var c = new sforce.SObject("Case");

c.id = records[n];

var efdata = sforce.connection.query("Select  Business_unit__c From case where id = " + "'" +c.id + "'" );


if(efdata == null){
alert("O caso está incompleto! Dados em falta: Business Unit");

}else{
var efdata2 = sforce.connection.query("Select  User_logged__c From case where id = " + "'" +c.id + "'" );
for (var i=0; i<efdata2.length; i++) {
c.ownerId=efdata2[i];
newRecords.push(c);



}
result = sforce.connection.update(newRecords);

window.location.reload();
}


}

joshbirkjoshbirk

Do you need to still set the session ID/URL for access? 

Vanessa BarrosVanessa Barros

tks joshbirk :) i had solved today !many tks

RagzRagz

I was going throug your post and reworked (reorganized) on the code and then realized you solved the issue(which is very good). Thought I would still post some of my observations (just in case if this helps)

 

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

//get those Ids for updates
var idsToUpdate = {!GETRECORDIDS($ObjectType.Case)};
var newRecords = [];
// form the query
var query = "Select  "Business_unit__c From case where id in (";

for (var n in idsToUpdate) {
	query = query + "'"+idsToUpdate[n]+"'";
	if (n!=idsToUpdate.length-1) {
		query = query + ",";
	}
}
query = query + ")";

// execute the query

var queryResults = sforce.connection.query(query);
// get records Array

var busUnitDetails = queryResults.getArray('records');

// initialize cases array 
var updatedCases = [];

// open the iterator (best practice)
var busUnitDetIter = new sforce.QueryResultIterator(queryResults);

// start your business logic for updates

while (busUnitDetIter.hasNext())
{
	var caseUpdate = new sforce.SObject("Case");
	var busUnitObj = busUnitDetIter.next();
	var efdata2 = sforce.connection.query("Select  User_logged__c From case where id = " + "'" +c.id + "'" ); 
	caseUpdate.Id = busUnitObj.Id;
	caseUpdate.ownerId = ;
	updatedCases.push(caseUpdate);
	
}
if (updatedCases.length > 0) {
	sforce.connection.update(updatedCases);
	parent.window.location.reload();
}

 

Vanessa BarrosVanessa Barros

many tks :)

always helps - learning other ways to thing :)