• Lee_Campbell
  • NEWBIE
  • 105 Points
  • Member since 2012

  • Chatter
    Feed
  • 4
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 32
    Questions
  • 73
    Replies

Hi folks,

 

I'm trying to create an object record that populates fields that have the same API name on another object in a general way. So... when I create a record on object A, a trigger creates a new record on object B, sees which of the fields on the two objects have the same API name, then uses the field values from the record on object A and puts them in the appropriate field in object B. The code below yields the error "expression cannot be assigned" at the line that begins newEoi.get(sharedFields[i]) in the for loop inside the trigger.new loop.

 

trigger PROC_CreateEoi on Procurement__c(after insert){
	//retrieve a list of all of the fields on the Procurement and EPOI objects
	Set<String> matchProcFields = Procurement__c.sObjectType.getDescribe().fields.getMap().keySet();
	Set<String> matEoiFields = EOI__c.sObjectType.getDescribe().fields.getMap().keySet();
	
	List<String> procFieldComparison = new List<String>();
	List<String> eoiFieldComparison = new List<String>();
	
	
	//search for fields that have the same API name in both the Procurement__c object and the EOI__c object.
	
	Set<String> fieldsTemp = new Set<String>();
	for(integer i = 0; i < eoiFieldComparison.size();i++){
		for(integer j = 0;j<procFieldComparison.size();j++){
			if(procFieldComparison[j] == eoiFieldComparison[i]){
				fieldsTemp.add(eoiFieldComparison[i]);
			}
		}
	}
	List<String> sharedFields = new List<String>();
	sharedFields.addAll(fieldsTemp);
	
	for(Procurement__c newProc:trigger.new){
		EOI__c newEoi = new Milestone1_Project__c();
		for(integer i = 0;i<sharedFields.size();i++){
			//assign the variables with the same name to the same value in the newly-created EOI record
			newEoi.get(sharedFields[i]) = newProc.get(sharedFields[i]);
		}
	}
}

 

I've tried doing this with "try and catch" statements and using String.valueof() etc. to see if that would handle the error, but I still get the "Expression cannot be assigned" error. So, any ideas how I assign the field values in this general way, without having to explicitly type all of the field names?

 

Thanks in advance, wonderful people of developerforce :D

 

Lee

Hi all,

 

Does anyone know of a way of producing a "list" of objects that are children of a given object? For example, if I have an object that is the parent in a master-detail relationship and that object has many objects at the "detail" end of the relationship, is there a way I can list these detail objects? I don't mind what format the list is in (string, string[], List<>, it's trivial to sort that out, isn't it?), I'd just like a way of querying, programmatically, for all of the objects that are children of a given object. I've RTFM, but I can't seem to see how to do this.

 

Thanks in advance, lovely people of Developerforce!

 

Lee

Hi folks,

 

I'm trying to conditionally set the OwnerID of a record based upon who the owner of certain other records is. I have two custom objects, Procurement__c and Demo__c. Procurement__c has a lookup field for an Account and one for Opportunity. A Demo__c record is created upon creating a Procurement__c when it meets a certain criteria (picklist value), or updating a Procurement__c record if its status is changed to one of the picklist values that would have created a Demo__c upon inserting a Procurment__c. I want to set the owner of the Demo__c according to the following criteria:

 

If the Account recorded on the Procurement__c is not blank, make the Demo__c's owner that Account owner.

(demo.OwnerId = proc.Account__r.OwnerId;)

 

If it is blank, and the Opportunity recorded on the Procurement__c is not blank, use that Opportunity owner.

(demo.OwnerId = proc.Opportunity__r.OwnerId;)

 

If both the Opportunity and Account are blank, just use the Procurement__c owner.

(demo.OwnerId = proc.OwnerId;)

 

If I try that logic, I get an error message:

 

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]: and then my trigger name.

 

Can anyone think why that might be? I've tried both before and after insert and update, and none of the permutations work. Any tips?

 

Thanks,

Lee

Hi folks,

 

