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
Pal AduPal Adu 

Help with a field update trigger

I am trying to write a simple trigger for a custom field update(text to text)

trigger UpdateCompanyOverviewField on Trip_Report__c (before insert) {
    for( Trip_Report__c obj: trigger.new){
        if(Company_Name__c=Name){
            Company_Over__c = Company_Overview__c;
        }
    }
}

error: Variable does not exist: Company_Over__c

I am stumpmed as the API name for Company_Over__c checks out. Any help please
Carson HuangCarson Huang
I am pretty sure you have to add obj in front of Company_Over__c, since you are calling the variable that is inside the Trip_Report__c object.

So for Company_Name__c, Company_Overview__c and Company_Over__c, you would have to add a 'obj.' in front of them.

Also, in your if statement, you have to have 2 equal signs (==). One equal signs (=) mean you're assigning the left value to the right value. Two equal signs checks for equality.
sweetzsweetz
trigger UpdateCompanyOverviewField on Trip_Report__c (before Insert)  
{  
     for(Trip_Report__c trip: Trigger.new)   
     {       
         if(trip.Company_Name__c == 'XYZ Corp')     // If you are comparing other object value then fetch that using soql and call that here
         {       
             trip.Company_Over__c= 'Example';     //Give the field name you are assigning here
         }               
     }   
}
If you are struggling in API names, Go to that object and look for the api names there else use workbench or enterprise wsdl fr api names. 
Pal AduPal Adu
Hi Sweet@Salesforce

I tried it and it still throws an error. Company Overview Fields are Text Area on both Tabs

trigger UpdateCompanyOverviewField on Trip_Report__c (before insert)
{
for(Trip_Report__C trip:Trigger.new)
    {
        if(trip.Company_Name__c==[SELECT Name from Account])
        {
            trip.Company_Overview__c=[SELECT Company_Overview__c from Account];
        }
    }
}

Problem: Illegal assignment from LIST<Account> to String at Line 7
Carson HuangCarson Huang
Hey Pal Adu,

There's two errors in your code, and it is because your logic is flawed. [SELECT Name From Account] does not return Name, nor does [SELECT Company_Overview__c from Account] returns Company_Overview__c. These two SOQL returns the OBJECT with the information you selected. So for your first query, your query would return the object Account with the accessible information Name (all other fields are not accessible because you didn't select them). For your second query, your query would return the object Account with the Company_Overview__c information.

WHAT'S MORE is that you haven't set any prerequesites, so your queries will return ALL Accounts, returning a List rather than a single object.

To understand queries, this is how I view it:

[SELECT x FROM y] => Return all y with only the information x (disregarding any other possible fields from y). Now, to add prerequisites in order to get only the y/y's you want, you add a WHERE => [SELECT x FROM y WHERE z] => Return all y that meets the requirement(s) z with only the information x.

So, now back your situation, you would have to add a WHERE to both your queries in order to have a query search that returns ONE result (the result that concerns your trip object)

Here's more information on queries:

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL.htm

If you need more clarification, please ask.
sweetzsweetz
string companyname;
companyname= [SELECT Name
            FROM Account
            WHERE Type= 'Prospect' LIMIT 1].Name;
This is how you should get values from soql and assign it to a variable. If you are getting multiple records then you can use list.