• Ingrid Stone 1
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 6
    Replies
I want to create opportuites automatically when I load new contacts.   I have wrtten a trigger but which sort of work am struggling with a couple of things:
- opportunity name should be opp-'contact name'  - this is not working
- the opportunity is not visible on the contact (from the opportunity you can get to contact).
- I have an additional field in opportunity that is a look up to user (not always the sane one loading data) this does not work.

This is my code - this is the first time I have written apex code :)

trigger CreateOpportunityFromContentContact on Contact (after insert) {
      List<Opportunity> oppsToUpdate = new List<Opportunity>();
      // If record_type = content_Contact 
      ID contactrecordid =[select Id from RecordType where Name = 'Content contacts' and SobjectType = 'Contact' limit 1].ID;
    ID opprecordid = [select Id from RecordType where Name = 'Content opportunity' and SobjectType = 'Opportunity' limit 1].ID; 
    
    for(Contact cc : Trigger.new) {

    // here is where you check if opportunity that is being inserted meets the criteria
          
   if (cc.RecordTypeid ==contactrecordid) {

    Opportunity o = new Opportunity (); 
    //set the object to put values for future record
// get account id  - this is not working 
  //    ID accrecordid = [select Accountid from account where name = :cc.id limit 1];        
       
    // now map opportunity fields to new object that is being created with this opportunity
    
list<contact> contact_name = [select name from contact where ID = :cc.id];
string oppname = cc.name;
  o.Name = 'Content Opp -' + cc.name;     
  o.recordtypeid = opprecordid;
  o.Content_Contact__c = cc.id ;
 // o.accountid=accrecordid;     
  o.Account = [select id from Account where name = 'xx'];
  //user opp_solicitor = [select ID from User where Name = :cc.Content_opp_Solictior__c];
  o.Content_Solicitor__c = cc.Content_opp_Solictior__c;
  
  list<ITCS_product__c> content_opp = [select ID from ITCS_Product__c where MySQL_Product_ID_Number__c = :cc.Content_opp_Product_ID__c];
    If (content_opp.size ( ) > 0) 
    { o.Content_ITCS_product__c = content_opp[0].id;
    } else o.Content_ITCS_product__c = null;
  o.StageName = 'In Progress'; 
  // close date to be a year from today
  o.CloseDate = Date.today() + 365;   
    

    oppsToUpdate.add(o);

 //   }//end if

}//end for o

//once loop is done, you need to insert new records in SF
// dml operations might cause an error, so you need to catch it with try/catch block.

    insert oppsToUpdate ;

    }
    }
I want to create opportuites automatically when I load new contacts.   I have wrtten a trigger but which sort of work am struggling with a couple of things:
- opportunity name should be opp-'contact name'  - this is not working
- the opportunity is not visible on the contact (from the opportunity you can get to contact).
- I have an additional field in opportunity that is a look up to user (not always the sane one loading data) this does not work.

This is my code - this is the first time I have written apex code :)

trigger CreateOpportunityFromContentContact on Contact (after insert) {
      List<Opportunity> oppsToUpdate = new List<Opportunity>();
      // If record_type = content_Contact 
      ID contactrecordid =[select Id from RecordType where Name = 'Content contacts' and SobjectType = 'Contact' limit 1].ID;
    ID opprecordid = [select Id from RecordType where Name = 'Content opportunity' and SobjectType = 'Opportunity' limit 1].ID; 
    
    for(Contact cc : Trigger.new) {

    // here is where you check if opportunity that is being inserted meets the criteria
          
   if (cc.RecordTypeid ==contactrecordid) {

    Opportunity o = new Opportunity (); 
    //set the object to put values for future record
// get account id  - this is not working 
  //    ID accrecordid = [select Accountid from account where name = :cc.id limit 1];        
       
    // now map opportunity fields to new object that is being created with this opportunity
    
list<contact> contact_name = [select name from contact where ID = :cc.id];
string oppname = cc.name;
  o.Name = 'Content Opp -' + cc.name;     
  o.recordtypeid = opprecordid;
  o.Content_Contact__c = cc.id ;
 // o.accountid=accrecordid;     
  o.Account = [select id from Account where name = 'xx'];
  //user opp_solicitor = [select ID from User where Name = :cc.Content_opp_Solictior__c];
  o.Content_Solicitor__c = cc.Content_opp_Solictior__c;
  
  list<ITCS_product__c> content_opp = [select ID from ITCS_Product__c where MySQL_Product_ID_Number__c = :cc.Content_opp_Product_ID__c];
    If (content_opp.size ( ) > 0) 
    { o.Content_ITCS_product__c = content_opp[0].id;
    } else o.Content_ITCS_product__c = null;
  o.StageName = 'In Progress'; 
  // close date to be a year from today
  o.CloseDate = Date.today() + 365;   
    

    oppsToUpdate.add(o);

 //   }//end if

}//end for o

//once loop is done, you need to insert new records in SF
// dml operations might cause an error, so you need to catch it with try/catch block.

    insert oppsToUpdate ;

    }
    }

Hello Guys,

I have created a trigger on contact that insert a new opportunity. It is working properly but when we perform bulk operation then it's giving the error CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: OpportunityCreatorOnContact: System.LimitException: Too many SOQL queries: 101 

See the trigger below and please provide me the solution for it

trigger OpportunityCreatorOnContact on Contact (before update) {
   
    if(OpportunityCreatorOnContactHandler.isFirstTime){
        OpportunityCreatorOnContactHandler.isFirstTime=false;
  
       List<Contact> ContList = new List<Contact>([SELECT Name,Contact.Account.Id,Contact.Account.Name, Most_recent_Opportunity_Stage__c, Contact.Account.OwnerId,Quarantine__c,mkto71_Lead_Score__c from Contact where id IN :Trigger.newMap.keySet()]); 
                for(Contact c : trigger.new)
                {   
                    for(Contact c1: ContList){  

                      if(c.Most_recent_Opportunity_Stage__c=='Closed Lost' && trigger.oldMap.get(c.Id).Create_New_Opp__c==false && c.Create_New_Opp__c==true  && c.mkto71_Lead_Score__c>=100 ) {   
                        
			Opportunity opp = new Opportunity(Name=c1.Account.Name, AccountID=c.AccountId, StageName='Open', CloseDate=Date.Today().addDays(+30),ownerID = c1.Account.OwnerId);
                        insert opp;
                      	
			opportunityCOntactRole oppCOn = new opportunityCOntactRole(OpportunityId=opp.id, contactId= c.Id, isPrimary=true);
                      	insert oppCon;

                      }    
                  }    
                }     

                        
          }    
}
Thanks

 

 

 

 

Is it possible to create a custom formula to count the number of characters in a Text field? 

I have a business requirement to build a formula that calculate how complete the data is on a custom record and then return a corresponding image (green, yellow, red traffic lights).  

 

As part of this requirement I need to count the # of characters in a long text field.  Unfortunately, long text fields can not be used as part of a Formula, otherwise I would use len(long_text_field__c) < 100 as part of my equation.

 

Plan B is to use a trigger to count the # of characters in the long_text_field__c and insert into count_of_long_text_field__c.  Only one issue here - apex is a mystery to me.

 

While I am not a programmer, I attempted to find some javascript code online and change it to work in a trigger.  I didn't find anything simple enough that will allow me to make an honest attempt at buildling my first trigger.

 

Can someone help me get started here?