I'm trying to export some of our opportunity data using the Data Loader, and one of my custom fields is missing. It's a text formula that about half of all profiles can see. I have a different text formula that not everyone can see as well (there is some overlap as to the profiles that can see it), and that downloads fine, so that covers the field type and permissions... I can't think of what other variables (except Data Loader temperament!) could be causing this rather weird phenomenon. Any clues, folks?

 

Thanks,

Lee

Hi all,

 

Apparently, ProcessInstanceStep can't be "updated" via DML. I was just wondering if anyone knows, if I change something in ProcessInstanceStep and then DML update its parent ProcessInstance, will the Step itself get updated? I'm trying to create a means by which approvers can approve things via email (more specifically, advance approval steps from "pending" to "approved" via email).

 

Any help you could give would be greatly appreciated, I can't find anything that clarifies the issue over the forums or in the docs.

 

Lee

Could anyone suggest to me why the following code, in which the variable being assigned from is a Date/Time and the "assigned to" is a private String:

 

completionFieldName = Milestone1_Project__c.Procurement_Link__c.Date_Time_Submitted__c.getDescribe().getName();

 

yields no error but the following, where the left is, again a private String and the right is a Boolean, gives the "method does not exist getDescribe()" error?

 

completionFieldName = Milestone1_Project__c.Procurement_Link__c.First_Pass_Complete__c.getDescribe().getName();

 

Any help greatly appreciated.

 

Thanks,

Lee

Hi folks,

 

I've written a function that's called by a trigger upon creation of an opportunity that calculates the percentage chance of winning an opportunity based on historical performance given a number of "factors" pertaining to that opportunity, namely: who the opportunity owner is, which product it is we're selling, whether we've sold to this customer before, the total value of the proposed sale, and which account we're selling to (which is subtly different from repeat business). The function works out the previous percentages of successful sales based on each of these criteria and provides a kind of weighted average of the "probability" of winning the opportunity based on these, for forecasting purposes.

 

The trouble, however, is that I'm getting SOQL limit exceptions in my live environment, which, as you might expect, has a lot more data to draw upon to make these calculations. Having said that, thinking about the number of SOQL queries a single calculation should make, it should be nowehere near the 50,000 limit. The code (below), as I said, works out the percentage chance of winning an opportunity based upon the following, which I've tabulated, and given, what I think is a sum of the total number of SOQL queries induced by a single opportunity being created (to reiterate, the function code is called by a trigger when an opportunity is created, and this trigger does NOTHING else):

 

Opportunity Owner - nobody in my organisation owns more than about 500 opportunities, so that's 500 SOQL queries.

Account - no account has more than about 200 opportunities recorded against it, taking our total to 700.

Value - I've bucketed the value into three categories, none of which has more than 4,000 members, taking our total to 4,700.

Type - this pertains to whether or not this is repeat business and has three categories, none of which has more than about 4,000 members again, taking our total to 8,700 queries.

Product - again, this has a few categories, but none of these have more than about 2,000 recorded opportunities against them, taking the total to 10,700

 

