• Mario G.
  • NEWBIE
  • 10 Points
  • Member since 2014
  • Salesforce Developer
  • Yellow Pages Group

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies

I have a method that calls a @future method to make a callout.  For me to pass a list of objects to the callout, I created a static map  that holds this list with and associated Id.  Prior to the callout, I place the list of objects to be process in the map, place the callout, and retrivieve the list from within the callout method (I have to do this because I'm working with "in memory" updated objects not commited to the database).

Everything works fine when I do my code coverage tests but in real life, the static map always returns null when I retrieve the list of objects to process.
Am I missing something?
It's like the callout gets processed before the static variables manipulations which I doubt.

Thanks for the help!

This code simplifies what I'm trying to do:

public class MyClass() {
	static map<Id, list<Custom_Object__c>> tempDrawer = new map<Id, list<Custom_Object__c>>();
	static Boolean someBool = false;

	/**
	 * This calls a webservice
	 */
	@future (callout=true)
	static void calloutMethod(Id someId) {
		list<Custom_Object__c> objsToProcess = tempDrawer.remove(someId);

		//someBool is set to false!
		//objsToProcess.get(someId) returns null!
		if(someBool && objsToProcess != null && objsToProcess.size() > 0) {
			//process the objects and make the webservice call
		}
	}

	/**
	 * This method is called from a visualPage button
	 */
	public void someMethod(Id caseId) {
		//some code here...

		Custom_Object__c[] cObjs = new list<Custom_Object__c>();

		for(Integer i=0; i<10; i++) {
			cObjs.add(new Custom_Object__c(Status__c='New'));
		}

		tempDrawer.put(caseId, cObjs);
		someBool = true;
		system.assertEquals(10, tempDrawer.get(caseId).size()) //this is OK!
		system.assertEquals(true, someBool)	//this is OK too!

		//calling the callout method
		calloutMethod(caseId);
	}
}



I have a trigger on a Lead that verifies if the lead has been converted and does some actions if some criteria is met.
The code seems to work fine on the application but my test class fails when I programatically convert a Lead.

The error seems to be a validation rule that fails.
This validation rule uses information from the $ objects provided:
$Setup.ypg_switches__c.data_Lead__c
Where ypg_switches is a hierarchical custom setting having data_Lead as a checkbox (true/false)
This "Error Condition Formula" always fails on my test method no matter what the value of the custom setting is with the following error:
16:32:39.116 (6116300765)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Validation Formula "AC" Invalid (null): []
Usually we expect to receive the error message.

I also tested another Error Condition Formula:
$User.data_AllOrAccount__c
This is a calculated field on the user object that return true or false but i'm experiencing the same behaviour... failure nomatter what's the value.

My question are...
Is it possible that those $objects are not available when in test mode?
If not available, how to test something like this?  I don't want to disable any validation rules just to test code!
If available, what am I doing wrong?

Here's the whole test class:
/**************************************************************************
 * Tests the MigrateAssociatedAssignmentRequests fonction
 **************************************************************************/
@isTest static void MigrateAssociatedAssignmentRequestsTest() {
	Integer nbObjectsToTest = 1;
	
	//preparing test (creates the custom setting if not available)
	ypg_Switches sws = new ypg_Switches(false);
	sws.dataAllSwitch = true;
	sws.dataLeadSwitch = true;
	sws.dataLoadSwitch = false;
	sws.saveToSF();
	
	String ownerToUse = retrieveUserId('John Doe');
	String ARType = retreiveRType('Assignment_Request');
	
	Lead[] leads = generateTestLeads(nbObjectsToTest, '514');
	
	//create AR
	Case c = new Case(
		RecordTypeId = ARType,
		Type = AR_TYPE_GENERAL,
		Status = AR_STATUS_INPROGRESS,
		Requested_By__c = ownerToUse
	);
	
	insert c;
	
	list<Assignment_Request_Item__c> aris = new list<Assignment_Request_Item__c>();
	
	//create AR Item
	for(Lead l: leads) {
		Assignment_Request_Item__c ari = new Assignment_Request_Item__c(
			Lead__c = l.Id,
			Case__c = c.Id,
			Assignee__c = ownerToUse
		);
		
		aris.add(ari);
	}
	
	insert aris;
	
	//preparing Lead convert
	LeadStatus ls = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
	list<Database.LeadConvert> converts = new list<Database.LeadConvert>();
	
	for(Assignment_Request_Item__c ari: aris) {
		Database.LeadConvert lc = new Database.LeadConvert();
		
		lc.setLeadId(ari.Lead__c);
		lc.setConvertedStatus(ls.MasterLabel);
		
		converts.add(lc);
	}
	
	Test.startTest();
	
	//convert leads
	Database.LeadConvertResult[] lcrs = Database.convertLead(converts);
	
	Test.stopTest();
	
	//asserting outcome
	system.assert(sws.dataAllSwitch);
	system.assert(sws.dataLeadSwitch);
	
	map<Id,Id> LeadToAcntMap = new map<Id,Id>();
	
	for(Database.LeadConvertResult lcr: lcrs) {
		system.assert(lcr.isSuccess());
		LeadToAcntMap.put(lcr.getLeadId(), lcr.getAccountId());
	}
	
	map<Id, Assignment_Request_Item__c> ariMap = new map<Id, Assignment_Request_Item__c>(aris);
	Assignment_Request_Item__c[] arisUpd = [SELECT Id, Case__c, Account__c, Lead__c FROM Assignment_Request_Item__c WHERE Id IN :ariMap.keySet()];
	
	for(Assignment_Request_Item__c ari: arisUpd) {
		system.assertEquals(null, ari.Lead__c);
		system.assertEquals(LeadToAcntMap.get(ariMap.get(ari.Id).Lead__c), ari.Account__c);
	}
}
Thanks for your help!




I have a method that calls a @future method to make a callout.  For me to pass a list of objects to the callout, I created a static map  that holds this list with and associated Id.  Prior to the callout, I place the list of objects to be process in the map, place the callout, and retrivieve the list from within the callout method (I have to do this because I'm working with "in memory" updated objects not commited to the database).

Everything works fine when I do my code coverage tests but in real life, the static map always returns null when I retrieve the list of objects to process.
Am I missing something?
It's like the callout gets processed before the static variables manipulations which I doubt.

Thanks for the help!

This code simplifies what I'm trying to do:

public class MyClass() {
	static map<Id, list<Custom_Object__c>> tempDrawer = new map<Id, list<Custom_Object__c>>();
	static Boolean someBool = false;

	/**
	 * This calls a webservice
	 */
	@future (callout=true)
	static void calloutMethod(Id someId) {
		list<Custom_Object__c> objsToProcess = tempDrawer.remove(someId);

		//someBool is set to false!
		//objsToProcess.get(someId) returns null!
		if(someBool && objsToProcess != null && objsToProcess.size() > 0) {
			//process the objects and make the webservice call
		}
	}

	/**
	 * This method is called from a visualPage button
	 */
	public void someMethod(Id caseId) {
		//some code here...

		Custom_Object__c[] cObjs = new list<Custom_Object__c>();

		for(Integer i=0; i<10; i++) {
			cObjs.add(new Custom_Object__c(Status__c='New'));
		}

		tempDrawer.put(caseId, cObjs);
		someBool = true;
		system.assertEquals(10, tempDrawer.get(caseId).size()) //this is OK!
		system.assertEquals(true, someBool)	//this is OK too!

		//calling the callout method
		calloutMethod(caseId);
	}
}



I have a trigger on a Lead that verifies if the lead has been converted and does some actions if some criteria is met.
The code seems to work fine on the application but my test class fails when I programatically convert a Lead.

The error seems to be a validation rule that fails.
This validation rule uses information from the $ objects provided:
$Setup.ypg_switches__c.data_Lead__c
Where ypg_switches is a hierarchical custom setting having data_Lead as a checkbox (true/false)
This "Error Condition Formula" always fails on my test method no matter what the value of the custom setting is with the following error:
16:32:39.116 (6116300765)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Validation Formula "AC" Invalid (null): []
Usually we expect to receive the error message.

I also tested another Error Condition Formula:
$User.data_AllOrAccount__c
This is a calculated field on the user object that return true or false but i'm experiencing the same behaviour... failure nomatter what's the value.

My question are...
Is it possible that those $objects are not available when in test mode?
If not available, how to test something like this?  I don't want to disable any validation rules just to test code!
If available, what am I doing wrong?

Here's the whole test class:
/**************************************************************************
 * Tests the MigrateAssociatedAssignmentRequests fonction
 **************************************************************************/
@isTest static void MigrateAssociatedAssignmentRequestsTest() {
	Integer nbObjectsToTest = 1;
	
	//preparing test (creates the custom setting if not available)
	ypg_Switches sws = new ypg_Switches(false);
	sws.dataAllSwitch = true;
	sws.dataLeadSwitch = true;
	sws.dataLoadSwitch = false;
	sws.saveToSF();
	
	String ownerToUse = retrieveUserId('John Doe');
	String ARType = retreiveRType('Assignment_Request');
	
	Lead[] leads = generateTestLeads(nbObjectsToTest, '514');
	
	//create AR
	Case c = new Case(
		RecordTypeId = ARType,
		Type = AR_TYPE_GENERAL,
		Status = AR_STATUS_INPROGRESS,
		Requested_By__c = ownerToUse
	);
	
	insert c;
	
	list<Assignment_Request_Item__c> aris = new list<Assignment_Request_Item__c>();
	
	//create AR Item
	for(Lead l: leads) {
		Assignment_Request_Item__c ari = new Assignment_Request_Item__c(
			Lead__c = l.Id,
			Case__c = c.Id,
			Assignee__c = ownerToUse
		);
		
		aris.add(ari);
	}
	
	insert aris;
	
	//preparing Lead convert
	LeadStatus ls = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
	list<Database.LeadConvert> converts = new list<Database.LeadConvert>();
	
	for(Assignment_Request_Item__c ari: aris) {
		Database.LeadConvert lc = new Database.LeadConvert();
		
		lc.setLeadId(ari.Lead__c);
		lc.setConvertedStatus(ls.MasterLabel);
		
		converts.add(lc);
	}
	
	Test.startTest();
	
	//convert leads
	Database.LeadConvertResult[] lcrs = Database.convertLead(converts);
	
	Test.stopTest();
	
	//asserting outcome
	system.assert(sws.dataAllSwitch);
	system.assert(sws.dataLeadSwitch);
	
	map<Id,Id> LeadToAcntMap = new map<Id,Id>();
	
	for(Database.LeadConvertResult lcr: lcrs) {
		system.assert(lcr.isSuccess());
		LeadToAcntMap.put(lcr.getLeadId(), lcr.getAccountId());
	}
	
	map<Id, Assignment_Request_Item__c> ariMap = new map<Id, Assignment_Request_Item__c>(aris);
	Assignment_Request_Item__c[] arisUpd = [SELECT Id, Case__c, Account__c, Lead__c FROM Assignment_Request_Item__c WHERE Id IN :ariMap.keySet()];
	
	for(Assignment_Request_Item__c ari: arisUpd) {
		system.assertEquals(null, ari.Lead__c);
		system.assertEquals(LeadToAcntMap.get(ariMap.get(ari.Id).Lead__c), ari.Account__c);
	}
}
Thanks for your help!



hi
 
i want to retrieve the Server URL in trigger or in APEX class?
 
Actual my requirement is:
 
i had an object RelatedAttachement. in that Hyperlink field is of URL type. in that i am saving the URL like /servlet/servlet.FileDownload?file=00PR0000000MqVH. For this i want to retrieve the server URL and concatenate that Server URL to this field and updating this field.  in one server it looks like http://cs2.salesforce.com/servlet/servlet.Download?file=00PR0000000MqVH and in another server it looks like http://na2.salesforce.com/servlet/servlet.Download?file=00PR0000000MqVH so for this one i want to retrieve the server URL and i want to concatenate that URL to that field.
 
So please tell me how to retrieve the Server URL in trigger or in APEX class?