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
Tony DeMarcoTony DeMarco 

Pass a vlalue from a trigger to a class and return a value from the class back to the trigger

Is this possible?

from the trigger:

//call out to the class
returnStateAbbreviations f = new returnStateAbbreviations();
f.returnStateAbbreviations(stateName);

//attempt to return from class
 public with sharing class returnStateAbbreviations 
{  
    public string returnState(string stateName)
    { 
        String abbr;
        String stateAbbr;
        
        if(stateName=='Alabama')
        {
            stateAbbr = 'AL';            
        } //
// etc
        abbr = stateAbbr;
        return abbr;        
    }  

}

how would I then receive abbr back at the trigger?
Best Answer chosen by Tony DeMarco
Anupam RastogiAnupam Rastogi
Hi Tony,

These should be the lines of code to be used.

Trigger - 
//call out to the class
returnStateAbbreviations f = new returnStateAbbreviations();
String abbrFromClass;
abbrFromClass = f.returnState(stateName);

Class - 
//attempt to return from class
public with sharing class returnStateAbbreviations 
{  
    public string returnState(string stateName)
    { 
        String abbr;
        String stateAbbr;
        
        if(stateName=='Alabama')
        {
            stateAbbr = 'AL';            
        } 
        abbr = stateAbbr;
        return abbr;        
    }  
}

Thanks
AR

If you found the reply useful that solved your problem then please mark it as best answer.

All Answers

Rajiv Bhatt 16Rajiv Bhatt 16
You class returnStateAbbreviations has method named returnState and hence the line in your trigger should be f.returnState(stateName);

 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Tony DeMarco,

Yes you can do that.
Please refer below link.
http://www.embracingthecloud.com/2010/07/08/ASimpleTriggerTemplateForSalesforce.aspx
Anupam RastogiAnupam Rastogi
Hi Tony,

These should be the lines of code to be used.

Trigger - 
//call out to the class
returnStateAbbreviations f = new returnStateAbbreviations();
String abbrFromClass;
abbrFromClass = f.returnState(stateName);

Class - 
//attempt to return from class
public with sharing class returnStateAbbreviations 
{  
    public string returnState(string stateName)
    { 
        String abbr;
        String stateAbbr;
        
        if(stateName=='Alabama')
        {
            stateAbbr = 'AL';            
        } 
        abbr = stateAbbr;
        return abbr;        
    }  
}

Thanks
AR

If you found the reply useful that solved your problem then please mark it as best answer.
This was selected as the best answer
Tony DeMarcoTony DeMarco
Spot on my friend. Works like a charm. Tony DeMarco Salesforce Administrator alliantgroup