So, after these very generous estimates, my code can call no more than 10,700 queries per invocation of the trigger (I understand that this would cause me problems if I want to bulk upload opportunities, but I'll cross that bridge when I come to it), in the meantime, I'm getting the SOQL limit error after only one invocation. My question then, is, given the code, can anybody point me in the direction of a solution to hitting this limit, which, I don't see how I'm hitting?

 

Apologies for being so verbose, but, hopefully, there shouldn't be anything I need to clarify :/

 

Thanks,

Lee

public class FCastProb {
	public static double getProbability(ID own, String opptype, String pipe, ID acc, decimal value){
    	
        double ownWins = 0;
        double ownSize=0;
        for(Opportunity a: [select Probability from Opportunity where OwnerID =:own and IsClosed=true]){
           		ownSize+=1;
                if(a.Probability==100){
               		ownWins+=1;
           		}
        	}
        
        
        if(ownWins == 0||ownSize==0){
            ownSize=1;
            ownWins=1;
        }
                
        double typeWins = 0;
        double typeSize = 0;
                
        	for(Opportunity b : [select Probability from Opportunity where Type =: oppType and IsClosed=true]){
           		typeSize+=1;
                if(b.Probability==100){
	               	typeWins+=1;
    	       	}
        	}
    	
        
    	if(typeWins==0||typeSize==0){
        	typeSize = 1;
        	typeWins = 1;
    	}
        
        double pipeWins = 0;
        double pipeSize = 0;
                	for(Opportunity c : [select Probability from Opportunity where Market_Pipeline__c =: pipe and IsClosed=true]){
           		pipeSize+=1;
                if(c.Probability==100){
	               	pipeWins+=1;
    	       	}
        	}
                if(pipeSize==0||pipeWins==0){
            pipeSize = 1;
            pipeWins = 1;
        }
                
        double accWins = 0;
               	double accSize=0;
        
			for(Opportunity d : [select Probability from Opportunity where AccountID =: acc and IsClosed = true]){
           		accSize+=1;
                if(d.Probability==100){
               		accWins+=1;
           		}
        	}
        
        if(accSize==0||accWins==0){
            accSize = 1;
            accWins = 1;
        }
        
        double valwins = 0;
        double valsize = 0;
        if(value<SOME_VALUE){
            for(Opportunity a: [select Probability from Opportunity where Total_Oppty_Amount__c < SOME_VALUE and IsClosed = true]){
                valsize+=1;
                if(a.Probability==0){
                    valwins+=1;
                }
            }
        }
        else if(value<SOME_VALUE&&value>=SOME_VALUE){
            for(Opportunity a:[select Probability from Opportunity where Total_Oppty_Amount__c >=SOME_VALUE and Total_Oppty_Amount__c <SOME_VALUE and IsClosed =true]){
                valsize+=1;
                if(a.Probability==100){
                    valwins+=1;
                }
            }
        }
        else for(Opportunity a:[select Probability from Opportunity where Total_Oppty_Amount__c >=SOME_VALUE and IsClosed = true]){
            valsize+=1;
            if(a.Probability==100){
                valwins+=1;
            }
        }
        if(valwins==0||valsize==0){
            valwins=1;
            valsize=1;
        }
        
        double x = 100*(ownWins/ownSize)*(typeWins/typeSize)*(pipeWins/pipeSize)*(accWins/accSize)*(valwins/valsize);

        system.debug('X = ' +x);
                
        return x;
    }
}

 

trigger BPT_CreateProject on Procurement__c (before insert, after update) {
    map< id, Milestone1_Project__c > proj = new map< id, Milestone1_Project__c >( );

    
	
    if(Trigger.isinsert){

    for( Procurement__c proc : trigger.new ) {
               proj.put( proc.id,
            new Milestone1_Project__c(
                        Name = proc.Name,
                        
                        Procurement_Link__c = proc.id
                		//a few other fields are passed fine by the code
                		)
            );
    
    insert proj.values();
}

 Hi folks,

 

I was wondering if you could help...

 

In the trigger above, I'm trying to create a new record in a custom object that contains much of the information from another custom object when that is created. So... I create a "Procurement__c" record, and the trigger automatically creates a new "Milestone1_Project__c" record. In this Milestone1_Project__c record, I want a lookup automatically populated with the ID of the Procurement__c record upon creation, among other fields. The other fields work fine when using a trigger like the one above, but this passing of the ID isn't working - the field is blank in the Milestone1_Project__c record.

 

Any ideas as to what I'm doing wrong here?

 

Many thanks,

Lee

Hi folks,

 

I've written a really simple controller extension for a custom object, that ensures the "target" of my Visualforce page is only ever one record (there only exists one record for this object, it's a kind of "joining object").


The class is below, but I'm struggling to get my head round what my test class should try and invoke. Any help massively appreciated!

 

Thanks,

Lee

 

public class BPT_SummaryViewController{
    public BPT_SummaryViewController(ApexPages.StandardController projcon){}
    
    public Bid_Planning_Summary__c getSummary(){
        Bid_Planning_Summary__c a = [select id from Bid_Planning_Summary__c][0];
        return a;
    }
}

 

Hi folks,

 

Is there any way I can convert the body of an inbound email from html to Plain Text? I've set up a service to email directly into chatter, but html emails sent from my organisation into Chatter are all of over 1,000 characters in length (even if I send a blank email). So, I'm looking for a way to strip the body text from the emails and discard all the html "nonsense", rather than force users to send emails in Plain Text format, which they'll be disinclined to do.

 

Any help anyone can give would be massively appreciated.

 

Thanks,

Lee

A query I'm running in a test:

 

Milestone1_Project__c x = [select id from Milestone1_Project__c limit 1];

Gives the "list has no rows" error, which is impossible, as there definitely exists some "Milestone1_Project__c" records. I have absolutely no idea why this is happening. Can anyone shed any light on the subject?

 

(I know it's common practice to create new records for use in a test, but I don't want to, for reasons I'll elaborate if anyone asks.)

Hi folks,

 

I was wondering if it's possible to produce dynamic SOQL queries. I'll elaborate:

 

I have a class whose input is a string, let's call it str. I then use str to manipulate a double on an object record (rec), like so:

 

(double)rec.get(str)

 

I'd like to query just for this double, specified by the above code, as, at the moment, I'm running something like

 

for(Custom_Object__c rec: [select **loads of different doubles** from Custom_Object__c where **condition**]){

 

which is hitting the SOQL query limit. Is there a way of, in the for loop above, just using "selecting" the double specified by (double)rec.get(str)? i.e. if str was something like "Number_of_Days__c", the loop:

 

for(Custom_Object__c rec: [select (double)str from Custom_Object__c where **condition**]){

 

would yield the same value as:

 

for(Custom_Object__r rec: [select Number_of_Days__c from Custom Object__c where **condition**]){

 

Any help you can give would be massively appreciated.

 

Thanks,

Lee

Hi all,

 

I was wondering if it's possible to do a "double.valueof(string)" and avoid the "Invalid Double" error if my string is something like:

 

"3 is the magic number"

 

My string will always begin with a number (just trust me, it will), but it could be of any length, so I can't use "substring". Any ideas?

 

Thanks,

Lee

Hi folks,

 

I'm using an inbound email handler so that users in my organisation who aren't particularly familiar with Chatter can post to Chatter any useful information they might have. However, the code below is doing some weird stuff (described below the code).

 

global class EmailtoChatter implements Messaging.InboundEmailHandler {
 
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        
        List<User> ChatterUser = [select id, Name, Email from User where Email =: email.fromAddress and IsActive = true limit 1];
        String s = '%'+email.Subject+'%';
        List<Account> AccRef = [select id, Name from Account where (Name like :s or Short_Name__c like :s) and ParentID = null limit 1];
            FeedItem a = new FeedItem();
            a.CreatedByID = ChatterUser[0].id;
            
            a.Body = email.PlainTextBody;
            a.ParentID = AccRef[0].id;
            insert a;
        
        
        
        
        return result;
        
        
    }
}

 

1) It works fine if I, system administrator, email to the inbound service I've set up.

2) If I try to email a post to chatter via the inbound service as a Chatter Free User in the Developer Environment, nothing happens (no debug log, nothing).

3) If I specify the ID of the Chatter Free User, rather than using the list, the Chatter Free User posts a comment, containing the contents of the email to the Account, even though he can't see the Account by logging into Salesforce, exactly as I had intended.

4) If I specify the ProfileID of Chatter Free User in the SOQL for users, I get a list of no size returned, even though I clearly have a Chatter Free User.

 

Help!!!

Hi all,

 

I'm writing a class that performs some simple arithmetic based on opportunity probabilities to get the average probability of all that owner/account/product's opportunities. My class takes some IDs and strings, and I want it to create lists based on these input arguments, but keep getting the "unexpected token" error. Here's a snippet of the code:

 

public class ForecastFunction {
	public static double getProbability(ID own, ID acc, String product, String division){
    	double estimate = 0;
        List<Opportunity> a = [select Probability from Opportunity where OwnerID := own];
        for(integer i = 0;i<a.size();i++){
            estimate+=a[i].Probability/a.size();
        }
        return estimate;
    }
}

 Where "own" is my unexpected token (both with and without the colon). What am I doing wrong? This should be bread and butter, but that error is crippling, and, to me, unobvious.

 

Any help you can offer would be great.

 

Thanks,

Lee

Hi folks,

 

I have a slight issue with a class that's implemented by a schedulable class...

 

I'm struggling to write a test class that gets any coverage for a class implemented by some scheduled apex. The schedule part itself is fine, but the class it implements is getting little-to-no coverage. Could anybody offer me any pointers as to how to get good coverage for the class below?

 

public class EmailSalesOffice {
   public static void SendEmail(){
      List<Milestone1_Milestone__c> activeproc = new List<Milestone1_Milestone__c>([Select id, Project__r.Sales_Office_Lead__r.Email, Project__r.Sales_Office_Lead__r.Name, Name, Project__r.Owner.Email, Kickoff__c, Project__r.Name, Deadline__c from Milestone1_Milestone__c where Deadline__c<=TODAY AND Kickoff__c>=TODAY]);
      String orgWideAddID = [select Address from OrgWideEmailAddress][0].Address;
       String[] address = new String[]{};
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           for(integer i=0;i<activeproc.size();i++){
               if(activeproc[i].Project__r.Sales_Office_Lead__r.Email!=null){
               address.add(activeproc[i].Project__r.Sales_Office_Lead__r.Email);
               }
       		address.add('me@something.com');
     
       			mail.setToAddresses(address);
                system.debug('TO ADDRESSES: '+address);
           		mail.setReplyTo(OrgWideAddID);
                system.debug('REPLY TO ADDRESS: '+OrgWideAddID);
       	      	mail.setSenderDisplayName('Display Name');
       			mail.setSubject('Record of hours spent on "' +activeproc[i].Name+'" of "' +activeproc[i].Project__r.Name+'".');
       			mail.setBccSender(false);
       			mail.setUseSignature(false);
       			mail.setPlainTextBody('Some text.');
       			List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.Email[] { mail });
       			System.debug('Email Sent: '+results.get(0).isSuccess() );
                }
   }
       		}

 

