+ Start a Discussion
MattyDHLMattyDHL 

Trigger - Copying Field from Account Object to Custom Object

Hi,

 

I have managed to create one trigger in my time with Salesforce which works perfectly but am working on another and am really stuck. Myprevious trigger was for validation, so im lost with this one.

 

I need field data to be passed from the account page to a field custom object. 

The Custom Objects Name is the AccountID. I have put the data that needs passing into a map, how do I then take it out of the map and place it into the field?

I need the field data from SAP_Number__c in account be placed into a field of the same name in the custom object AccountSetupForm__c.

 

Here is what I have so far, any help would be appriciated.

 

 

trigger UpdateToAccSetupForm on Account (after insert, after update) {
    Map<String, String> SetupFormMap = new Map<String, String>();
    for (Account acc : System.Trigger.new){
        if (acc.IF_SAP__c != null){   
            SetupFormMap.put(acc.ID, acc.IF_SAP__c);
        }
        else{
           
        }
        for (Account aacc :[Select AccountName__c, SAP_Number__c from AccountSetupForm__c where
        AccountName__c IN : SetupFormMap.keySet()] ){
           
           
        }
       
    }

 

Thanks

 

Matt

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

Some code to get you started below. However, I see a potential flaw in the trigger - if you are running this after insert of an account, does the matching record in custom object exist at this point? Is there a before insert that creates the record? If not, you would need to add one, or amend this trigger to insert a record if it doesn't find a match in the map.

 

So without knowing much about the custom object, the code would look something like this:

 

 


trigger UpdateToAccSetupForm on Account (after insert, after update) {
    Map<String, String> SetupFormMap = new Map<String, String>();
    for (Account acc : System.Trigger.new){
        if (acc.IF_SAP__c != null){   
            SetupFormMap.put(acc.ID, acc.IF_SAP__c);
        }
 

//Since you are using a for loop, you need to create a list to update when the loop is complete:
list<AccountSetupForm__c > recordsforupdates = new List <AccountSetupForm__c >();
//Your loop looked wrong since you are looping throught the custom object:
For (AccountSetupForm__c aacc :[Select Id, AccountName__c, SAP_Number__c from AccountSetupForm__c where
AccountName__c IN : SetupFormMap.keySet()] ){

//For each row returned by the loop, we should check that the map contains a match
if (SetupFormMap.containsKey(aacc.AccountName__c)
{
//if it does, we retrieve the value in the map by looking up the key:
String actName = SetupFormMap.get(AccountName__c);
//Now add the current record in the loop to the recordsforupdates list, setting the SAP_Number__c to the actname string - you can eventually do that directly in the add to list statement, but it's easier to understand if you see this extra step
recordsforupdates.add (new recordsforupdates (Id = aacc.Id, SAP_Number__c = actName);
}

}
update recordsforupdates ;

}

 

 

 

 

All Answers

BritishBoyinDCBritishBoyinDC

Some code to get you started below. However, I see a potential flaw in the trigger - if you are running this after insert of an account, does the matching record in custom object exist at this point? Is there a before insert that creates the record? If not, you would need to add one, or amend this trigger to insert a record if it doesn't find a match in the map.

 

So without knowing much about the custom object, the code would look something like this:

 

 


trigger UpdateToAccSetupForm on Account (after insert, after update) {
    Map<String, String> SetupFormMap = new Map<String, String>();
    for (Account acc : System.Trigger.new){
        if (acc.IF_SAP__c != null){   
            SetupFormMap.put(acc.ID, acc.IF_SAP__c);
        }
 

//Since you are using a for loop, you need to create a list to update when the loop is complete:
list<AccountSetupForm__c > recordsforupdates = new List <AccountSetupForm__c >();
//Your loop looked wrong since you are looping throught the custom object:
For (AccountSetupForm__c aacc :[Select Id, AccountName__c, SAP_Number__c from AccountSetupForm__c where
AccountName__c IN : SetupFormMap.keySet()] ){

//For each row returned by the loop, we should check that the map contains a match
if (SetupFormMap.containsKey(aacc.AccountName__c)
{
//if it does, we retrieve the value in the map by looking up the key:
String actName = SetupFormMap.get(AccountName__c);
//Now add the current record in the loop to the recordsforupdates list, setting the SAP_Number__c to the actname string - you can eventually do that directly in the add to list statement, but it's easier to understand if you see this extra step
recordsforupdates.add (new recordsforupdates (Id = aacc.Id, SAP_Number__c = actName);
}

}
update recordsforupdates ;

}

 

 

 

 

This was selected as the best answer
MattyDHLMattyDHL

Hi,

 

Thanks for the help, this code is extremely useful, and the theory behind it has improved my trigger thinking no end. I presumed that the custom object would always be there as the business rules say it should (in theory) but i should not presume that.

 

 One question though, you say I could add the SAP_Number directly? is it better to do this, and how would this be achieved so I can compare the code.


Again thanks, extremely useful information.

BritishBoyinDCBritishBoyinDC

Understanding Maps was certainly key for me to able to work within limits, so happy to help other understand them better.

 

Re the code, you wouldn't need to assign the Sap_Number to string  variable, you could just do it in the add statement:

 

recordsforupdates.add (new recordsforupdates (Id = aacc.Id, SAP_Number__c = SetupFormMap.get(AccountName__c))); 

MattyDHLMattyDHL

Hi,

 

OK, so i've had a little play with the code and I am having a few problems. I have spotted the errors though, so thats a good sign, its just I don't understand how to fix it. I have a few suggestions, so maybe you could go through them and point us in the right directions.

 

Firstly, I changed this (I think this is correct now)

//if it does, we retrieve the value in the map by looking up the key:
String actName = SetupFormMap.get(aacc.AccountName__c);

 

 Secondly from what I understand a list can only have one field/object, but we try to add two fields to this list. So it wont be allowed.

 

//Since you are using a for loop, you need to create a list to update when the loop is complete:
list<AccountSetupForm__c > recordsforupdates = new List <AccountSetupForm__c >();
//Your loop looked wrong since you are looping throught the custom object:
For (AccountSetupForm__c aacc :[Select Id, AccountName__c, SAP_Number__c from AccountSetupForm__c where
AccountName__c IN : SetupFormMap.keySet()] ){

//For each row returned by the loop, we should check that the map contains a match
if (SetupFormMap.containsKey(aacc.AccountName__c)
{
//if it does, we retrieve the value in the map by looking up the key:
String actName = SetupFormMap.get(AccountName__c);
//Now add the current record in the loop to the recordsforupdates list, setting the SAP_Number__c to the actname string - you can eventually do that directly in the add to list statement, but it's easier to understand if you see this extra step
recordsforupdates.add (new recordsforupdates (Id = aacc.Id, SAP_Number__c = actName);
}

}
update recordsforupdates ;

}

 

My solutoin ideas.

1) Add the new field to the object before adding to the list - I think this is the best solution, but don't know how to impliment it.

2.) Change the list to a Map, but then how would i be able to update the Map to the server?

 

So I have attempted solution 1)

 

 

List<AccountSetupForm__c> recordsforupdates = new list<AccountSetupForm__c>();

for (AccountSetupForm__c aacc :[Select Id, AccountName__c, SAP_Number__c from AccountSetupForm__c where
AccountName__c IN : SetupFormMap.keySet()] ){

if (SetupFormMap.containsKey(aacc.AccountName__c)){

aacc.SAP_Number__c = SetupFormMap.get(acc.IF_SAP__c);
recordsforupdates.add(new recordsforupdates(aacc));


}

}
update recordsforupdates;

 

 

 Here I get the error:

 

recordsforupdates.add(new recordsforupdates(aacc));- Save error: Invalid type: recordforupdates

 

I've also tried:

 

 

if (SetupFormMap.containsKey(aacc.AccountName__c)){

aacc.SAP_Number__c = SetupFormMap.get(acc.IF_SAP__c);
recordsforupdates.add(aacc);

 

and got the error

 

   aacc.SAP_Number__c = SetupFormMap.get(acc.IF_SAP__c); - Save error - Variable does not exist: acc.IF_SAP__c

 

 

 

 

I thought I got a good understanding of lists but this threw it out of the window :(

 

Matt

 

 

 

 

Message Edited by MattyDHL on 10-15-2009 08:09 AM
BritishBoyinDCBritishBoyinDC

The first fix looks right...

 

Not sure I understand the second point though...when a list is defined as an sObject List, it is effectively an array of the fields for that sObject, where each item in the list is an instance of the sObject in question  - in your case AccountSetupForm__c.

 

So a simple example might be:

Account a = new Account(Name='Test2', Site='Arlington');

insert a;

List<Contact> c = new List<Contact> ();

c.add(new Contact(lastname='test C 1', firstname='test f', accountid = a.id));

c.add(new Contact(lastname='test C 2', firstname='test f2', accountid = a.id));

insert c;

 

List<Contact> updates = new List<Contact> ();

  

for (contact uc: c) { 

updates.add(new Contact(Id = uc.Id, Lastname = 'test C 1' + 'a'));

update updates;

 

So when we add records to that updates list, we can set each field in that instance of the Object to a value - so as you loop through the list of AccountSetupForm__c records, you can add them to the new container list recordsforupdates, and in doing so set the new values based on the data you extract from the map. In the example above (and in your code) by setting the Id field when we add the records to the recordsforupdates list, we are determining that we will only be able to update these records (since you can't set an Id when you update), but when you execute  'update recordsforupdate' SF is updating these records, using the Id the unique identifier to find that existing record in SF, and then updating that matching record to the values you set in the code.    

 

Not sure if that makes if any clearer, but  hopefully you'll see that you can just set all the fields for a record when you add it to the list, and then execute a singel updates for all the records you have added

MattyDHLMattyDHL

EDIT - It didn't work as I wanted :(

http://community.salesforce.com/sforce/board/message?board.id=apex&thread.id=21616

--------------------------------------------------------------------------------------------------------------------------

 

Thanks for your help again.

 

I have managed to get the trigger to work, tested on the Sandbox and it worked fine.

 

Changed it a little, but looking good. Thanks for your help.

 


trigger UpdateToAccSetupForm on Account (after update) {
//Map to store SAP number - Map key is AccountID
Map<String, String> SetupFormMap = new Map<String, String>();
//Loop for each update
for (Account acc : System.Trigger.new){
//If SAP Number is not Null
if (acc.IF_SAP__c != null){
//Put SAP ID into map.
SetupFormMap.put(acc.ID, acc.IF_SAP__c);
}
else{

}
}
//Create a list to put all records in that need updating.
List<AccountSetupForm__c> recordsforupdates = new list<AccountSetupForm__c>();
//For each key that is in the map and has an AccountSetupForm
for (AccountSetupForm__c aacc :[Select Id, AccountName__c, SAP_Number__c from AccountSetupForm__c where
AccountName__c IN : SetupFormMap.keySet()] ){
//If the Map contains the AccountSetupForm Foreign Key
if (SetupFormMap.containsKey(aacc.AccountName__c)){
//Take SAP Number from map and add to AccountSetupForm
aacc.SAP_Number__c = SetupFormMap.get(aacc.AccountName__c);
//Add updated AccountSetupForm record to list.
recordsforupdates.add(aacc);
}
}
//Update all AccountSetupForm records
update recordsforupdates;
}

 


 

Message Edited by MattyDHL on 10-21-2009 12:36 AM
Message Edited by MattyDHL on 10-21-2009 02:18 AM