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
furamagfuramag 

Trigger to Update custom field

I have two custom objects (SFDC_Employee__c Promotion__c). I need to update field Unit__c (text) in object SFDC_Employee__c when sombody add or change field Unit__c (Picklist) in object Promotions__c.

I made trigger in object Promotions__c with this code:

Code:
trigger Unit_update on Promotions__c (after insert, after update) {

sObject s = [SELECT Unit__c FROM SFDC_Employee__c LIMIT 1];

s.Unit__c = 'sdfsfsdf';

update s;

}


But this trigger doesn't work. Can you please explane what is the better way to fix my problem.
David VPDavid VP
Doesn't your query need a 'where' clause ? I suppose you have more than one SFDC_Employee__c right ?



-David

furamagfuramag
Maybe I need 'where' clause in my query, but I don't now what I have to write there. Yes, I have SFDC_Benefit, SFDC_Employee, SFDC_PTO_Request, SFDC_Salary_History. Field Unit__c is into SFDC_Employee. I have an error when I save my trigger: Error: Compile Error: Field expression not allowed for generic SObject at line 5 column 1 .
arnt72arnt72
you need to replace sObject with the custom object API name or you cannot access anything but the ID (IIRC).
Here is more on queries and select:
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL.htm
http://www.salesforce.com/us/developer/docs/api/index_CSH.htm#sforce_api_calls_soql.htm
PerGeertPerGeert
What are the relationships between the two objects? I suppose there is a lookup referes from promotion to employee, i.e Employee -> Promotion? If so, this might help you (this is a trigger utilizing Account -> Contact, which updates Contact with info froma Account and Accoutn with info from Contact:
 
// When Contact is created, update mailing address with Account's primary address
// Standard functionality is that mailing address is copied from Billing Address
// but that might not be filled.
trigger tgrContact on Contact (before insert, before update) {
   // For every Contact record, add its associated Account entry
   // to a set so there are no duplicates.
   Set<Id> accIds = new Set<Id>();
   for (Contact co : Trigger.new) {
      accIds.add(co.AccountId);
   }
   // Query the Accounts for primary address and place
   // in a map.
   Map<Id, Account> accentries = new Map<Id, Account>(
      [select PrimaryCity__c, PrimaryCountry__c, PrimaryState__c, PrimaryStreet__c, PrimaryPostalCode__c from Account
      where id in :accIds]);
   // Now use the map to set mailing address for contact
   for (Contact co : Trigger.new) {
      if (co.AccountId != null) {
       if (co.MailingCity == null) {
          co.MailingCity = accentries.get(co.AccountId).PrimaryCity__c; 
          co.MailingCountry = accentries.get(co.AccountId).PrimaryCountry__c; 
          co.MailingState = accentries.get(co.AccountId).PrimaryState__c; 
          co.MailingStreet = accentries.get(co.AccountId).PrimaryStreet__c; 
          co.MailingPostalCode = accentries.get(co.AccountId).PrimaryPostalCode__c; 
       }
      }
   }
   // If the OLS flag is set on a contact, set the flag on the Account   
   Set<Id> accOlsIds = new Set<Id>();
   for (Contact co : Trigger.new)
   {     
    if (co.OLSUser__c == true)
    {        
     accOlsIds.add(co.AccountId);      
  }   
 }         
 // Create list of Accounts to update   
 List<Account> updAccount = new List<Account>();      
 for (Account ac : [select id, OLSUser__c from Account where
   OLSUser__c = false and id in :accOlsIds])   
   {      
    ac.OLSUser__c = true;
    updAccount.add(ac);   
 }   
 //update   
 update updAccount;
}
furamagfuramag
Please, give me only one example with custom object update. I found in this community many examples with Account and Contact update. I can update account, but I don't understand how can I update custom object field.
arnt72arnt72
Code:
sObject s = [SELECT Unit__c FROM SFDC_Employee__c LIMIT 1];

 you should use the following instead of the above
Code:
SFDC_Employee__c s = [SELECT Unit__c FROM SFDC_Employee__c LIMIT 1];

 otherwise you can access only the ID field



Message Edited by arnt72 on 10-15-2008 04:26 PM
furamagfuramag
This code is working. Thank You!
I have another problem with this trigger.
SFDC_Employee__c - custom object. Promotions__c have field Employee__c (lookup to SFDC_Employee__c).
To update SFDC_Employee__c.Unit__c I use this code:
Code:
trigger Unit_update on Promotions__c (after insert, before after) {

Promotions__c[] opp = trigger.new;

String str = opp[0].Employee_ID__c;

SFDC_Employee__c s = [SELECT Unit__c FROM SFDC_Employee__c WHERE Employee_ID__c = :str];

s.Unit__c = str;

update s;

}

 I don't now why, but str contain "a0I70000001pvU9EAI", it's wrong value. My id must contain "a0I70000001pvU9" (same value but without last 3 symbols). Why I have this problem?



Message Edited by furamag on 10-16-2008 03:46 PM
arnt72arnt72
the web interface shows you the 15digit version of the ID (case sensitive) while the API uses the 18digit IDs (case insensitive). I don't think that is causing the problems here because you are using the API only. What is the error message?

does this query return anything?
Code:
SFDC_Employee__c s = [SELECT Unit__c FROM SFDC_Employee__c WHERE Employee_ID__c = :str];

 



Message Edited by arnt72 on 10-17-2008 10:54 AM