I've tried things like "System.assert(results.get(0).isSuccess());", but it doesn't seem to do anything to add to my coverage. I'm not after someone to write a test class for me, per se, but I'd like some "methodology" if you will for writing test classes surrounding email.

 

Thanks,

Lee

If I created a class such as:

 

public class myClass{
    public double myMethod(List<Object> a, integer b){
        //some logic
    }
}

 

And a list such as: [Select id, some_double__c, some_String__c, some_other_double__c from Object where blah blah blah],

 

could I use those doubles in the class?

 

i.e. logic that said something generic like:

 

a new double x is found by finding the doubles on row i of the list, multiply the first double by 5 then subtract the second double, which in the case above would be:

 

x = 5 * a[i].some_double__c - a[i].some_other_double__c;

 

Any ideas?

 

 

Hi folks,

 

I have a number of Triggers that, upon creation of a custom record, automatically create an associated custom record. The Triggers themselves fire fine, but I can only get about 70% code coverage in my test classes. If anyone could help me with the form one of the test classes should take, I'm sure I'll be able to write the rest for the other Triggers. Here's an example of the kind of Trigger I mean:

 

trigger AutoProject on Procurement__c (before insert) {
    map< id, Milestone1_Project__c > proj = new map< id, Milestone1_Project__c >( );
    
    for( Procurement__c proc : trigger.new ) {
       proj.put( proc.id,
            new Milestone1_Project__c(
                  Name = proc.Name,
                  Submission_Deadline__c = proc.Submission_Deadline__c)
            );
    
    insert proj.values();
    proc.Bid_Planning_Timeline__c = proj.get(proc.id).id;    
    }        
    
}

 Any help very greatly appreciated.

 

