1

Can someone point me to a code sample that does the following:

I want to update a custom field on all the account related contact records called relationship__c from a button on the parent account record.

I do not care if the button is a detail or list button. All it needs to do is get the account ID, search for all the child contact records and update that field to say relationship__C = "New". That's it but for someone who is not familiar with javascript, I'm hitting a wall. (I can update the account record but can't seem to get the contacts.) If anyone can help with a code example, I'd appreciate it.

1
  • 1
    Can you add the code you have so far to your question, it'll help others point you in the right direction.
    – Jenny B
    Commented Nov 7, 2014 at 21:02

2 Answers 2

1

You can invoke an apex class using the AJAX Toolkit. Here is one small example.

Create a custom button on Account and select JavaScript Execute onclick:

{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")} 
var id= '{!Account.Id}'; 
alert(id); 
sforce.apex.execute('buttoncls','updateRecord',{recId :id}); 

window.location.href = window.location.href;

Create an Apex webservive class and write your logic. Here is the code:

global class buttoncls {
    webservice static void updateRecord(Id recId){

        System.debug('------Method called on button click.....');
        list<contact> con  = new list<contact>();

        if(recId!=null)
        {
            for (Contact c :[select id ,phone from Contact where accountid=:recId])
            {
                c.phone='123';
                con.add(c);
            }

             update con;
        }
    } 
}

Add your button from page layouts...

Cheers Vineeth

1
  • HI @KarolF ,Could be please close this question marking these as best answer
    – Vineeth
    Commented Nov 10, 2014 at 15:44
0

I believe you can actually do this without resorting to the use of JavaScript. I used the following blog post by our very own Andrew Fawcett to use as a reference and create a List Button based on your example.

I have not had a chance to completely test it, but I am fairly confident this will work.

Using this information I can create the following controller:

public with sharing class CreateNewRelationshipListController
{
    private ApexPages.StandardSetController AccountSetController;

    public CreateNewRelationshipListController(ApexPages.StandardSetController inheritedSetController)
    {
        AccountSetController = inheritedSetController;
    }

    public PageReference SetContactRelationshipsToNew()
    {
        List<Account> selectedAccounts = (List<Account>)AccountSetController.getSelected();

        if(selectedAccounts.size() > 0)
        {
            Set<Id> accountIdSet = (new Map<Id, Account>(selectedAccounts)).keyset();

            List<Contact> contactsToUpdateRelationships = [SELECT Id, Relationship__c FROM Contact WHERE AccountId IN :accountIdSet];


            if(contactsToUpdateRelationships.size() > 0)
            {
                for(Contact contactToUpdateRelationship : contactsToUpdateRelationships)
                    contactToUpdateRelationship.Relationship__c = 'New';

                UPDATE contactsToUpdateRelationships;
            }
        }

        return null;
    }
} 

Next you would have to create a custom VisualForce page to specify the action once your user clicks the custom button:

<apex:page standardController="Account" extensions="CreateNewRelationshipListController"
           action="{!SetContactRelationshipsToNew}" recordSetVar="accountsToProcess">

Lastly, you would create a custom list button, choose display checkboxes (so you can select multiple Accounts), and choose the VisualForce created above as the Content Source.

There are reservations to this. Mainly this is open CSRF attacks. So you should be weary of that.

For more information on how to accomplish this, please take a look at this blog post I used as a reference for more information.

There is actually another link that the blog mentions, where you can use the same controller for either a List Button and Detail Page Button. You can find more information here, but it's not the most elegant solution (which the author admits himself).

You must log in to answer this question.

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