• hwelch
  • NEWBIE
  • 50 Points
  • Member since 2011

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

I have a trigger that works on a custom object called Sales Credit Detail and is supposed to add a sharing to theOpportunity owner (which is a related object via lookup).

 

Our org wide default for this Sales Credit object is Private and not using hierarchies to grant access. The idea is that ONLY the sales credit detail owner AND the opportunity owner can edit the sales credit detail (from now on SCD) record. An Opportunity can have many SCDs and for each the owner of that record gets default FULL access, BUT also for each of these I want to add the Opportunity owner to be able to have full access to it as well.

 

I decided to add a trigger that would add a share to the SCD for the Opp owner BUT when I edit the SCD record, the rule is not created for the Opportunity owner. BElow is the code, can someone see if I am failing somewhere? I cannot figure it out. I have tested the list sizes and the opp owner etc and they all give the correct value, so I dont know why it is not working

 

 

trigger Opportunity_Owner_share on Sales_Credit_Detail__c (after insert, after update) {
    //Create list of sharing records to create for SCDs in api call. 
    List<Sales_Credit_Detail__Share> SCD_Shares = new List<Sales_Credit_Detail__Share>();
    List<Sales_Credit_Detail__c> SCDS;
   
   //get all Opportunities for all SCD that will be inserted.
   List<Opportunity> opps = [SELECT Id, OwnerId, Name From Opportunity Where Id IN(SELECT ParentOpportunity__c From Sales_Credit_Detail__c Where Id IN :Trigger.newMap.keyset())];
   System.Debug(opps.size());
   
   //For each SCD record being inserted/edited, create a sharing record to the Opportunity owner of that SCD
    for(Sales_Credit_Detail__c scd: Trigger.new){
	   Sales_Credit_Detail__Share scd_share = new Sales_Credit_Detail__Share();
	   scd_share.ParentId = scd.Id;
	   scd_share.AccessLevel = 'All';
	   scd_share.RowCause = Schema.Sales_Credit_Detail__share.RowCause.Opportunity_Owner_Access__c;
	   System.Debug(scd_share.RowCause);
	  
	   for(Opportunity o:opps){
	   	//Add share record to list of shares to upload IF not for Opp Owner same as SCD owner
	       if(o.Id == scd.ParentOpportunity__c && o.OwnerId != scd.OwnerId){
	           scd_share.UserOrGroupId = o.OwnerId;
	           }       	
	       }
	    // Add the new Share record to the list of new Share records.
	    SCD_Shares.add(scd_share);
	    System.Debug(SCD_Shares);
        }
        //Insert all shares to the Database.
        Try{
         insert(SCD_Shares);
        }catch (Exception e){
        	System.Debug(e);
        }
}

 

 

I'm new to apex and i have somehow managed to create a trigger in sandbox. It works fine.

The only issue is when i try to inbound change to production. Trigger deployment fails and covers only 0% of code.

 

I know the reason is i haven't created any test class for the trigger.

I don't know how to create one.

Please help.

 

following is my trigger.

 

trigger changeowner on Case (before insert,before update) {
    for(Case c : Trigger.new) {
        if (c.Status == 'Closed to Case Owner' ) {
            String createId = c.CreatedById;
            c.OwnerId = createId.subString(0, 15);
        }
    }
}

 

  • February 18, 2011
  • Like
  • 0

I have a trigger that works on a custom object called Sales Credit Detail and is supposed to add a sharing to theOpportunity owner (which is a related object via lookup).

 

Our org wide default for this Sales Credit object is Private and not using hierarchies to grant access. The idea is that ONLY the sales credit detail owner AND the opportunity owner can edit the sales credit detail (from now on SCD) record. An Opportunity can have many SCDs and for each the owner of that record gets default FULL access, BUT also for each of these I want to add the Opportunity owner to be able to have full access to it as well.

 

I decided to add a trigger that would add a share to the SCD for the Opp owner BUT when I edit the SCD record, the rule is not created for the Opportunity owner. BElow is the code, can someone see if I am failing somewhere? I cannot figure it out. I have tested the list sizes and the opp owner etc and they all give the correct value, so I dont know why it is not working

 

 