Many thanks,

Lee

I've set up a class that sends an email to email addresses stored as fields within a custom object. The way the class works is that it queries for the custom objects that have "expired" and emails to everyone on this list of addresses. I want their responses to be recorded directly against the object, so, I was thinking I'd need a means of recording the outbound email ID, storing it and seeing what the inReplyTo feild contained in any inbound messages to match them back up to the correct record entry of the custom object. Any ideas how I can record these outbound message IDs? I can't seem to find an API name for them in the documentation.

 

Thanks,

Lee

I'm trying to set up a service that generates an email and then uses the contents of the response to update a field. To do this, I set my mail.replyTo address on the outbound message as the huge auto-generated inbound mail handler address. Later, staring into the fridge thinking about what I wanted on my sandwich, it dawned on me that, when I try to deploy that into a production environment, it's not going to work, because the email address will be different. Any clues as to how I get round that?

 

Thanks,

Lee

Hi folks,

 

I'm trying to create an object record that populates fields that have the same API name on another object in a general way. So... when I create a record on object A, a trigger creates a new record on object B, sees which of the fields on the two objects have the same API name, then uses the field values from the record on object A and puts them in the appropriate field in object B. The code below yields the error "expression cannot be assigned" at the line that begins newEoi.get(sharedFields[i]) in the for loop inside the trigger.new loop.

 

