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
msglsmomsglsmo 

Custom Link to copy data from Account to Case?

Hi All,

 

I'm wanting to create a custom link that copies the shipping address and account name from the Account to the Case.  This is somewhat similar to what is done internaly on on the billing/shipping fields on the account page.

 

Has anyone done this?  Is it succesful?  Anyone willing to share some code examples or best practices?

 

Thank You!

 

Mike

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You can't create it in the same general location without the use of Visualforce, but if you're okay with just having a custom button or link, you can use that to update the case with information.

 

Have you considered maybe a formula field, instead, or do the values need to be editable afterwards? You could paste the data into the fields via the custom button/link, then allow the user to save or edit additional details, or you could use the API, update the record, then refresh the page (via the AJAX toolkit).

 

There's a handful ways you can do this, ranging from very simple (say, using a quick AJAX call), to moderately complex (e.g. loading the edit page and populating values, which is less code but more prep time), up through the most complex (Visualforce+Apex Code), with advantages to each.

 

Visualforce would create the seamless appearance (it appears as a link on the edit page, like the example you mentioned), while the other ways require an extra step to complete the process (e.g. you have to save first, then click the button/link).

 

As I said, you can also use a formula or a workflow rule if you wanted to automate without writing code, but it really depends on your use case. Once you choose a method, we can help you get where you want to go. Personally, I'd use a formula field and be done with it, but if you need editability, then you'll have to go with one of the other options.

All Answers

sfdcfoxsfdcfox

You can't create it in the same general location without the use of Visualforce, but if you're okay with just having a custom button or link, you can use that to update the case with information.

 

Have you considered maybe a formula field, instead, or do the values need to be editable afterwards? You could paste the data into the fields via the custom button/link, then allow the user to save or edit additional details, or you could use the API, update the record, then refresh the page (via the AJAX toolkit).

 

There's a handful ways you can do this, ranging from very simple (say, using a quick AJAX call), to moderately complex (e.g. loading the edit page and populating values, which is less code but more prep time), up through the most complex (Visualforce+Apex Code), with advantages to each.

 

Visualforce would create the seamless appearance (it appears as a link on the edit page, like the example you mentioned), while the other ways require an extra step to complete the process (e.g. you have to save first, then click the button/link).

 

As I said, you can also use a formula or a workflow rule if you wanted to automate without writing code, but it really depends on your use case. Once you choose a method, we can help you get where you want to go. Personally, I'd use a formula field and be done with it, but if you need editability, then you'll have to go with one of the other options.

This was selected as the best answer
Rahul SharmaRahul Sharma

Just a thought,

- Create a button on case which would copy the Account fields to Case.

- Execute onclick Javascript to update the Case.

@sfdcfox: is it possible.

msglsmomsglsmo

Forumula fields I can do, but we do periodically need to edit this field.  Sounds like I need to get with our programmers. :-)

sfdcfoxsfdcfox

Rahul,

 

That approach would certainly work. As I said, there's many ways that this can be accomplished. This is just one possible scenario:

 

{!RequireScript('/soap/ajax/23.0/connection.js')}
if('{!Case.AccountId}'=='') {
  alert('There is no account for this case.')
  return
}

var theCase = new sforce.SObject('Case')
theCase['Id'] = '{!Case.Id}'
theCase['Street__c'] = '{!JSENCODE(Account.BillingStreet)}'
theCase['City__c'] = '{!JSENCODE(Account.BillingCity)}'
theCase['State__c'] = '{!JSENCODE(Account.BillingState)}'
theCase['PostalCode__c'] = '{!JSENCODE(Account.BillingPostalCode)}'
theCase['Country__c'] = '{!JSENCODE(Account.BillingCountry)}'
try {
  sforce.connection.update([theCase])
} catch(err) {
  alert(err)
  return
}
window.top.location.href = '/{!Case.Id}'

Just remember to use JSENCODE on fields that can accept user input or might generate characters that would interfere with execution of the script.

Rahul SharmaRahul Sharma

Thanks a lot sdfcfox for the confirmation!!

msglsmomsglsmo

Thank you to everyone for the great assistance so far.

 

Everything makes sense to me and I see how it works.   Thank you for showing this to me!

 

I have tried to implement it, but I cant seem to make the button or link available while editing the case.  Is there something I am missing?

sfdcfoxsfdcfox

Yes, that was mentioned earlier in this thread. You can't place custom links/buttons at will on a system edit page. You'd have to write your own custom Visualforce page if you wanted to have this copy link available while editing a case. This is the most time-consuming and least friendly option, so I would advise you simply stick with a simple solution (e.g. the button I just posted code for) and tell the users how to use it.