trigger Opportunity_Owner_share on Sales_Credit_Detail__c (after insert, after update) {
    //Create list of sharing records to create for SCDs in api call. 
    List<Sales_Credit_Detail__Share> SCD_Shares = new List<Sales_Credit_Detail__Share>();
    List<Sales_Credit_Detail__c> SCDS;
   
   //get all Opportunities for all SCD that will be inserted.
   List<Opportunity> opps = [SELECT Id, OwnerId, Name From Opportunity Where Id IN(SELECT ParentOpportunity__c From Sales_Credit_Detail__c Where Id IN :Trigger.newMap.keyset())];
   System.Debug(opps.size());
   
   //For each SCD record being inserted/edited, create a sharing record to the Opportunity owner of that SCD
    for(Sales_Credit_Detail__c scd: Trigger.new){
	   Sales_Credit_Detail__Share scd_share = new Sales_Credit_Detail__Share();
	   scd_share.ParentId = scd.Id;
	   scd_share.AccessLevel = 'All';
	   scd_share.RowCause = Schema.Sales_Credit_Detail__share.RowCause.Opportunity_Owner_Access__c;
	   System.Debug(scd_share.RowCause);
	  
	   for(Opportunity o:opps){
	   	//Add share record to list of shares to upload IF not for Opp Owner same as SCD owner
	       if(o.Id == scd.ParentOpportunity__c && o.OwnerId != scd.OwnerId){
	           scd_share.UserOrGroupId = o.OwnerId;
	           }       	
	       }
	    // Add the new Share record to the list of new Share records.
	    SCD_Shares.add(scd_share);
	    System.Debug(SCD_Shares);
        }
        //Insert all shares to the Database.
        Try{
         insert(SCD_Shares);
        }catch (Exception e){
        	System.Debug(e);
        }
}

 

 

I have trigger handler class that generically handles Lead, Contact, and Account records.   The Lead, Contact and Account triggers assign it to an sObject variable, which they pass to the trigger handler class.   The trigger takes one of the fields of the sObject (the handler decides which field by getting the field name from configuration data) and uses the value (e.g. 1223334.0) of that field to build a dynamic SOQL query.   

  Example SOQL query string:    “Select AnnualRevenue from Lead where (AnnualRevenue = 1223334.0) “

The problem I am having is how to generically turn a field value (e.g. 1223334.0) into a string no matter which data type it is.
My first solution was to do the following:

  Object myfieldvalue_object  = mySObjectRecord.get(fieldname);
  String myfieldvalue_string = (String) myfieldvalue_object;
  buildQuery(myfieldvalue_string)

I immediately got an exception on the second line when the object was of type DOUBLE.

My next solution was to case on the datatype of the field and cast the myfieldvalue_object as the appropriate internal data type and then turn that datatype into a string.

  if (fieldtype == Schema.DisplayType.DateTime) {
     Double convertedvalue = ((Double) myfieldvalue_object);
     String myfieldvalue_string = convertedvalue.format();
     buildQuery(myfieldvalue_string)
  }
 
But, the converted string for a Double datatype contained commas and a decimal point (i.e the displayable value) which cannot be  used in a Dynamic SOQL query.

Given I have the field name, does anyone know how I can convert the value of a field in an sObject  to a String that can be used in a dynamic SOQL query.?    I need to be able to do it for all Object datat types with Date, Text, Number being the priority right now.

Thanks in advance.

Tom DJ

  • February 18, 2011
  • Like
  • 0

I'm new to apex and i have somehow managed to create a trigger in sandbox. It works fine.

The only issue is when i try to inbound change to production. Trigger deployment fails and covers only 0% of code.

 

I know the reason is i haven't created any test class for the trigger.

I don't know how to create one.

Please help.

 

following is my trigger.

 

trigger changeowner on Case (before insert,before update) {
    for(Case c : Trigger.new) {
        if (c.Status == 'Closed to Case Owner' ) {
            String createId = c.CreatedById;
            c.OwnerId = createId.subString(0, 15);
        }
    }
}

 

  • February 18, 2011
  • Like
  • 0