trigger PROC_CreateEoi on Procurement__c(after insert){
	//retrieve a list of all of the fields on the Procurement and EPOI objects
	Set<String> matchProcFields = Procurement__c.sObjectType.getDescribe().fields.getMap().keySet();
	Set<String> matEoiFields = EOI__c.sObjectType.getDescribe().fields.getMap().keySet();
	
	List<String> procFieldComparison = new List<String>();
	List<String> eoiFieldComparison = new List<String>();
	
	
	//search for fields that have the same API name in both the Procurement__c object and the EOI__c object.
	
	Set<String> fieldsTemp = new Set<String>();
	for(integer i = 0; i < eoiFieldComparison.size();i++){
		for(integer j = 0;j<procFieldComparison.size();j++){
			if(procFieldComparison[j] == eoiFieldComparison[i]){
				fieldsTemp.add(eoiFieldComparison[i]);
			}
		}
	}
	List<String> sharedFields = new List<String>();
	sharedFields.addAll(fieldsTemp);
	
	for(Procurement__c newProc:trigger.new){
		EOI__c newEoi = new Milestone1_Project__c();
		for(integer i = 0;i<sharedFields.size();i++){
			//assign the variables with the same name to the same value in the newly-created EOI record
			newEoi.get(sharedFields[i]) = newProc.get(sharedFields[i]);
		}
	}
}

 

I've tried doing this with "try and catch" statements and using String.valueof() etc. to see if that would handle the error, but I still get the "Expression cannot be assigned" error. So, any ideas how I assign the field values in this general way, without having to explicitly type all of the field names?

 

Thanks in advance, wonderful people of developerforce :D

 

Lee

Hi all,

 

Does anyone know of a way of producing a "list" of objects that are children of a given object? For example, if I have an object that is the parent in a master-detail relationship and that object has many objects at the "detail" end of the relationship, is there a way I can list these detail objects? I don't mind what format the list is in (string, string[], List<>, it's trivial to sort that out, isn't it?), I'd just like a way of querying, programmatically, for all of the objects that are children of a given object. I've RTFM, but I can't seem to see how to do this.

 

Thanks in advance, lovely people of Developerforce!

 

Lee

Hi folks,

 

I'm trying to conditionally set the OwnerID of a record based upon who the owner of certain other records is. I have two custom objects, Procurement__c and Demo__c. Procurement__c has a lookup field for an Account and one for Opportunity. A Demo__c record is created upon creating a Procurement__c when it meets a certain criteria (picklist value), or updating a Procurement__c record if its status is changed to one of the picklist values that would have created a Demo__c upon inserting a Procurment__c. I want to set the owner of the Demo__c according to the following criteria:

 

If the Account recorded on the Procurement__c is not blank, make the Demo__c's owner that Account owner.

(demo.OwnerId = proc.Account__r.OwnerId;)

 

If it is blank, and the Opportunity recorded on the Procurement__c is not blank, use that Opportunity owner.

(demo.OwnerId = proc.Opportunity__r.OwnerId;)

 

If both the Opportunity and Account are blank, just use the Procurement__c owner.

(demo.OwnerId = proc.OwnerId;)

 

If I try that logic, I get an error message:

 

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]: and then my trigger name.

 

Can anyone think why that might be? I've tried both before and after insert and update, and none of the permutations work. Any tips?

 

Thanks,

Lee

Hi folks,

 

I'm trying to export some of our opportunity data using the Data Loader, and one of my custom fields is missing. It's a text formula that about half of all profiles can see. I have a different text formula that not everyone can see as well (there is some overlap as to the profiles that can see it), and that downloads fine, so that covers the field type and permissions... I can't think of what other variables (except Data Loader temperament!) could be causing this rather weird phenomenon. Any clues, folks?

 

Thanks,

Lee

Hi all,

 

Apparently, ProcessInstanceStep can't be "updated" via DML. I was just wondering if anyone knows, if I change something in ProcessInstanceStep and then DML update its parent ProcessInstance, will the Step itself get updated? I'm trying to create a means by which approvers can approve things via email (more specifically, advance approval steps from "pending" to "approved" via email).

 

Any help you could give would be greatly appreciated, I can't find anything that clarifies the issue over the forums or in the docs.

 

Lee

Could anyone suggest to me why the following code, in which the variable being assigned from is a Date/Time and the "assigned to" is a private String:

 

completionFieldName = Milestone1_Project__c.Procurement_Link__c.Date_Time_Submitted__c.getDescribe().getName();

 

yields no error but the following, where the left is, again a private String and the right is a Boolean, gives the "method does not exist getDescribe()" error?

 

completionFieldName = Milestone1_Project__c.Procurement_Link__c.First_Pass_Complete__c.getDescribe().getName();

 

Any help greatly appreciated.

 

Thanks,

Lee

Hi folks,

 

I've written a function that's called by a trigger upon creation of an opportunity that calculates the percentage chance of winning an opportunity based on historical performance given a number of "factors" pertaining to that opportunity, namely: who the opportunity owner is, which product it is we're selling, whether we've sold to this customer before, the total value of the proposed sale, and which account we're selling to (which is subtly different from repeat business). The function works out the previous percentages of successful sales based on each of these criteria and provides a kind of weighted average of the "probability" of winning the opportunity based on these, for forecasting purposes.

 

The trouble, however, is that I'm getting SOQL limit exceptions in my live environment, which, as you might expect, has a lot more data to draw upon to make these calculations. Having said that, thinking about the number of SOQL queries a single calculation should make, it should be nowehere near the 50,000 limit. The code (below), as I said, works out the percentage chance of winning an opportunity based upon the following, which I've tabulated, and given, what I think is a sum of the total number of SOQL queries induced by a single opportunity being created (to reiterate, the function code is called by a trigger when an opportunity is created, and this trigger does NOTHING else):

 

Opportunity Owner - nobody in my organisation owns more than about 500 opportunities, so that's 500 SOQL queries.

Account - no account has more than about 200 opportunities recorded against it, taking our total to 700.

Value - I've bucketed the value into three categories, none of which has more than 4,000 members, taking our total to 4,700.

Type - this pertains to whether or not this is repeat business and has three categories, none of which has more than about 4,000 members again, taking our total to 8,700 queries.

Product - again, this has a few categories, but none of these have more than about 2,000 recorded opportunities against them, taking the total to 10,700

 

So, after these very generous estimates, my code can call no more than 10,700 queries per invocation of the trigger (I understand that this would cause me problems if I want to bulk upload opportunities, but I'll cross that bridge when I come to it), in the meantime, I'm getting the SOQL limit error after only one invocation. My question then, is, given the code, can anybody point me in the direction of a solution to hitting this limit, which, I don't see how I'm hitting?

 

Apologies for being so verbose, but, hopefully, there shouldn't be anything I need to clarify :/

 

Thanks,

Lee

public class FCastProb {
	public static double getProbability(ID own, String opptype, String pipe, ID acc, decimal value){
    	
        double ownWins = 0;
        double ownSize=0;
        for(Opportunity a: [select Probability from Opportunity where OwnerID =:own and IsClosed=true]){
           		ownSize+=1;
                if(a.Probability==100){
               		ownWins+=1;
           		}
        	}
        
        
        if(ownWins == 0||ownSize==0){
            ownSize=1;
            ownWins=1;
        }
                
        double typeWins = 0;
        double typeSize = 0;
                
        	for(Opportunity b : [select Probability from Opportunity where Type =: oppType and IsClosed=true]){
           		typeSize+=1;
                if(b.Probability==100){
	               	typeWins+=1;
    	       	}
        	}
    	
        
    	if(typeWins==0||typeSize==0){
        	typeSize = 1;
        	typeWins = 1;
    	}
        
        double pipeWins = 0;
        double pipeSize = 0;
                	for(Opportunity c : [select Probability from Opportunity where Market_Pipeline__c =: pipe and IsClosed=true]){
           		pipeSize+=1;
                if(c.Probability==100){
	               	pipeWins+=1;
    	       	}
        	}
                if(pipeSize==0||pipeWins==0){
            pipeSize = 1;
            pipeWins = 1;
        }
                
        double accWins = 0;
               	double accSize=0;
        
			for(Opportunity d : [select Probability from Opportunity where AccountID =: acc and IsClosed = true]){
           		accSize+=1;
                if(d.Probability==100){
               		accWins+=1;
           		}
        	}
        
        if(accSize==0||accWins==0){
            accSize = 1;
            accWins = 1;
        }
        
        double valwins = 0;
        double valsize = 0;
        if(value<SOME_VALUE){
            for(Opportunity a: [select Probability from Opportunity where Total_Oppty_Amount__c < SOME_VALUE and IsClosed = true]){
                valsize+=1;
                if(a.Probability==0){
                    valwins+=1;
                }
            }
        }
        else if(value<SOME_VALUE&&value>=SOME_VALUE){
            for(Opportunity a:[select Probability from Opportunity where Total_Oppty_Amount__c >=SOME_VALUE and Total_Oppty_Amount__c <SOME_VALUE and IsClosed =true]){
                valsize+=1;
                if(a.Probability==100){
                    valwins+=1;
                }
            }
        }
        else for(Opportunity a:[select Probability from Opportunity where Total_Oppty_Amount__c >=SOME_VALUE and IsClosed = true]){
            valsize+=1;
            if(a.Probability==100){
                valwins+=1;
            }
        }
        if(valwins==0||valsize==0){
            valwins=1;
            valsize=1;
        }
        
        double x = 100*(ownWins/ownSize)*(typeWins/typeSize)*(pipeWins/pipeSize)*(accWins/accSize)*(valwins/valsize);

        system.debug('X = ' +x);
                
        return x;
    }
}

 

trigger BPT_CreateProject on Procurement__c (before insert, after update) {
    map< id, Milestone1_Project__c > proj = new map< id, Milestone1_Project__c >( );

    
	
    if(Trigger.isinsert){

    for( Procurement__c proc : trigger.new ) {
               proj.put( proc.id,
            new Milestone1_Project__c(
                        Name = proc.Name,
                        
                        Procurement_Link__c = proc.id
                		//a few other fields are passed fine by the code
                		)
            );
    
    insert proj.values();
}

 Hi folks,

 

I was wondering if you could help...

 

In the trigger above, I'm trying to create a new record in a custom object that contains much of the information from another custom object when that is created. So... I create a "Procurement__c" record, and the trigger automatically creates a new "Milestone1_Project__c" record. In this Milestone1_Project__c record, I want a lookup automatically populated with the ID of the Procurement__c record upon creation, among other fields. The other fields work fine when using a trigger like the one above, but this passing of the ID isn't working - the field is blank in the Milestone1_Project__c record.

 

Any ideas as to what I'm doing wrong here?

 

Many thanks,

Lee

Hi All,

 

I am new to Saleforce development. Please can any one guide me to sample apex code to create(add) custom fields to custom or standard objects dynamically using Salesforce Metadata API in my development org.

 

Thanks in advance.

  • June 29, 2011
  • Like
  • 0